python - 如何使用 NLP 库使复合词成为单数?
问题描述
问题
我正在尝试使用spaCy将复合词从复数变为单数。
但是,我无法修复将复数转换为单数作为复合词的错误。
如何获得如下所示的首选输出?
cute dog
two or three word
the christmas day
开发环境
蟒蛇 3.9.1
错误
print(str(nlp(word).lemma_))
AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'lemma_'
代码
import spacy
nlp = spacy.load("en_core_web_sm")
words = ["cute dogs", "two or three words", "the christmas days"]
for word in words:
print(str(nlp(word).lemma_))
审判
cute
dog
two
or
three
word
the
christmas
day
import spacy
nlp = spacy.load("en_core_web_sm")
words = ["cute dogs", "two or three words", "the christmas days"]
for word in words:
word = nlp(word)
for token in word:
print(str(token.lemma_))
解决方案
正如您所发现的,您无法获得文档的引理,只能获得单个单词的引理。多词表达在英语中没有引理,引理仅适用于单个单词。但是,方便的是,在英语中,复合词只需将最后一个单词复数即可,因此您可以将最后一个单词设为单数。这是一个例子:
import spacy
nlp = spacy.load("en_core_web_sm")
def make_compound_singular(text):
doc = nlp(text)
if len(doc) == 1:
return doc[0].lemma_
else:
return doc[:-1].text + doc[-2].whitespace_ + doc[-1].lemma_
texts = ["cute dogs", "two or three words", "the christmas days"]
for text in texts:
print(make_compound_singular(text))
推荐阅读
- java - 排序单元 Java 程序
- react-native - React Native 中的对齐按钮
- angular - Angular 内联编辑设计
- javascript - Firebase Google signInWithRedirect 未在成功验证时重定向到指定页面
- swift - 上传构建时 SDK 版本问题失败
- r - R - 扩散模型中的非线性回归时间间隔估计
- c++ - OpenGL反射未正确映射天空盒
- php - 使用 XMLReader() 的内部服务器错误 500
- netbeans-8 - JDK11 + NetBeans 8.2 - 无法访问 java.lang
- c++ - c++ 运算符重载未按预期工作