前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自然语言处理 | 使用Spacy 进行自然语言处理(二)

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

作者头像
用户1622570
发布2018-09-14 18:35:13
2.2K0
发布2018-09-14 18:35:13
举报
文章被收录于专栏:机器学习和数学

上次我们简单介绍了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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-08-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器学习和数学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
NLP 服务
NLP 服务(Natural Language Process,NLP)深度整合了腾讯内部的 NLP 技术,提供多项智能文本处理和文本生成能力,包括词法分析、相似词召回、词相似度、句子相似度、文本润色、句子纠错、文本补全、句子生成等。满足各行业的文本智能需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档