gpt4 book ai didi

python - (生物医学)词干的所有可能词形补全

转载 作者:行者123 更新时间:2023-11-28 18:36:53 25 4
gpt4 key购买 nike

我熟悉 R 中 tm 包的词干提取和补全。

我正在尝试想出一种快速而肮脏的方法来查找给定单词的所有变体(在某些语料库中。)例如,如果我的输入是,我想得到“leukocytes”和“leuckocytic” “白细胞”。

如果我现在必须这样做,我可能会选择类似的东西:

library(tm)
library(RWeka)
dictionary <- unique(unlist(lapply(crude, words)))
grep(pattern = LovinsStemmer("company"),
ignore.case = T, x = dictionary, value = T)

我用Lovins是因为Snowball的Porter好像攻击性不够。

我愿意接受有关其他词干分析器、脚本语言(Python?)或完全不同的方法的建议。

最佳答案

此解决方案需要预处理您的语料库。但是一旦完成,它就是一个非常快速的字典查找。

from collections import defaultdict
from stemming.porter2 import stem

with open('/usr/share/dict/words') as f:
words = f.read().splitlines()

stems = defaultdict(list)

for word in words:
word_stem = stem(word)
stems[word_stem].append(word)

if __name__ == '__main__':
word = 'leukocyte'
word_stem = stem(word)
print(stems[word_stem])

对于 /usr/share/dict/words 语料库,这会产生结果

['leukocyte', "leukocyte's", 'leukocytes']

它使用 stemming可以安装的模块

pip install stemming

关于python - (生物医学)词干的所有可能词形补全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31596476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com