반응형

/*******************************************************************************************************************
-- Title : [Py3.5] Word Frequency and Group by POS w/ spaCy - ver.dBRang
-- Reference : spacy.io dbrang
-- Key word : nlp word tokenizer word_token word tokenizing word tokenization lemmatization lemmatizing
                  lemma lemmatizer stopword stopwords stop word stop words word frequency word count
                  자연어 처리 워드 토큰 단어 토큰 단어 토크나이저 단어 토크나이징 불용어 단어 빈도수 단어 카운트
                  spacy spacy.load lexeme group by pos dict.get get lemma.split split
*******************************************************************************************************************/

■ Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import pandas as pd
import spacy
from nltk.corpus import stopwords
from collections import Counter
 
 
# =======================================
# -- Set Dataframe Option
# =======================================
pd.set_option('display.max_rows'500)
pd.set_option('display.max_columns'500)
pd.set_option('display.width'1000)
pd.set_option('display.max_colwidth'-1)
 
 
# =======================================
# -- Load Source and Model
# =======================================
 
# --
# -- Load Source
# --
source = "spaCy is an open-source software library for advanced Natural-Language-Processing, " \
         "written in the programming languages Python -Cython 4.5.678. " \
         "It offers the fastest syntactic parser in the world. " \
         "The library is published under the MIT license and currently supports English and German, " \
         "as well as tokenization for Chinese and several other languages. " \
         "Unlike NLTK, which is mainly * intended for teaching and research, " \
         "spaCy focuses on providing software for production usage. As of version 11.0, " \
         "spaCy also supports deep learning workflows that allow connecting statistical models trained " \
         "by popular machine learning libraries like TensorFlow, Keras or Scikit-learn. " \
         "spaCy's machine learning library, Thinc, is also available as a separate open-source Python library."
 
print(source[:150+ '...')
print("... source""." * 100"\n")
 
# --
# -- Load Model
# --
nlp = spacy.load("en")
doc = nlp(source)
 
 
# =======================================
# -- Add stopword
# =======================================
 
# --
# -- set stopwords
# --
 
# -- add stopword for example
#    Ref : dbrang.tistory.com/1205
lst_stopwords = stopwords.words('english')
lst_stopwords.append('spacy')                    # 테스트 용
lst_stopwords.append('python')                   # 테스트 용
 
 
print("lst_stopwords: ", lst_stopwords)
print(",,, stopwords""," * 100"\n")
 
# -- add stopwords from stopwords.words
for sword in lst_stopwords:
    lexeme = nlp.vocab[sword]
    lexeme.is_stop = True
    #print(lexeme.orth_, "|",lexeme.is_stop)
 
 
# =======================================
# -- word_tokenizing with lemma & pos & stopwords
#    Ref : dbrang.tistory.com/1248
# =======================================
 
# --
# -- spacy.pos_가 spacy.tag_보다 큰 Category 개념
# --
lst_want_pos = ['NOUN','PROPN','VERB','ADJ','ADV']
lst_want_tag = ['NN','NNP','VB''VBG''VBD''VBP''VBN''VBZ','RB','JJ']
 
lst_lemma_pos = [(token_word.lemma_ + '_*_' + token_word.pos_)
                 for token_word in doc
                 if (token_word.lemma_ not in lst_stopwords and token_word.pos_ in lst_want_pos)]
 
lst_lemma_tag = [(token_word.lemma_ + '_*_' + token_word.tag_)
                 for token_word in doc
                 if (token_word.lemma_ not in lst_stopwords and token_word.tag_ in lst_want_tag)]
 
print("lst_lemma_pos: ", lst_lemma_pos, "\n")
print("lst_lemma_tag: ", lst_lemma_tag)
print(";;; lst_lemma_pos & _tag without stopword"";" * 100"\n")
 
 
# =======================================
# -- Frequency Counting
# =======================================
cntr_lemma_pos = Counter(lst_lemma_pos)
cntr_lemma_tag = Counter(lst_lemma_tag)
 
print("type(cntr_lemma_pos): ", type(cntr_lemma_pos))
print("cntr_lemma_pos:", cntr_lemma_pos)
print("cntr_lemma_tag:", cntr_lemma_tag)
print("^^^ frequency counting""^" * 100"\n")
 
 
# =======================================
# -- Grouping ('word', count) by POS
# =======================================
dict_pos = dict()
dict_tag = dict()
 
for lemma, freq in cntr_lemma_pos.items():
    word, pos = lemma.split('_*_')
    dict_pos[pos] = dict_pos.get(pos, []) + [(word, freq)]
 
for lemma, freq in cntr_lemma_tag.items():
    word, tag = lemma.split('_*_')
    dict_tag[tag] = dict_tag.get(tag, []) + [(word, freq)]
 
print("type(dict_pos):", type(dict_pos))
print("dict_pos: ", dict_pos)
print("dict_tag: ", dict_tag)
print("*** grouping by pos or tag""*" * 100"\n")
cs



반응형

+ Recent posts