我是spacy的新手,想使用它的词形还原功能,但我不知道如何使用它,比如将我输入的单词字符串转换成基本形式的字符串。
示例:
- 'words'=> 'word'
- 'did' => 'do'
谢谢。
我是spacy的新手,想使用它的词形还原功能,但我不知道如何使用它,比如将我输入的单词字符串转换成基本形式的字符串。
示例:
谢谢。
之前的回答有些复杂,无法编辑,所以这里提供一个更加传统的回答。
# make sure your downloaded the english model with "python -m spacy download en"
import spacy
nlp = spacy.load('en')
doc = nlp(u"Apples and oranges are similar. Boots and hippos aren't.")
for token in doc:
print(token, token.lemma, token.lemma_)
输出:
Apples 6617 apples
and 512 and
oranges 7024 orange
are 536 be
similar 1447 similar
. 453 .
Boots 4622 boot
and 512 and
hippos 98365 hippo
are 536 be
n't 538 not
. 453 .
如果你只想使用词形归并器,可以按照以下方式操作:
from spacy.lemmatizer import Lemmatizer
from spacy.lang.en import LEMMA_INDEX, LEMMA_EXC, LEMMA_RULES
lemmatizer = Lemmatizer(LEMMA_INDEX, LEMMA_EXC, LEMMA_RULES)
lemmas = lemmatizer(u'ducks', u'NOUN')
print(lemmas)
输出
['duck']
更新
自从spacy 2.2版本以来,LEMMA_INDEX、LEMMA_EXC和LEMMA_RULES已经被打包成一个Lookups
对象:
import spacy
nlp = spacy.load('en')
nlp.vocab.lookups
>>> <spacy.lookups.Lookups object at 0x7f89a59ea810>
nlp.vocab.lookups.tables
>>> ['lemma_lookup', 'lemma_rules', 'lemma_index', 'lemma_exc']
你仍然可以直接使用词形还原器,需要提供一个单词和它的词性标记(POS)。
You can still use the lemmatizer directly with a word and a POS (part of speech) tag:
你仍然可以直接使用词形还原器,需要提供一个单词和它的词性标记(POS)。
from spacy.lemmatizer import Lemmatizer, ADJ, NOUN, VERB
lemmatizer = nlp.vocab.morphology.lemmatizer
lemmatizer('ducks', NOUN)
>>> ['duck']
您可以像上面那样将 POS 标签作为导入的常量传递,也可以作为字符串传递:lemmatizer('ducks', 'NOUN')
>>> ['duck']
从spacy.lemmatizer模块中导入Lemmatizer、ADJ、NOUN、VERB。
无法从'spacy.lang.en'中导入'LEMMA_INDEX'
。 - Daniel dos Santos代码:
import os
from spacy.en import English, LOCAL_DATA_DIR
data_dir = os.environ.get('SPACY_DATA', LOCAL_DATA_DIR)
nlp = English(data_dir=data_dir)
doc3 = nlp(u"this is spacy lemmatize testing. programming books are more better than others")
for token in doc3:
print token, token.lemma, token.lemma_
输出:
this 496 this
is 488 be
spacy 173779 spacy
lemmatize 1510965 lemmatize
testing 2900 testing
. 419 .
programming 3408 programming
books 1011 book
are 488 be
more 529 more
better 615 better
than 555 than
others 871 others
示例参考: 这里
我使用的是 Spacy 2.x 版本。
import spacy
nlp = spacy.load('en_core_web_sm', disable=['parser', 'ner'])
doc = nlp('did displaying words')
print (" ".join([token.lemma_ for token in doc]))
并输出:
do display word
希望它能帮到你 :)
Hope it helps :)
要获取单词和它们的词形变化之间的映射关系,请使用以下内容:
import spacy
# instantiate pipeline with any model of your choosing
nlp = spacy.load("en_core_web_lg")
words = "Those quickest and brownest foxes jumped over the laziest ones."
# only enable the needed pipeline components to speed up processing
with nlp.select_pipes(enable=['tok2vec', 'tagger', 'attribute_ruler', 'lemmatizer']):
doc = nlp(words)
lemma_mapping = dict([(token.text, token.lemma_)
for token in doc if token.is_punct==False])
print(lemma_mapping)
输出
{'Those': 'those',
'quickest': 'quick',
'and': 'and',
'brownest': 'brown',
'foxes': 'fox',
'jumped': 'jump',
'over': 'over',
'the': 'the',
'laziest': 'lazy',
'ones': 'one'}
我使用了:
import spacy
nlp = en_core_web_sm.load()
doc = nlp("did displaying words")
print(" ".join([token.lemma_ for token in doc]))
>>> do display word
但它返回了
OSError: [E050] Can't find model 'en_core_web_sm'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.
我使用了:
pip3 install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.2.0/en_core_web_sm-2.2.0.tar.gz
消除错误。