6

【NLP】NLTK学习笔记

 2 years ago
source link: https://www.guofei.site/2021/05/02/nltk.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【NLP】NLTK学习笔记

2021年05月02日

Author: Guofei

文章归类: 2-4-NLP ,文章编号: 341


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2021/05/02/nltk.html

Edit

词频统计

from bs4 import BeautifulSoup
import requests

# 先用爬虫获取一段语料
text = BeautifulSoup(requests.get('https://www.python.org/').text, 'lxml').get_text()


import nltk

freq_dist = nltk.FreqDist(text.split())  # 类似 Counter 的词频统计
freq_dist.plot(50, cumulative=False)  # 画图

同义词

from nltk.corpus import wordnet

wordnet.synsets('motorcar')  # 返回一个对象,等同于 wordnet.synset('car.n.01'),含义是 car 的名词释义中的第1个

wordnet.synset('car.n.01').lemma_names()  # 返回同义词:['car', 'auto', 'automobile', 'machine', 'motorcar']


# 有些词是一词多义的,例如 car
wordnet.synsets('car')  # 返回 5 类近义词
# 用一样的方法打印每个类型都有哪些近义词
for i in wordnet.synsets('car'):
    print(i.lemma_names())


# 得到:
# ['car', 'auto', 'automobile', 'machine', 'motorcar']
# ['car', 'railcar', 'railway_car', 'railroad_car']
# ['car', 'gondola']
# ['car', 'elevator_car']
# ['cable_car', 'car']

不但有同义词功能,还有其它:

# 动作的蕴含关系:
wordnet.synset('walk.v.01').entailments()  # [Synset('step.v.01')]
wordnet.synset('eat.v.01').entailments()  # [Synset('chew.v.01'), Synset('swallow.v.01')]
wordnet.synset('tease.v.03').entailments() #[Synset('arouse.v.07'), Synset('disappoint.v.01')]


# 反义词
wordnet.lemma('supply.n.02.supply').antonyms()  # [Lemma('demand.n.02.demand')]

NLP应用

信息摘要

思路:有较多实体和名词的句子,往往重要性较高。因此只需要构建一个重要性评分标准,然后找出前n句。如此简单的算法即可获得不错的效果。
改进:用单词的 TF-IDF 评分。

机器翻译

信息检索

Information Retrieval(IR)


您的支持将鼓励我继续创作!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK