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. 关于#pragma once和#ifndefine组合的区别

    最近在看duilib代码,发现头文件既有#pragma once 又有 #ifndefine...#define,忽然就觉得有点不解,因为据我所知这两者都是防止头文件二次包含的. 经过下面两位的解释后 ...

  2. [转]响应式WEB设计学习(2)—视频能够做成响应式吗

    原文地址:http://www.jb51.net/web/70361.html 上集回顾: 昨天讲了页面如何根据不同的设备尺寸做出响应.主要是利用了@media命令以及尺寸百分比化这两招. 上集补充: ...

  3. Shell命令_正则表达式

    正则表达式是包含匹配,通配符是完全匹配 基础正则表达式 test.txt示例文件 1 2 3 4 5 6 7 8 9 10 11 12 Mr. James said: he was the hones ...

  4. selenium3各种报错解决办法

    解决办法全在这个链接里 http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3

  5. MySQL忘记密码的解决方法

    1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysqld --skip-grant-tables回车. 4.原窗口已经不动了,再开一个DOS窗口,转到my ...

  6. 【CodeForces 699D】Fix a Tree

    dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1 ...

  7. while 循环 于 for循环语句

    while 循环 while 循环 while 条件: 如果条件是真的,就继续的循环 如果条件是假的,就停止循环 循环的意思就是让程序重复地执行某些语句,whiler循环就是循环结构的一种,当事先不知 ...

  8. c# base64 编码解码

    一. Base64的编码规则 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编 码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24 位数据,再把这24位数 ...

  9. linux中sh基本语法

    介绍:1 开头程序必须以下面的行开始(必须方在文件的第一行):#!/bin/sh  有人说是bash符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序 ...

  10. springMVC-自定义数据类型转换器

    自定义类型转换器 201603005,今天想了一个问题,Spring中的Conventer是如何实现的,因为他没有绑定类中的属性,它怎么知道要将那个String转换?看了几遍的书也没有找到,后来想想, ...