• python -m spacy download zh_core_web_sm
    • 后续使用spacy.load("zh_core_web_sm")时,需要预先下载该模型 ```python import spacy

nlp = spacy.load(“zh_core_web_sm”)

doc = nlp(‘这是一段中文测试文本。来自中国。来自中华人民共和国’)

jy: True; 得到的 doc 是一个可迭代对象;

print(isinstance(doc, Iterable))

print(“1) tokenize ==============================================”)、 for token in doc: print(token, token.is_punct) “”” 这是 False 一 False 段 False 中文 False 测试 False 文本 False 。 True 来自 False 中国 False 。 True 来自 False 中华 False 人民 False 共和国 False “””

print(“2) 词干化(Lemmatize) =====================================”) for token in doc: print(token, token.lemma_, token.lemma) “”” 这是 0 一 0 段 0 中文 0 测试 0 文本 0 。 0 来自 0 中国 0 。 0 来自 0 中华 0 人民 0 共和国 0 “””

print(“3) 词性标注(POS Tagging) =================================”) for token in doc: print(token, token.pos, token.pos, token.tag, token.tag) “”” 这是 VERB 100 VC 18127629375280135038 一 NUM 93 CD 8427216679587749980 段 NUM 93 M 3209455878668673793 中文 NOUN 92 NN 15308085513773655218 测试 VERB 100 VV 652203769202407526 文本 NOUN 92 NN 15308085513773655218 。 PUNCT 97 PU 1367045748362297863 来自 VERB 100 VV 652203769202407526 中国 PROPN 96 NR 4110910393796925761 。 PUNCT 97 PU 1367045748362297863 来自 VERB 100 VV 652203769202407526 中华 PROPN 96 NR 4110910393796925761 人民 NOUN 92 NN 15308085513773655218 共和国 NOUN 92 NN 15308085513773655218 “””

print(“4) 命名实体识别(NER) =====================================”) for entity in doc.ents: print(entity, entity.label_, entity.label)

print(“5) 名词短语提取 ==========================================”) for nounc in doc.noun_chunks: print(nounc) “”” Traceback (most recent call last): File “tmp2.py”, line 13, in for nounc in doc.noun_chunks: File “spacy/tokens/doc.pyx”, line 852, in noun_chunks NotImplementedError: [E894] The ‘noun_chunks’ syntax iterator is not implemented for language ‘zh’. “””

print(“6) 文本分句 ============================================”)
for ix, sent in enumerate(doc.sents, 1): print(“Sentence number {}: {}”.format(ix, sent)) “”” Sentence number 1: 这是一段中文测试文本。 Sentence number 2: 来自中国。 Sentence number 3: 来自中华人民共和国 “”” ```