python - Python:自然语言处理 (NLP)
问题描述
我在 HiveQL 数据库中有十亿个字符串。我正在将它们加载到 Python 中。字符串不是用空格分隔的,看起来像:
"mynameisanon"
...
"helloworld"
我想计算每个字符串中的单词数。但首先,我需要一本字典。我不知道如何获取单词词典,但假设我有以下词典:
{ hello, world, my, name, is}
然后该功能将像这样工作:
Input: mynameisanon
Output: 3
Input: helloworld
Output: 2
最后,我想要一张 Pandas 桌子。
解决方案
正如我在评论中提到的,在一般情况下,这不能唯一完成,但假设有一个字典可以解释这一点:
(没有特别好测试):
strings = ["mynameisanon", "helloworld"]
words = ["hello", "world", "my", "name", "is"]
for string in strings:
count = 0
max_interval = len(string)
for interval_length in range(1,max_interval+1):
for interval_start in range(0, len(string)+1-interval_length):
interval = string[interval_start:(interval_start+interval_length)]
if interval in words:
count += 1
print(string)
print(count)
这假设单词可以小到一个字母,也可以和整个字符串一样长,并检查这些值之间所有长度的单词
推荐阅读
- javascript - 将我的静态代码与我的动态代码结合起来。(避免匹配同名玩家)
- flutter - 在 Flutter 中调用 super.dispose 失败的错误 - 尽管调用了 super.dipose()?
- python - 如何在函数参数中键入提示“SupportsOrdering”?
- jenkins - 詹金斯的工作是在提到的位置发送位置而不是内容
- excel - 使用宏 VBA 在每张工作簿上使用 VPageBreaks
- c# - 自动化 DataGridView_CellPainting
- oracle - 为物化视图更新测试生成自愿错误
- arrays - 如何将对象传递给另一个数组对象
- pine-script - 如何在 pine-script 中绘制直到昨天的标签而不是当天的标签
- c++ - 在CMD中用MingW编译显示找不到libisl-21.dll