文本向量化------从文本到向量


from gensim import corpora, models, similarities

documents = ["Human machine interface for lab abc computer applications",

              "A survey of user opinion of computer system response time",

              "The EPS user interface management system",

              "System and human system engineering testing of EPS",

              "Relation of user perceived response time to error measurement",

              "The generation of random binary unordered trees",

              "The intersection graph of paths in trees",

              "Graph minors IV Widths of trees and well quasi ordering",

              "Graph minors A survey"]  





# remove common words and tokenize

stoplist = set('for a of the and to in'.split())

texts = [[word for word in document.lower().split() if word not in stoplist]

          for document in documents]

 

# remove words that appear only once

all_tokens = sum(texts, [])

tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) == 1)

texts = [[word for word in text if word not in tokens_once]

          for text in texts]

 

print texts

 

[['human', 'interface', 'computer'],

 ['survey', 'user', 'computer', 'system', 'response', 'time'],

 ['eps', 'user', 'interface', 'system'],

 ['system', 'human', 'system', 'eps'],

 ['user', 'response', 'time'],

 ['trees'],

 ['graph', 'trees'],

 ['graph', 'minors', 'trees'],

 ['graph', 'minors', 'survey']] 

去停止词之后 ,文本变成了这个格式,下面我们看一下如何实现了向量化


dictionary = corpora.Dictionary(texts)

##这里实现了单词的--映射

print dictionary.token2id

{'minors': 11, 'graph': 10, 'system': 5, 'trees': 9, 'eps': 8, 'computer': 0,

'survey': 4, 'user': 7, 'human': 1, 'time': 6, 'interface': 2, 'response': 3}

如何用这种方法去表示一片文档呢


new_doc = "Human computer interaction"

new_vec = dictionary.doc2bow(new_doc.lower().split())

print new_vec # the word "interaction" does not appear in the dictionary and is ignored

 

[(0, 1), (1, 1)] 

这是什么意思,首先前面建立的dict没有interaction,自动过滤 

字典0:computer出现了一次所以[0,1]

字典1:human出现了一次所以[1,1]

其他字典词出现0词


corpus = [dictionary.doc2bow(text) for text in texts]

corpora.MmCorpus.serialize('/tmp/deerwester.mm', corpus) # store to disk, for later use

print corpus

 

[(0, 1), (1, 1), (2, 1)]

[(0, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]

[(2, 1), (5, 1), (7, 1), (8, 1)]

[(1, 1), (5, 2), (8, 1)]

[(3, 1), (6, 1), (7, 1)]

[(9, 1)]

[(9, 1), (10, 1)]

[(9, 1), (10, 1), (11, 1)]

[(4, 1), (10, 1), (11, 1)]

同理对应到文本上

猜你喜欢

转载自blog.csdn.net/kylin_learn/article/details/83035804