自然语言22_Wordnet with NLTK
python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campaign=commission&utm_source=cp-400000000398149&utm_medium=share
机器学习,统计项目合作QQ:231469242
Wordnet with NLTK
英语的同义词和反义词函数
# -*- coding: utf-8 -*-
"""
Spyder Editor 英语的同义词和反义词函数
""" import nltk
from nltk.corpus import wordnet
syns=wordnet.synsets('program')
'''
syns
Out[11]:
[Synset('plan.n.01'),
Synset('program.n.02'),
Synset('broadcast.n.02'),
Synset('platform.n.02'),
Synset('program.n.05'),
Synset('course_of_study.n.01'),
Synset('program.n.07'),
Synset('program.n.08'),
Synset('program.v.01'),
Synset('program.v.02')] ''' print(syns[0].name()) '''
plan.n.01
''' #just the word只显示文字,lemma要点
print(syns[0].lemmas()[0].name())
'''
plan
'''
#单词句子使用
print(syns[0].examples())
'''
['they drew up a six-step plan', 'they discussed plans for a new bond issue']
''' '''
synonyms=[]
antonyms=[] list_good=wordnet.synsets("good")
for syn in list_good:
for l in syn.lemmas():
#print('l.name()',l.name())
synonyms.append(l.name())
if l.antonyms():
antonyms.append(l.antonyms()[0].name()) print(set(synonyms))
print(set(antonyms))
''' word="good"
#返回一个单词的同义词和反义词列表
def Word_synonyms_and_antonyms(word):
synonyms=[]
antonyms=[]
list_good=wordnet.synsets(word)
for syn in list_good:
for l in syn.lemmas():
#print('l.name()',l.name())
synonyms.append(l.name())
if l.antonyms():
antonyms.append(l.antonyms()[0].name())
return (set(synonyms),set(antonyms)) #返回一个单词的同义词列表
def Word_synonyms(word):
list_synonyms_and_antonyms=Word_synonyms_and_antonyms(word)
return list_synonyms_and_antonyms[0] #返回一个单词的反义词列表
def Word_antonyms(word):
list_synonyms_and_antonyms=Word_synonyms_and_antonyms(word)
return list_synonyms_and_antonyms[1] '''
Word_synonyms("evil")
Out[43]:
{'evil',
'evilness',
'immorality',
'iniquity',
'malefic',
'malevolent',
'malign',
'vicious',
'wickedness'} Word_antonyms('evil')
Out[44]: {'good', 'goodness'}
'''
wordNet是一个英语词汇数据库,普林斯顿大学创建,是nltk语料库的一部分
WordNet is a lexical database for the English language, which was created by Princeton, and is part of the NLTK corpus.
You can use WordNet alongside the NLTK module to find the meanings
of words, synonyms同义词, antonyms反义词, and more. Let's cover some examples.
First, you're going to need to import wordnet:
from nltk.corpus import wordnet
Then, we're going to use the term "program" to find synsets 同义词集合like so:
syns = wordnet.synsets("program")
An example of a synset:
print(syns[0].name())
plan.n.01
Just the word: 只显示单词
print(syns[0].lemmas()[0].name())
plan
Definition of that first synset:
print(syns[0].definition())
a series of steps to be carried out or goals to be accomplished
Examples of the word in use:
print(syns[0].examples())
['they drew up a six-step plan', 'they discussed plans for a new bond issue']
Next, how might we discern synonyms and antonyms to a word? The lemmas will be synonyms, and then you can use .antonyms to find the antonyms to the lemmas. As such, we can populate some lists like:
synonyms = []
antonyms = [] for syn in wordnet.synsets("good"):
for l in syn.lemmas():
synonyms.append(l.name())
if l.antonyms():
antonyms.append(l.antonyms()[0].name()) print(set(synonyms))
print(set(antonyms))
As you can see, we got many more synonyms than antonyms, since we just looked up the antonym for the first lemma, but you could easily balance this buy also doing the exact same process for the term "bad."
比较单词近似度
Next, we can also easily use WordNet to compare the similarity of two words and their tenses, by incorporating the Wu and Palmer method for semantic related-ness.
Let's compare the noun of "ship" and "boat:"
w1 = wordnet.synset('ship.n.01')
w2 = wordnet.synset('boat.n.01')
print(w1.wup_similarity(w2))
0.9090909090909091
w1 = wordnet.synset('ship.n.01')
w2 = wordnet.synset('car.n.01')
print(w1.wup_similarity(w2))
0.6956521739130435
w1 = wordnet.synset('ship.n.01')
w2 = wordnet.synset('cat.n.01')
print(w1.wup_similarity(w2))
0.38095238095238093
Next, we're going to pick things up a bit and begin to cover the topic of Text Classification.
自然语言22_Wordnet with NLTK的更多相关文章
- 转 --自然语言工具包(NLTK)小结
原作者:http://www.cnblogs.com/I-Tegulia/category/706685.html 1.自然语言工具包(NLTK) NLTK 创建于2001 年,最初是宾州大学计算机与 ...
- 自然语言17_Chinking with NLTK
https://www.pythonprogramming.net/chinking-nltk-tutorial/?completed=/chunking-nltk-tutorial/ 代码 # -* ...
- 自然语言16_Chunking with NLTK
Chunking with NLTK 对chunk分类数据结构可以图形化输出,用于分析英语句子主干结构 # -*- coding: utf-8 -*-"""Created ...
- Python自然语言处理工具NLTK的安装FAQ
1 下载Python 首先去python的主页下载一个python版本http://www.python.org/,一路next下去,安装完毕即可 2 下载nltk包 下载地址:http://www. ...
- Python自然语言工具包(NLTK)入门
在本期文章中,小生向您介绍了自然语言工具包(Natural Language Toolkit),它是一个将学术语言技术应用于文本数据集的 Python 库.称为“文本处理”的程序设计是其基本功能:更深 ...
- Python NLTK 自然语言处理入门与例程(转)
转 https://blog.csdn.net/hzp666/article/details/79373720 Python NLTK 自然语言处理入门与例程 在这篇文章中,我们将基于 Pyt ...
- NLTK在自然语言处理
nltk-data.zip 本文主要是总结最近学习的论文.书籍相关知识,主要是Natural Language Pracessing(自然语言处理,简称NLP)和Python挖掘维基百科Infobox ...
- Python自然语言处理工具小结
Python自然语言处理工具小结 作者:白宁超 2016年11月21日21:45:26 目录 [Python NLP]干货!详述Python NLTK下如何使用stanford NLP工具包(1) [ ...
- 自然语言处理(NLP)入门学习资源清单
Melanie Tosik目前就职于旅游搜索公司WayBlazer,她的工作内容是通过自然语言请求来生产个性化旅游推荐路线.回顾她的学习历程,她为期望入门自然语言处理的初学者列出了一份学习资源清单. ...
随机推荐
- void与void之间没有隐式转换(纯属恶搞,请勿在意)
强大的vs弹出了这个提示:.有没有觉得强大的vs不应该出现该提示. 但就是出现了. 看客,您知道怎么让vs弹出这个提示吗^~^
- Java--剑指offer(7)
31.求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.AC ...
- [转]Spring 注解总结
原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...
- Shell命令_Cron使用
chkconfig crond on d表示damon,后台进程 chkconfig --list | grep crond crontab [选项] 选项: -e: 编辑crontab定时任务 -l ...
- 【codevs 1565】【SDOI 2011】计算器 快速幂+拓展欧几里得+BSGS算法
BSGS算法是meet in the middle思想的一种应用,参考Yveh的博客我学会了BSGS的模版和hash表模板,,, 现在才会hash是不是太弱了,,, #include<cmath ...
- 数据库开发基础-SQl Server 聚合函数、数学函数、字符串函数、时间日期函数
SQL 拥有很多可用于计数和计算的内建函数. 函数的语法 内建 SQL 函数的语法是: SELECT function(列) FROM 表 函数的类型 在 SQL 中,基本的函数类型和种类有若干种.函 ...
- MySQL的Sleep进程
php的垃圾回收机制,其实只针对于php本身. 对于mysql,php没权利去自动去释放它的东西. 如果你在页面执行完毕前不调用mysql_close(),那么mysql那边是不会关闭这个连接的. 如 ...
- Sublime Text 3 python和Package Control配置方法
(如果下面的方法试了Packages control功能还是不能用参考这个方法: 1.直接把C:\Sublime Text 3x64\Data\Packages\ 目录下原有的Packages c ...
- Xcode 修改工程名以及注意事项
1.先把整个工程文件夹名改为新的工程名. 2.打开工程,单击,输入新的工程名,会出现,点击确定. 3.回到工程界面,在中选择 Manage Schemes,然后再弹出的对话框,把工程名改为新的名字. ...
- cmd执行sql文件
string infile = @"C:\Users\yudm\Desktop\test\Patch.sql"; Process sqlprocess = new Process( ...