[英]How can I make compounds words singular using an NLP library?
我正在嘗試使用spaCy將復合詞從復數變為單數。
但是,我無法修復將復數轉換為單數作為復合詞的錯誤。
如何獲得如下所示的首選 output?
cute dog
two or three word
the christmas day
開發環境
Python 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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.