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))
{'beneficial', 'just', 'upright', 'thoroughly', 'in_force', 'well', 'skilful', 'skillful', 'sound', 'unspoiled', 'expert', 'proficient', 'in_effect', 'honorable', 'adept', 'secure', 'commodity', 'estimable', 'soundly', 'right', 'respectable', 'good', 'serious', 'ripe', 'salutary', 'dear', 'practiced', 'goodness', 'safe', 'effective', 'unspoilt', 'dependable', 'undecomposed', 'honest', 'full', 'near', 'trade_good'} {'evil', 'evilness', 'bad', 'badness', 'ill'}

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的更多相关文章

  1. 转 --自然语言工具包(NLTK)小结

    原作者:http://www.cnblogs.com/I-Tegulia/category/706685.html 1.自然语言工具包(NLTK) NLTK 创建于2001 年,最初是宾州大学计算机与 ...

  2. 自然语言17_Chinking with NLTK

    https://www.pythonprogramming.net/chinking-nltk-tutorial/?completed=/chunking-nltk-tutorial/ 代码 # -* ...

  3. 自然语言16_Chunking with NLTK

    Chunking with NLTK 对chunk分类数据结构可以图形化输出,用于分析英语句子主干结构 # -*- coding: utf-8 -*-"""Created ...

  4. Python自然语言处理工具NLTK的安装FAQ

    1 下载Python 首先去python的主页下载一个python版本http://www.python.org/,一路next下去,安装完毕即可 2 下载nltk包 下载地址:http://www. ...

  5. Python自然语言工具包(NLTK)入门

    在本期文章中,小生向您介绍了自然语言工具包(Natural Language Toolkit),它是一个将学术语言技术应用于文本数据集的 Python 库.称为“文本处理”的程序设计是其基本功能:更深 ...

  6. Python NLTK 自然语言处理入门与例程(转)

    转 https://blog.csdn.net/hzp666/article/details/79373720     Python NLTK 自然语言处理入门与例程 在这篇文章中,我们将基于 Pyt ...

  7. NLTK在自然语言处理

    nltk-data.zip 本文主要是总结最近学习的论文.书籍相关知识,主要是Natural Language Pracessing(自然语言处理,简称NLP)和Python挖掘维基百科Infobox ...

  8. Python自然语言处理工具小结

    Python自然语言处理工具小结 作者:白宁超 2016年11月21日21:45:26 目录 [Python NLP]干货!详述Python NLTK下如何使用stanford NLP工具包(1) [ ...

  9. 自然语言处理(NLP)入门学习资源清单

    Melanie Tosik目前就职于旅游搜索公司WayBlazer,她的工作内容是通过自然语言请求来生产个性化旅游推荐路线.回顾她的学习历程,她为期望入门自然语言处理的初学者列出了一份学习资源清单. ...

随机推荐

  1. 简单的解释XSS攻击

    XSS 跨站点脚本 cross site script 怎么造成攻击? 举例:有一个公共的页面,所有用户都可以访问且可以保存内容,输入的时候若输入<script>alert('I am h ...

  2. [cross domain] four approachs to cross domain in javascript

    four approachs can cross domain in javascript 1.jsonp 2.document.domain(only in frame and they have ...

  3. Java算法-冒泡排序

    冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成.这个算法的名字 ...

  4. dede去掉当前位置position后面的箭头

    理论是,dede的当前位置标签{dedefield name='position'},结构是 "首页 > 主栏目 > 子栏目 > ",箭头符号字段数据都是在后台设 ...

  5. 在浏览器中输入URL后执行的全部过程的个人总结

    这个问题经常可以看到,今天我好好总结了下,是从网络模型的角度来分析问题的,主要涉及应用层:DNS,HTTP,传输层:TCP,网络层:IP和路由选择协议:RIP,OSPF(内部网关协议),BGP(外部网 ...

  6. JSon 事件格式化

    JS~json日期格式化   起因 对于从C#返回的日期字段,当进行JSON序列化后,在前台JS里显示的并不是真正的日期,需要格式化时间 实现 function ChangeDateFormat(js ...

  7. iOS 蓝牙开发(三)app作为外设被连接的实现(转)

    转载自:www.cocoachina.com/ios/20151105/14071.html 原作者:刘彦玮 再上一节说了app作为central连接peripheral的情况,这一节介绍如何使用ap ...

  8. windows下关闭进程

    1.首先查找到占用8080端口的进程号PID是多少 CMD>netstat -ano | findstr 8888 这个命令输出的最后一列表示占用8080端口的进程号是多少,这里是5880 2. ...

  9. 77.Android之代码混淆

    转载:http://www.jianshu.com/p/7436a1a32891 简介 作为Android开发者,如果你不想开源你的应用,那么在应用发布前,就需要对代码进行混淆处理,从而让我们代码即使 ...

  10. java高新技术-反射

    一.反射的基石->Class类 定义一个类使用 class 有一个类叫Class Java程序中的各个Java类属于同一类事务,描述这类事物的Java类名就是Class. Person类代表人, ...