英文稿分析-1

字詞解析程式碼分析程式碼

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

#!/usr/bin/env python3# -*- coding: utf-8 -*-import sysimport codecsimport stringimport nltkimport numpyimport mathimport igraphfrom matplotlib import pyplot as plt from matplotlib.font_manager import FontProperties class TextFileProcessing: __numList__ = None __puncList__ = None __stopWords__ = None __additional__ = ['also', 'paper', 'always', 'figure', 'table', 'shown', 'illustrated', 'shows'] def __init__(self): self.__numList__ = string.digits self.__puncList__ = string.punctuation self.__stopWords__ = set(nltk.corpus.stopwords.words('english')) def __filterOut__(self, word): _word = word if _word in self.__additional__: _word = '' for ch in self.__numList__: _word = _word.replace(ch, '') for ch in self.__puncList__: _word = _word.replace(ch, '') if _word in self.__stopWords__: _word = '' return _word def __isWord__(self, word): return len(nltk.corpus.wordnet.synsets(word)) > 0 def LoadFile(self, fileName): content = [] termFreqList = {} with codecs.open(fileName, 'r', 'utf-8') as textFifle: for line in textFifle: _line = line.strip().lower() _wordList = nltk.word_tokenize(_line) for word in _wordList: _word = self.__filterOut__(word) if len(_word) > 3 and self.__isWord__(_word): content.append(_word) if word in termFreqList: termFreqList[_word] = termFreqList[_word] + 1 else: termFreqList[_word] = 1 return (content, termFreqList) def LoadFile2Distance(self, fileName): content = [] with codecs.open(fileName, 'r', 'utf-8') as textFifle: for line in textFifle: _line = line.strip().lower() _wordList = nltk.word_tokenize(_line) for word in _wordList: _word = self.__filterOut__(word) if len(_word) > 3 and self.__isWord__(_word): content.append(_word) termFreqList = {} for pos in range(0, len(content) - 1): for w in range(pos + 1, len(content)): if content[pos] == content[w]: continue _word = '%s|%s' % (content[pos], content[w]) if content[pos] > content[w]: _word = '%s|%s' % (content[w], content[pos]) if _word in termFreqList: termFreqList[_word] += w - pos else: termFreqList[_word] = w - pos return (content, termFreqList) def LoadFile2Graph(self, fileName, minimal=30): content = [] with codecs.open(fileName, 'r', 'utf-8') as textFifle: for line in textFifle: _line = line.strip().lower() _wordList = nltk.word_tokenize(_line) for word in _wordList: _word = self.__filterOut__(word) if len(_word) > 3 and self.__isWord__(_word): content.append(_word) termFreqList = {} for pos in range(0, len(content) - 1): for w in range(pos + 1, len(content)): if content[pos] == content[w]: continue _word = '%s|%s' % (content[pos], content[w]) if content[pos] > content[w]: _word = '%s|%s' % (content[w], content[pos]) if _word in termFreqList: termFreqList[_word] += w - pos else: termFreqList[_word] = w - pos g = igraph.Graph() nodeLabels = [] for term in sorted(termFreqList, key=termFreqList.get, reverse=True): if minimal > 0: nodes = term.split('|') for node in nodes: try: _node = g.vs.find(name=node) except: g.add_vertex(node) nodeLabels.append(node) g.add_edge(nodes[0], nodes[1], weight=termFreqList[term]) minimal -= 1 return g def OutputFreq(self, termFreqList, minimal=None): _freq = [] for term in termFreqList: _freq.append(termFreqList[term]) freq = numpy.array(_freq) if minimal is None: minimal = freq.mean() + freq.std() for term in sorted(termFreqList, key=termFreqList.get, reverse=True): if termFreqList[term] > minimal: print('%s:%d' % (term, termFreqList[term]), end=' ') print() def OutputDistanceFreq(self, termFreqList, minimal=None): _freq = [] for term in termFreqList: _freq.append(termFreqList[term]) freq = numpy.array(_freq) if minimal is None: minimal = math.ceil(freq.mean() + freq.std()) for term in sorted(termFreqList, key=termFreqList.get, reverse=True): if termFreqList[term] > minimal: print('%s:%d' % (term, termFreqList[term] - minimal)) print() def Output(self, content): for word in content: print(word, end=' ') print() if __name__ == '__main__': fileName = 'data/1.txt' if len(sys.argv) > 1: fileName = sys.argv[1] worker = TextFileProcessing() content = None termFreqList = None taskControl = [False, False, True] if taskControl[0]: content, termFreqList = worker.LoadFile(fileName) else: content, termFreqList = worker.LoadFile2Distance(fileName) if taskControl[1]: worker.Output(content) if taskControl[2]: if taskControl[0]: worker.OutputFreq(termFreqList) else: worker.OutputDistanceFreq(termFreqList)

程式輸出

innovation|product:257666 innovation|service:248808 innovation|market:239000 market|product:216479 innovation|pricing:203296 competitiveness|innovation:203056 product|service:195432 pricing|product:194946 competitiveness|product:192808 business|innovation:190522 competitiveness|service:184148 market|service:182808 pricing|service:182252 market|pricing:180561 competitiveness|market:180187 innovation|model:174720 business|product:169207 model|product:162867 business|market:152618 competitiveness|pricing:150966 market|model:148002 business|competitiveness:147121 competition|innovation:144138 business|pricing:143645 model|service:142384 innovation|products:135134 business|service:133566 competitiveness|model:133073 model|pricing:132767 features|innovation:128866 competition|product:128475 innovation|innovator:126200 innovator|service:122112 innovator|product:122083 innovation|simulation:119946 innovator|market:115383 competition|market:114859 product|simulation:113626 business|model:113132 demand|innovation:113124 dynamic|innovation:112480 competition|competitiveness:108521