簡體   English   中英

給定一個詞,我們可以使用 Spacy 獲得所有可能的引理嗎?

[英]Given a word can we get all possible lemmas for it using Spacy?

輸入詞是獨立的,不是句子的一部分,但我想得到它所有可能的詞條,就好像輸入詞在不同的句子中一樣,帶有所有可能的 POS 標簽。 我還想獲得單詞引理的查找版本。

我為什么要這樣做?

我從所有文檔中提取了引理,並且還計算了引理之間的依賴關系鏈接的數量。 我使用en_core_web_sm完成了這兩項工作。 現在,給定一個輸入詞,我想返回鏈接最頻繁的詞條到輸入詞的所有可能詞條。

所以簡而言之,我想用所有可能的 POS 標簽復制token._lemma的行為,以保持與我計算過的引理鏈接的一致性。

我發現很難直接從 spaCy 中得到引理和變形,而不首先構造一個例句來給它上下文。 這並不理想,所以我進一步觀察,發現LemmaInflect做得很好。

> from lemminflect import getInflection, getAllInflections, getAllInflectionsOOV

> getAllLemmas('watches')
{'NOUN': ('watch',), 'VERB': ('watch',)}

> getAllInflections('watch')
{'NN': ('watch',), 'NNS': ('watches', 'watch'), 'VB': ('watch',), 'VBD': ('watched',), 'VBG': ('watching',), 'VBZ': ('watches',),  'VBP': ('watch',)}

spaCy 並不是為此而設計的——它是為分析文本而不是生成文本而設計的。

鏈接庫看起來不錯,但如果您想堅持使用 spaCy 或需要英語以外的語言,您可以查看spacy-lookups-data ,這是用於引理的原始數據。 通常,每個詞性都會有一本字典,可以讓您查找單詞的引理。

為了獲得替代引理,我正在嘗試結合使用 Spacy rule_lemmatize和 Spacy 查找數據。 rule_lemmatize可能會產生不止一個有效的引理,而查找數據只會為給定的單詞提供一個引理(在我檢查過的文件中)。 然而,在某些情況下,查找數據會產生引理,而rule_lemmatize不會。

我的例子是西班牙語:

import spacy
import spacy_lookups_data

import json
import pathlib

# text = "fui"
text = "seguid"
# text = "contenta"
print("Input text: \t\t" + text)

# Find lemmas using rules:
nlp = spacy.load("es_core_news_sm")
lemmatizer = nlp.get_pipe("lemmatizer")
doc = nlp(text)
rule_lemmas = lemmatizer.rule_lemmatize(doc[0])
print("Lemmas using rules: " + ", ".join(rule_lemmas))

# Find lemma using lookup:
lookups_path = str(pathlib.Path(spacy_lookups_data.__file__).parent.resolve()) + "/data/es_lemma_lookup.json"
fileObject = open(lookups_path, "r")
lookup_json = fileObject.read()
lookup = json.loads(lookup_json)
print("Lemma from lookup: \t" + lookup[text])

Output:

Input text:         fui        # I went; I was (two verbs with same form in this tense)
Lemmas using rules: ir, ser    # to go, to be (both possible lemmas returned)
Lemma from lookup:  ser        # to be

Input text:         seguid     # Follow! (imperative)
Lemmas using rules: seguid     # Follow! (lemma not returned) 
Lemma from lookup:  seguir     # to follow

Input text:         contenta   # (it) satisfies (verb); contented (adjective) 
Lemmas using rules: contentar  # to satisfy (verb but not adjective lemma returned)
Lemma from lookup:  contento   # contented (adjective, lemma form)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM