自然语言处理 | 使用Spacy 进行自然语言处理(二)

2018 年 8 月 27 日 机器学习和数学


上次我们简单介绍了Spacy,学习了它的安装以及实体识别等基本的方法。今天我继续给大家介绍一下它的其他功能如何操作,主要有词性还原,词性标注,名词块识别,依存分析等内容。废话不多说,直接看代码。



  1. import en_core_web_sm

  2. parser = en_core_web_sm.load()

  3. sentences = "There is an art, it says, or rather, a knack to flying." \

  4.            "The knack lies in learning how to throw yourself at the ground and miss." \

  5.            "In the beginning the Universe was created. This has made a lot of people " \

  6.            "very angry and been widely regarded as a bad move."

  7. print("解析文本中包含的句子:")

  8. sents = [sent for sent in parser(sentences).sents]

  9. for x in sents:

  10.    print(x)

  11. """

  12. There is an art, it says, or rather, a knack to flying.

  13. The knack lies in learning how to throw yourself at the ground and miss.

  14. In the beginning the Universe was created.

  15. This has made a lot of people very angry and been widely regarded as a bad move.

  16. """

  17. print("- * -"*20)

  18. # 分词

  19. print()

  20. tokens = [token for token in sents[0] if len(token) > 1]

  21. print(tokens)

  22. print("- * -"*20)

  23. # 词性还原

  24. lemma_tokens = [token.lemma_ for token in sents[0] if len(token) > 1]

  25. print(lemma_tokens)

  26. print("- * -"*20)

  27. # 简化版的词性标注

  28. pos_tokens = [token.pos_ for token in sents[0] if len(token) > 1]

  29. print(pos_tokens)

  30. print("- * -"*20)

  31. # 词性标注的细节版

  32. tag_tokens = [token.tag_ for token in sents[0] if len(token) > 1]

  33. print(tag_tokens)

  34. print("- * -"*20)

  35. # 依存分析

  36. dep_tokens = [token.dep_ for token in sents[0] if len(token) > 1]

  37. print(dep_tokens)

  38. print("- * -"*20)

  39. print("名词块分析")

  40. doc = parser(u"Autonomous cars shift insurance liability toward manufacturers")

  41. # 获取名词块文本

  42. chunk_text = [chunk.text for chunk in doc.noun_chunks]

  43. print(chunk_text)

  44. print("- * -"*20)

  45. # 获取名词块根结点的文本

  46. chunk_root_text = [chunk.root.text for chunk in doc.noun_chunks]

  47. print(chunk_root_text)

  48. print("- * -"*20)

  49. # 依存分析

  50. chunk_root_dep_ = [chunk.root.dep_ for chunk in doc.noun_chunks]

  51. print(chunk_root_dep_)

  52. print("- * -"*20)

  53. #

  54. chunk_root_head_text = [chunk.root.head.text for chunk in doc.noun_chunks]

  55. print(chunk_root_head_text)

  56. print("- * -"*20)


最后给大家附上一个句法依存分析的结果解释的资料,是斯坦福自然语言处理的一个依存句法分析的解释文档

链接:https://nlp.stanford.edu/software/dependencies_manual.pdf

如果下载不下来,可以微信和我要。


百度文库有中文版:https://wenku.baidu.com/view/1e92891dbceb19e8b8f6bae5.html







登录查看更多
10

相关内容

深度学习自然语言处理概述,216页ppt,Jindřich Helcl
专知会员服务
209+阅读 · 2020年4月26日
【教程】自然语言处理中的迁移学习原理,41 页PPT
专知会员服务
93+阅读 · 2020年2月8日
计算机视觉最佳实践、代码示例和相关文档
专知会员服务
17+阅读 · 2019年10月9日
学习自然语言处理路线图
专知会员服务
133+阅读 · 2019年9月24日
R语言自然语言处理:词性标注与命名实体识别
R语言中文社区
7+阅读 · 2019年3月5日
自然语言处理NLP快速入门
专知
19+阅读 · 2018年10月8日
自然语言处理 | 使用Spacy 进行自然语言处理
机器学习和数学
18+阅读 · 2018年8月22日
在Python中使用SpaCy进行文本分类
专知
24+阅读 · 2018年5月8日
教你用Python进行自然语言处理(附代码)
数据派THU
6+阅读 · 2018年3月28日
【推荐】自然语言处理(NLP)指南
机器学习研究会
35+阅读 · 2017年11月17日
NLP自然语言处理(二)——基础文本分析
乐享数据DataScientists
12+阅读 · 2017年2月7日
Arxiv
6+阅读 · 2019年7月11日
Arxiv
6+阅读 · 2018年11月1日
Arxiv
26+阅读 · 2018年9月21日
Arxiv
21+阅读 · 2018年8月30日
Arxiv
5+阅读 · 2018年1月30日
VIP会员
相关资讯
R语言自然语言处理:词性标注与命名实体识别
R语言中文社区
7+阅读 · 2019年3月5日
自然语言处理NLP快速入门
专知
19+阅读 · 2018年10月8日
自然语言处理 | 使用Spacy 进行自然语言处理
机器学习和数学
18+阅读 · 2018年8月22日
在Python中使用SpaCy进行文本分类
专知
24+阅读 · 2018年5月8日
教你用Python进行自然语言处理(附代码)
数据派THU
6+阅读 · 2018年3月28日
【推荐】自然语言处理(NLP)指南
机器学习研究会
35+阅读 · 2017年11月17日
NLP自然语言处理(二)——基础文本分析
乐享数据DataScientists
12+阅读 · 2017年2月7日
相关论文
Arxiv
6+阅读 · 2019年7月11日
Arxiv
6+阅读 · 2018年11月1日
Arxiv
26+阅读 · 2018年9月21日
Arxiv
21+阅读 · 2018年8月30日
Arxiv
5+阅读 · 2018年1月30日
Top
微信扫码咨询专知VIP会员