栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用spacy规则化提取自然语言文本信息

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用spacy规则化提取自然语言文本信息

1. 安装spacy
pip install spicy

spicy还需要载入文本库,使用pip的下载方式:

python3 -m spacy download en_core_web_sm

但是很有可能因为网络问题下载速度非常缓慢,所以可以选择到github上去直接下载(注意和自己的spacy版本匹配):github下载链接
下载*.tar.gz文件即可。
然后切换到下载路径,

pip install en_core_web_sm-3.1.0.tar.gz
2. spacy的一些基础用法

使用spacy来处理nlp相关的功能还是很强大的,下面是一些基础用法展示:

import spacy
from spacy import displacy
from spacy.matcher import Matcher

nlp = spacy.load("en_core_web_sm")
text = """
Go to the bedroom with the guitars and black bed and empty the board.
"""
doc = nlp(text)
''' 词性提取
'''
print([(w.text,w.tag_) for w in doc])# 词性-细粒度
print([(w.text,w.pos_) for w in doc])# 词性-粗粒度
print([(w.text,w.label_) for w in doc.ents]) # 实体提取

''' 可视化依赖关系
'''
html_str = displacy.render(doc,style="dep")
    with open('spacy_display.html','w',encoding='utf-8') as f:
        f.write(html_str)
''' 匹配
'''
matcher = Matcher(nlp.vocab)
pattern_1 = [
        {"LOWER":"go"},
        {"TEXT":"to"},
        {"TEXT":"the","OP":"?"},
        {"POS":"NOUN"}
] # go to the xxx
pattern_2 = [
        {"POS":"VERB"},
        {"TEXT":"the","OP":"?"},
        {"POS":"NOUN","OP":"+"}
]
matcher.add("go_to_pattern",[pattern_1])
matcher.add("verb_target_pattern",[pattern_2])
matches = matcher(doc)

for match_id, start, end in matches:
    print(nlp.vocab.strings[match_id])
    matched_span = doc[start:end]
    print(matched_span.text)

匹配的写法教程:https://course.spacy.io/en/chapter1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/879085.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号