python - 如何在没有预定义的多词列表的情况下识别句子中的多词表达
问题描述
我正在尝试识别句子中的每个多词表达并标记该句子。例如,示例输入语句是“总之,这个商品供不应求”。我希望输出如下所示:
['In short', ',', 'this', 'merchandise', 'is', 'in short supply', '.']
我已经通过使用预定义列表和以下 python 代码实现了上述结果。
from nltk import sent_tokenize, word_tokenize
from nltk.tokenize import MWETokenizer
multiwordExpressionList = [("In", "short"), ("in", "short", "supply" )] ## this is a predefined list
sentence = "In short, this merchandise is in short supply."
mwe = MWETokenizer(multiwordExpressionList, separator = ' ')
resultList = mwe.tokenize(word_tokenize(sentence))
print(resultList)
但是,缺点很明显。这个程序需要一个预定义的多词表达列表来识别句子中是否存在任何多词表达。是否有任何建议的 python 包、模块或方法可以识别句子中存在的任何多词表达式?
解决方案
您有 2 个选择:要么使用一些现有模型(例如,也支持 MWE https://stanfordnlp.github.io/stanza/mwt.html的 Stanza ),或者如果您有足够的数据构建自己的 MWE 模型,例如使用 Gensim:https ://radimrehurek.com/gensim/models/phrases.html。我已经相当成功地使用了第二个选项。
推荐阅读
- php - SQL LIKE 字面上没有显示任何结果(根本)
- c++ - 我有一个嵌套数组,它由另一个数组中的元素组成 2 。之后,我想显示每个元素的属性
- excel - 有没有办法旋转 OLEobjects?
- django - 博客文章不显示图像、粗体文本或其他属性,仅显示原始代码... Django/Ckeditor
- javascript - 如何将jsx元素存储到javascript变量
- rust - 如何固定库的间接依赖项?
- angular - 如何在不刷新的情况下更新角度页面?
- mysql - 计算子选择 SQL 的百分比
- algorithm - 在无向图中找到所有循环的任何有效算法?
- r - Rstudio 在控制台中呈现,但在 Markdown 中输出到 HTML 时出错