gensim-语料库和向量空间

字符串到向量

string = ["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"]

cut_string = [item.split() for item in string]

#创建词频字典
from collections import defaultdict
dic = defaultdict(int)
for item in cut_string:
    for cut in item:
        dic[cut]+=1

#将原文本中频数大于2的词汇保存下来
result_string = [[cut for cut in item if dic[cut]>=2] for item in cut_string]

总结:--基于python的总结

  1. 善于生成器的使用,只要是对列表进行操作,并且生成列表,那么可以使用生成式,可以替代其他语言中比较复杂的for循环的语法
  2. 对于dafualtdict的使用,只要结果是希望生成一个字典,则可以使用该方法,dic = defaultdict(int)中的参数用来指定字典值的类型。

词袋模型

词袋模型->就是对于一篇文档,将其整理成 “该词在文档中的出现次数?1 次。”的形式.
我们将词用一个整型 ID 来表示,词的 ID 和其词频之间的映射表即被称作词典

基础模型:

接下来展示建立词袋模型,并且建立词向量的过程:

#步骤一:建立词典
from gensim import corpora
dictionary = corpora.Dictionary(result_string)
…… 
Dictionary(12 unique tokens)

最后可以看到整个语料库中只有 12 个单词,这也就意味着,每篇文档可以用一个 12 维的向量来表示。

#步骤二:查看词典信息
#1.可以使用token2id来查看token-id
print(dictionary.token2id)


#2.可以使用dfs查看id-词频(frequency)
#步骤三:建立词向量
new_vec = dictionary.doc2bow(“Hunman computer interaction”.split())
…… 
[(0, 1), (1, 1)] 
返回结果[(0,1),(1,1)]表示:在文档
“Hunman computer interaction”中,单词“computer”(ID 为 0)出现了 1 次,
单词“human”(ID 为 1)出现了 1 次,词典中的其余 10 个单词出现次数均为
0。 

改进模型-建立向量:

以上建立模型存在的问题,每次处理的结果都保存在内存里,当数据量较大时难以应对压力,所以需要对其进行改进,一个主要的方法就是对其使用生成器,生成器是python大数据处理的一个利器。
生成器使用示例:

class mycorpora(object):
    def __iter__(self):
        with open(文件地址) as f:
            for line in f:
                yield corpora.doc2bow(line.split())

在__iter__函数中,对语料库数据进行解析,将每一篇文档变成一个只包含单词的list,然后通过建好的词典将这个 list 转换成一个能表示该文档的稀疏向量。

corpus_memory_friendly = mycorpora() 

for vector in corpus_memory_friendly: 
  print(vector) 
…… 
[(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)] 

尽管输出和此前 list 的输出是一样的,但是这种方法却是非常节省内存的,因为每次只有一个向量留在内存中,如此一来,你可以处理任意大的语料库。

改进模型--建立字典

from six import iteritems 

#此处接收生成器
dictionary = corpora.Dictionary(line.lower().split() for line in open('mycorpus.txt 

')) 

#使用iteritems迭代字典效率较高
once_ids = [tokenid for tokenid, docfreq in iteritems(dictionary.dfs) if docfreq == 1] 

#字典过滤掉id
dictionary.filter_tokens(stop_ids + once_ids) 

#由于字典过滤掉id,所以使用以下的方法可以使其变得紧凑
dictionary.compactify() 
print(dictionary) 
…… 
Dictionary(12 unique tokens) 

总结:--基于gensim

  1. corpora.Dictionary(参数)--->接收迭代器
  2. dictionary.dfs,dictionary.token2bow---->用来查看 词语-id,和id->频数
  3. dictionary.doc2bow(参数)---->用来将文档转换为向量
  4. dictionary.filter_tokens(参数)---->需要过滤掉的id的列表
  5. dictionary.compactify()----->使词典变得紧凑

总结:--基于生成器
生成器可以用来处理大数据:

  1. 当一个处理的方法是处理一条数据时,则可以通过for循环结合yield将每条数据传递给处理的方法,然后用for循环得到处理后的结果
  2. 也可以将生成器当成一个整体传递给处理方法,前提是处理方法里有关于该整体的for循环,这样可以将生成器当做和列表没有区别,对其进行循环正常处理即可。

你可能感兴趣的:(gensim-语料库和向量空间)