spaCyTest.py 662 Bytes
import spacy
from spacy import displacy
nlp = spacy.load("en_core_web_sm")
# 导入工具包,下载英语模型

doc = nlp('Weather is good, very windy and sunny. We have no classes in the afternoon.')

# 分词
for token in doc:
    print(token)

# 分句
for token in doc.sents:
    print(token)

# 词性
for token in doc:
    print('{}:{}'.format(token, token.pos_))

# 命名体识别
doc_2 = nlp('I went to Paris where I met my old friend Jack from uni.')
for ent in doc_2.ents:
    print('{}:{}'.format(ent, ent.label_))
# 展示
# doc1 = nlp('I went to Paris where I met my old friend Jack from uni.')
# displacy.render(doc1, style='ent', jupyter=True)