Hướng dẫn dùng phrase synonym python

What is Wordnet?

Wordnet is an NLTK corpus reader, a lexical database for English. It can be used to find the meaning of words, synonym or antonym. One can define it as a semantically oriented dictionary of English. It is imported with the following command:

Nội dung chính

  • What is Wordnet?
  • Text Mining — Extracting Synonyms and Antonyms
  • To find the meaning of the word
  • How do I get synonyms for NLP?
  • How do I find synonyms for a word?
  • Which key is used to find synonyms?

from nltk.corpus import wordnet as guru

Stats reveal that there are 155287 words and 117659 synonym sets included with English WordNet.

Different methods available with WordNet can be found by typing dir(guru)

[‘_LazyCorpusLoader__args’, ‘_LazyCorpusLoader__kwargs’, ‘_LazyCorpusLoader__load’, ‘_LazyCorpusLoader__name’, ‘_LazyCorpusLoader__reader_cls’, ‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattr__’, ‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__name__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘__unicode__’, ‘__weakref__’, ‘_unload’, ‘subdir’, ‘unicode_repr’]

Let us understand some of the features available with the wordnet:

Synset: It is also called as synonym set or collection of synonym words. Let us check a example

from nltk.corpus import wordnet
syns = wordnet.synsets("dog")
print(syns)

Output:

[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]

Lexical Relations: These are semantic relations which are reciprocated. If there is a relationship between {x1,x2,…xn} and {y1,y2,…yn} then there is also relation between {y1,y2,…yn} and {x1,x2,…xn}. For example Synonym is the opposite of antonym or hypernyms and hyponym are type of lexical concept.

Let us write a program using python to find synonym and antonym of word “active” using Wordnet.

from nltk.corpus import wordnet
	synonyms = []
	antonyms = []

	for syn in wordnet.synsets("active"):
		for l in syn.lemmas():
			synonyms.append(l.name())
			if l.antonyms():
				 antonyms.append(l.antonyms()[0].name())

	print(set(synonyms))
	print(set(antonyms))

The output of the code:

{‘dynamic’, ‘fighting’, ‘combat-ready’, ‘active_voice’, ‘active_agent’, ‘participating’, ‘alive’, ‘active’} — Synonym

{‘stative’, ‘passive’, ‘quiet’, ‘passive_voice’, ‘extinct’, ‘dormant’, ‘inactive’} — Antonym

Hướng dẫn dùng phrase synonym python

Explanation of the code

  1. Wordnet is a corpus, so it is imported from the ntlk.corpus
  2. List of both synonym and antonym is taken as empty which will be used for appending
  3. Synonyms of the word active are searched in the module synsets and are appended in the list synonyms. The same process is repeated for the second one.
  4. Output is printed

Conclusion:

WordNet is a lexical database that has been used by a major search engine. From the WordNet, information about a given word or phrase can be calculated such as

  • synonym (words having the same meaning)
  • hypernyms (The generic term used to designate a class of specifics (i.e., meal is a breakfast), hyponyms (rice is a meal)
  • holonyms (proteins, carbohydrates are part of meal)
  • meronyms (meal is part of daily food intake)

WordNet also provides information on co-ordinate terms, derivates, senses and more. It is used to find the similarities between any two words. It also holds information on the results of the related word. In short or nutshell one can treat it as Dictionary or Thesaurus. Going deeper in wordnet, it is divided into four total subnets such as

  1. Noun
  2. Verb
  3. Adjective
  4. Adverb

It can be used in the area of artificial intelligence for text analysis. With the help of Wordnet, you can create your corpus for spelling checking, language translation, Spam detection and many more.

In the same way, you can use this corpus and mold it to work some dynamic functionality. This is just like ready to made corpus for you. You can use it in your way.

Text Mining — Extracting Synonyms and Antonyms

Photo by micah boswell on Unsplash

Language analysis can be carried out in many ways. In this blog, we will see how to extract Synonyms and Antonyms from the text using Natural Language Processing(NLTK) WordNet library.

Source: Vincent Russo

The WordNet is a part of Python’s Natural Language Toolkit. It is a large collection of words and vocabulary from the English language that are related to each other and are grouped in some way. A collection of similar words is called lemmas. Also, It’s a combination of dictionary and thesaurus. It is used for automatic text analysis and artificial intelligence applications. It supports many other languages in its collection. Please check for more information about WordNet here

Nouns, verbs, adjectives, and adverbs are grouped into sets of cognitive synonyms (synsets), each expressing a distinct concept. Synsets are interlinked by means of conceptual-semantic and lexical relations. Let’s see some examples

To find the meaning of the word

Code

Import NLTK library and install Wordnet

import nltk
nltk.download('wordnet')

In this example, we will see how wordnet returns meaning and other details of the word. Let’s go ahead and look up the word “travel”

Sometimes, if some examples are available, it may also provide that.

#Checking the word "Teaching"syn = wordnet.synsets(“teaching”)
syn

Output

[Synset('teaching.n.01'),
Synset('teaching.n.02'),
Synset('education.n.01'),
Synset('teach.v.01'),
Synset('teach.v.02')]

We can see that “TEACHING” has five meanings. Let’s find the first sense to get a better understanding of the kind of information each synset contains. We can do this by indexing the first element at its name. n, v represents Parts of speech tagging.

Code

# Printing the Synonym, meaning and example of "teaching" for the first two indexes#First Indexprint(‘Word and Type : ‘ + syn[0].name())
print(‘Synonym of teaching is: ‘ + syn[0].lemmas()[0].name())
print(‘The meaning of the teaching: ‘ + syn[0].definition())
print(‘Example of teaching : ‘ + str(syn[0].examples()))
#Second Index
print(‘Word and Type : ‘ + syn[1].name())
print(‘Synonym of teaching is: ‘ + syn[1].lemmas()[0].name())
print(‘The meaning of the teaching : ‘ + syn[1].definition())
print(‘Example of teaching : ‘ + str(syn[1].examples()))

Output

# Output for first indexWord and Type : teaching.n.01
Synonym of Teaching is: teaching
The meaning of the Teaching: the profession of a teacher
Example of Teaching : ['he prepared for teaching while still in college', 'pedagogy is recognized as an important profession']
# Output for second indexWord and Type : teaching.n.02
Synonym of Teaching is: teaching
The meaning of the Teaching : a doctrine that is taught
Example of Teaching : ['the teachings of religion', 'he believed all the Christian precepts']

Synonyms

We can use lemmas() function of the synset. It returns synonyms as well as antonyms of that particular synset.

Code

#Checking synonym for the word "travel"
from nltk.corpus import wordnet
#Creating a list
synonyms = []
for syn in wordnet.synsets("travel"):
for lm in syn.lemmas():
synonyms.append(lm.name())#adding into synonyms
print (set(synonyms))

Output

{'trip', 'locomote', 'jaunt', 'change_of_location', 'go', 'traveling', 'travelling', 'locomotion', 'travel', 'move', 'journey', 'move_around'}

We can see the synonyms for the word ‘travel’ from the above output.

Antonyms

Code

#Checking antonym for the word "increase"
from nltk.corpus import wordnet
antonyms = []
for syn in wordnet.synsets("increase"):
for lm in syn.lemmas():
if lm.antonyms():
antonyms.append(lm.antonyms()[0].name()) #adding into antonyms
print(set(antonyms))

Output

{'decrement', 'decrease'}

WordNet has other feature called word similarity. It helps us check the similarity between two words that I didn’t cover in this blog.

Thanks for reading. Keep learning and stay tuned for more!

How do I get synonyms for NLP?

How to get synonyms of a particular word from wordnet in nlp.

Step 1 - Import the necessary libraries. from nltk.corpus import wordnet..

Step 2 - Find the Sysnsets of words. My_sysn = wordnet.synsets("fight") ... .

Step 3 - Print the result..

How do I find synonyms for a word?

Using the thesaurus, you can look up synonyms (different words with the same meaning) and antonyms (words with the opposite meaning). Tip: In the desktop versions of Word, PowerPoint, and Outlook, you can get a quick list of synonyms by right-clicking a word and choosing Synonyms.

Which key is used to find synonyms?

A thesaurus is a software tool included with some word processors that provides synonyms for selected words on command. Users using Microsoft Word can open a thesaurus by highlighting the word they want to look up and pressing the shortcut key Shift+F7.