自然语言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,她的工作内容是通过自然语言请求来生产个性化旅游推荐路线.回顾她的学习历程,她为期望入门自然语言处理的初学者列出了一份学习资源清单. ...
随机推荐
- MySQL的热备percona-xtrabackup、innobackupex的安装方法
http://blog.csdn.net/dbanote/article/details/13295727 http://blog.csdn.net/yangzhawen/article/detail ...
- Mac 下,配置SVN
Mac 环境下 sv 服务器的配置 本文目录 • 一.创建代码仓库,用来存储客户端所上传的代码 • 二.配置svn的用户权限 • 三.使用svn客户端功能 在Windows环境中,我们一般使用Tort ...
- 【线性规划与网络流 24题】已完成(3道题因为某些奇怪的原因被抛弃了QAQ)
写在前面:SDOI2016 Round1滚粗后蒟蒻开始做网络流来自我拯救(2016-04-11再过几天就要考先修课,现在做网络流24题貌似没什么用←退役节奏) 做的题目将附上日期,见证我龟速刷题. 1 ...
- 线段树单点更新poj 2828
n个人 他要插入的位置 和权值(这东西就最后输出来的) 倒的插就一定是他自己的位子 一个线段树维护一下就可以了 nlog(n) #include<stdio.h> #include< ...
- 小 div在大 div中左右上下居中
<!DOCTYPE HTML><html><head> <meta http-equiv="Content-Type" content=& ...
- iOS 蓝牙开发(四)BabyBluetooth蓝牙库介绍(转)
转载自:http://www.cocoachina.com/ios/20151106/14072.html 原文作者:刘彦玮 BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBlue ...
- 【转】eclipse导入V7包出现错误解决办法
android下v4 v7 v21等包是android系统的扩展支持包,就想windows的系统补丁一个道理. android的扩展包主要是用来兼容低版本的,比如android3.0以后出现 ...
- js-自制轮播插件!
刚接触轮播的时候,感觉这种东西好高端,后来学习了jquery后,发现原来挺简单的,而且实现轮播也有很多形式,我用jquery自制了一个轮播插件,其实我这个说是插件,好像其实就是一个高度抽象的函数而已. ...
- 【BZOJ-1096】仓库建设 斜率优化DP
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3719 Solved: 1633[Submit][Stat ...
- 正则表达式——语法
正则表达式(regular expression)--描述一种字符串匹配模式,可以用来检测一个字符串是否包含特定的子串.用其他字符串将其代替.提取出某个符合要求的子串. 正则表达式 由普通字符 和 ...

