在Python或MySQL中是否有一个内置函数可以返回一组3个频繁出现的单词?

2024-04-26 14:10:04 发布

您现在位置:Python中文网/ 问答频道 /正文

python中是否有一个内置函数返回“三个频繁出现的单词集(连续)”。我知道如何通过编程来实现,但我正在寻找一个内置函数。另外,我将这些单词存储在MySQL表中的一行字段中,因此我在python或MySQL中搜索解决方案。在

例如,如果我的数据库包含用户注释作为字段,那么我想检索这些注释中出现频率最高的3个连续词。这三个连续单词的一个例子是“我认为”。 我也知道如何使用SQL对1个单词进行搜索…但我搜索了之前的帖子,却找不到连续的3个单词?在


Tags: 函数用户数据库sql编程mysql解决方案单词
2条回答

对于您所需的内容,没有内置程序,但是这个列表理解应该是有效的,并且非常简洁:

l = 'there are no builtins for that'.split()
print [" ".join(l[n:n+3]) for n in xrange(len(l)-2)]
['there are no', 'are no builtins', 'no builtins for', 'builtins for that']

然后,调用最后一个结果r

^{pr2}$

另一个选择:

>>> from collections import Counter
>>> l = 'zip can be used for that. Counter can be used as well'.lower().split()
>>> Counter(zip(l, l[1:], l[2:]))
Counter({('can', 'be', 'used'): 2, ('used', 'as', 'well'): 1, ('for', 'that.', 'counter'): 1, ('counter', 'can', 'be'): 1, ('be', 'used', 'for'): 1, ('zip', 'can', 'be'): 1, ('used', 'for', 'that.'): 1, ('be', 'used', 'as'): 1, ('that.', 'counter', 'can'): 1})

现在可以应用统计信息:

^{pr2}$

或者,如果您想再次使用连接线:

^{3}$

相关问题 更多 >