【问题标题】:count all occurences of each word from a list that appear in several thousand records in python从出现在python数千条记录中的列表中计算每个单词的所有出现次数
【发布时间】:2017-11-25 00:11:46
【问题描述】:

我有一个评论列表和一个单词列表,我试图计算每个单词在每个评论中出现的次数。关键字列表大约有 30 个,并且可能会增长/更改。目前的评论数量大约为 5000 条,评论字数从 3 到几百个字不等。评论的数量肯定会增加。目前关键字列表是静态的,评论的数量不会增长太多,因此任何在每条评论中获取关键字计数的解决方案都可以工作,但理想情况下,如果评论数量急剧增加或关键字发生变化,所有评论都必须重新分析。

我一直在阅读有关 stackoverflow 的不同方法,但无法使用任何方法。我知道您可以使用skikit learn 来计算每个单词的数量,但还没有弄清楚是否有办法计算一个短语。我也尝试过各种正则表达式。如果关键字列表都是单个单词,我知道我可以很容易地使用skikit learn、循环或正则表达式,但是当关键字有多个单词时我会遇到问题。 我试过的两个链接

Python - Check If Word Is In A String

Phrase matching using regex and Python

这里的解决方案很接近,但它不计算同一单词的所有出现次数 How to return the count of words from a list of words that appear in a list of lists?

关键字和评论列表都是从 MySQL 数据库中提取的。所有关键字都是小写的。所有文本均已小写,除空格外的所有非字母数字均已从评论中删除。我最初的想法是使用skikit learn countvectorizer 来计算单词,但不知道如何计算我切换的短语。我目前正在尝试使用循环和正则表达式,但我愿意接受任何解决方案

# Example of what I am currently attempting with regex
keywords = ['test','blue sky','grass is green']
reviews = ['this is a test. test should come back twice and not 3 times for testing','this pharse contains test and blue sky and look another test','the grass is green test']

 for review in reviews:
     for word in keywords:
         results = re.findall(r'\bword\b',review)  #this returns no results, the variable word is not getting picked up
         #--also tried variations of this to no avail
         #--tried creating the pattern first and passing it
         # pattern = "r'\\b" + word + "\\b'"
         # results = re.findall(pattern,review)  #this errors with the msg: sre_constants.error: multiple repeat at position 9


#The results would be
review1: test=2; 'blue sky'=0;'grass is green'=0
review2: test=2; 'blue sky'=1;'grass is green'=0
review3: test=1; 'blue sky'=0;'grass is green'=1

【问题讨论】:

  • 那么你尝试了哪一个?
  • @user1767754 我尝试了上面 re.iterall 代码中显示的各种正则表达式。对于 re.findall(r'\bword\b',review) 我不确定为什么没有值被传递给变量 word

标签: python regex


【解决方案1】:

我会先用蛮力而不是过度复杂化,然后再尝试优化它。

from collections import defaultdict

keywords = ['test','blue sky','grass is green']
reviews = ['this is a test. test should come back twice and not 3 times for testing','this pharse contains test and blue sky and look another test','the grass is green test']

results = dict()
for i in keywords:
    for j in reviews:
        results[i] = results.get(i, 0) + j.count(i)


print results
>{'test': 6, 'blue sky': 1, 'grass is green': 1}

重要的是我们使用.get 查询字典,以防我们没有密钥集,我们不想处理KeyError 异常。

如果您想走复杂的路线,您可以构建自己的triecounter 结构来在大文本文件中进行搜索。

Parsing one terabyte of text and efficiently counting the number of occurrences of each word

【讨论】:

  • 太好了,这很好用,是的,我肯定是想把它复杂化。我翻转了循环,因为我需要通过审核来计算关键字计数。我将阅读您发送的链接作为将来的参考
【解决方案2】:

您尝试的所有选项均未搜索 word 的值:

  • results = re.findall(r'\bword\b', review) 检查字符串中的单词 word。
  • 当您尝试pattern = "r'\\b" + word + "\\b'" 时,您会检查字符串“r'\b[value of word]\b'。

您可以使用第一个选项,但模式应为r'\b%s\b' % word。这将搜索 word 的值。

【讨论】:

  • 感谢工作。正则表达式中的 %s 是否像 shell 中的 $1 一样工作,您将第一个参数传递给命令?
  • %s 如果是字符串的格式说明符。当您编写 '%s' % 变量时,这意味着您将使用该变量的值创建一个字符串。当您需要使用许多变量时,它很有用:'这里有一个字符串 %s。这里有一个 int %d。这里有一个 float %f' % (str_var, int_var, float_var)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
相关资源
最近更新 更多