我有一个数据表,其中包含来自在线评论的单词元组。它包含太多错别字,因此我尝试删除不属于该词典的单词。我尝试使用的词典是KBBI(印度尼西亚语词典)https://pypi.org/project/kbbi/,从...
pip install kbbi
from kbbi import KBBI
我有麻烦匹配我的数据与字典,因为我不熟悉它的数据类型。函数我发现从原来的资源显示,它允许我们搜索一个词,它将返回的定义。我将只需要在字典内搜索(或者可能其他方式是提取所有文本内的txt文件字典)。这里是一个输入的例子...
# trying to look for "anjing" in the dictionary. Anjing is Indonesian for dog.
anjing = KBBI('anjing')
print (anjing)
其输出
an.jing
1. (n) mamalia yang biasa dipelihara untuk menjaga rumah, berburu, dan sebagainya 〔Canis familiaris〕
2. (n) anjing yang biasa dipelihara untuk menjaga rumah, berburu, dan sebagainya 〔Canis familiaris〕
这就是我期望我的结果会是什么样子(注意粗体字被删除了,因为它不在字典中)...
| 之前|之后|
| - -|- -|
| [马西赫,布洛姆,凯尔,jugagmn,在]|[马西,凯尔]|
| [alhmdllh,sangat,membantu,meski,bunga,cukup,besar](西班牙语)|[阿尔赫姆德勒赫,桑加特,蒙班图,梅斯基,本加,库库普,大]|
以下是我目前为止尝试的方法...
def remove_typo(text):
text = [word for word in text if word in KBBI]
return text
df['after'] = df['before'].apply(lambda x: remove_typo(x))
我在第二行遇到一个错误,说“'type'类型的参数不可迭代”。
4条答案
按热度按时间kpbwa7wx1#
我检查了
kbbi
的文档,解决方案变更为try-except
:fivyi3re2#
text=[如果是BKKI中的单词,则为文本中的单词]
z4iuyo4d3#
首先,确保您确实应该使用
word in KBBI
而不是word in table
。如果这是正确的,则错误来自Series,您可以修改函数,使其在值不正确时立即返回:
或使用例外状况:
vatpfxk54#
第一个