首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将文本转换为向量

将文本转换为向量
EN

Stack Overflow用户
提问于 2018-07-25 07:33:09
回答 2查看 1.8K关注 0票数 2

我有一本字典,里面有单词和每个单词的频率。

代码语言:javascript
复制
{'cxampphtdocsemployeesphp': 1,
'emptiness': 1, 
'encodingundefinedconversionerror': 1, 
'msbuildexe': 2,
'e5': 1, 
'lnk4049': 1,
'specifierqualifierlist': 2, .... }

现在我想用这个字典创建一个单词包模型(我不想使用标准库和函数。我想使用算法来应用这一点。)

  1. 在字典中找出最受欢迎的单词并计算它们。现在我们有了一本最流行单词的字典。
  2. 对于字典中的每个标题,创建一个维数等于N的零向量。
  3. 对于语料库中的每个文本,对字典中的单词进行迭代,并将相应的坐标增加1。

我有我的文本,我将用它来用函数来创建向量。

功能应该是这样的,

代码语言:javascript
复制
def my_bag_of_words(text, words_to_index, dict_size):
"""
    text: a string
    dict_size: size of the dictionary

    return a vector which is a bag-of-words representation of 'text'
"""


 Let say we have N = 4 and the list of the most popular words is 

['hi', 'you', 'me', 'are']

Then we need to numerate them, for example, like this: 

{'hi': 0, 'you': 1, 'me': 2, 'are': 3}

And we have the text, which we want to transform to the vector:
'hi how are you'

For this text we create a corresponding zero vector 
[0, 0, 0, 0]

And iterate over all words, and if the word is in the dictionary, we increase the value of the corresponding position in the vector:
'hi':  [1, 0, 0, 0]
'how': [1, 0, 0, 0] # word 'how' is not in our dictionary
'are': [1, 0, 0, 1]
'you': [1, 1, 0, 1]

The resulting vector will be 
[1, 1, 0, 1]

在应用这方面的任何帮助都是非常有帮助的。我使用python来实现。

谢谢,

尼尔

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-25 13:38:39

您需要首先计算每个术语的语料库频率,为每个单词计算您的案例,并将它们保存在频率字典中。假设樱桃恰巧发生在你的语料库上78次--> 78次,你需要保留。然后根据频率值对频率字典进行排序,然后保持第一个N对。

然后,对于枚举,您可以保留一个字典作为索引。例如,cherry -> term2用于索引字典。

现在,需要准备一个关联矩阵。它将是文件的载体,如下所示:

代码语言:javascript
复制
doc_id   term1 term2 term3 .... termN
doc1       35     0    23         1
doc2        0     0    13         2
   .        .     .     .         .
docM        3     1     2         0

您的语料库中的每个文档(文本、标题、句子)都需要有一个id或索引,并列出上面的内容。现在是为文档创建向量的时候了。迭代您的文档并通过对它们进行标记来获取术语,每个文档都有令牌。迭代令牌,检查频率字典中是否存在下一个令牌。如果为真,请使用索引字典和频率字典更新零向量。

假设doc5有樱桃,我们的第一个流行术语就是它。取其频率( 78)和指数( term5)。现在更新doc5的零向量:

代码语言:javascript
复制
doc_id   term1 term2 term3 .... termN
doc1       35     0    23         1
doc2        0     0    13         2
   .        .     .     .         .
doc5        0    78     0         0 (under process)

您需要针对您的语料库中的每个文档的所有流行术语对每个令牌执行此操作。

最后,您将得到一个NxM矩阵,它在您的语料库中包含M文档的向量。

我可以建议你看看红外读物。https://nlp.stanford.edu/IR-book/information-retrieval-book.html

您可能会考虑使用基于tf-以色列国防军的矩阵,而不是基于语料库频率的术语关联矩阵,就像他们提议的那样。

希望这篇文章能帮上忙

干杯

票数 2
EN

Stack Overflow用户

发布于 2018-07-27 07:27:02

我做了调查,从我的结尾,并想分享我的答案,以及!

我的数据,看起来像这样,已经存储在一个列表中:

代码语言:javascript
复制
data_list = ['draw stacked dotplot r',
 'mysql select records datetime field less specified value',
 'terminate windows phone 81 app',
 'get current time specific country via jquery',
 'configuring tomcat use ssl',...]

接下来,我计算了列表中每个单词的频率,

代码语言:javascript
复制
words_counts = {}                                                      
for text in data_list:
   for word in text.split():
      if word in words_counts:
        words_counts[word] += 1
      else:
        words_counts[word] = 1

因此,我的words_counts字典将包含data_list中的所有单词及其频率。它会看起来像这样

代码语言:javascript
复制
 {'detailed': 6,
 'ole_handle': 1,
 'startmonitoringsignificantlocationchanges': 2,
 'pccf02102': 1,
 'insight': 2,
 'combinations': 26,
 'tuplel': 1}

现在,对于我们的my_bag_of_word函数,我需要按降序排序我的words_counts字典,并为每个单词分配索引。

代码语言:javascript
复制
index_to_word = sorted(words_counts.key(), key = lambda x:words_counts[x], reverse = True) 
words_to_index = {word:i for i,word in enimerate(index_to_words)}

现在我们的words_to_index应该是这样的:

代码语言:javascript
复制
  {'address': 387,
 'behind': 706,
 'page': 23,
 'inherited': 1617,
 '106': 4677,
 'posting': 1293,
 'expressions': 876,
 'occured': 3241,
 'highest': 2989}

最后,我们可以用我们创建的字典得到文本的向量,

代码语言:javascript
复制
def my_bag_of_words(text, words_to_index, size_of_dictionary):
   word_vector = np.zeros(size_of_dictionary)
   for word in text.split():
       if word in words_to_index:
          word_vector[words_to_index[word]] += 1
   return word_vector

这是学习和理解这个概念的好方法。感谢你的帮助和支持。

快乐学习

尼尔

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51513261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档