How to find synonyms in 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:

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

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.

Here are some helper functions to make NLTK easier to use, and two examples of how those functions can be used.

def download_nltk_dependencies_if_needed(): try: nltk.word_tokenize('foobar') except LookupError: nltk.download('punkt') try: nltk.pos_tag(nltk.word_tokenize('foobar')) except LookupError: nltk.download('averaged_perceptron_tagger') def get_some_word_synonyms(word): word = word.lower() synonyms = [] synsets = wordnet.synsets(word) if (len(synsets) == 0): return [] synset = synsets[0] lemma_names = synset.lemma_names() for lemma_name in lemma_names: lemma_name = lemma_name.lower().replace('_', ' ') if (lemma_name != word and lemma_name not in synonyms): synonyms.append(lemma_name) return synonyms def get_all_word_synonyms(word): word = word.lower() synonyms = [] synsets = wordnet.synsets(word) if (len(synsets) == 0): return [] for synset in synsets: lemma_names = synset.lemma_names() for lemma_name in lemma_names: lemma_name = lemma_name.lower().replace('_', ' ') if (lemma_name != word and lemma_name not in synonyms): synonyms.append(lemma_name) return synonyms

Example 1: get_some_word_synonyms

This approach tends to return the most relevant synonyms, but some words like "angry" won't return any synonyms.

download_nltk_dependencies_if_needed() words = ['dog', 'fire', 'erupted', 'throw', 'sweet', 'center', 'said', 'angry', 'iPhone', 'ThisIsNotARealWorddd', 'awesome', 'amazing', 'jim dandy', 'change'] for word in words: print('Synonyms for {}:'.format(word)) synonyms = get_some_word_synonyms(word) for synonym in synonyms: print(" {}".format(synonym))

Example 1 output:

Synonyms for dog: domestic dog canis familiaris Synonyms for fire: Synonyms for erupted: erupt break out Synonyms for throw: Synonyms for sweet: henry sweet Synonyms for center: centre middle heart eye Synonyms for said: state say tell Synonyms for angry: Synonyms for iPhone: Synonyms for ThisIsNotARealWorddd: Synonyms for awesome: amazing awe-inspiring awful awing Synonyms for amazing: amaze astonish astound Synonyms for jim dandy: Synonyms for change: alteration modification

Example 2: get_all_word_synonyms

This approach will return all possible synonyms, but some may not be very relevant.

download_nltk_dependencies_if_needed() words = ['dog', 'fire', 'erupted', 'throw', 'sweet', 'center', 'said', 'angry', 'iPhone', 'ThisIsNotARealWorddd', 'awesome', 'amazing', 'jim dandy', 'change'] for word in words: print('Synonyms for {}:'.format(word)) synonyms = get_some_word_synonyms(word) for synonym in synonyms: print(" {}".format(synonym))

Example 2 output:

Synonyms for dog: domestic dog canis familiaris frump cad bounder blackguard hound heel frank frankfurter hotdog hot dog wiener wienerwurst weenie pawl detent click andiron firedog dog-iron chase chase after trail tail tag give chase go after track Synonyms for fire: firing flame flaming ardor ardour fervor fervour fervency fervidness attack flak flack blast open fire discharge displace give notice can dismiss give the axe send away sack force out give the sack terminate go off arouse elicit enkindle kindle evoke raise provoke burn burn down fuel Synonyms for erupted: erupt break out irrupt flare up flare break open burst out ignite catch fire take fire combust conflagrate come out break through push through belch extravasate break burst recrudesce Synonyms for throw: stroke cam stroke shed cast cast off shake off throw off throw away drop thrust give flip switch project contrive bewilder bemuse discombobulate hurl hold have make confuse fox befuddle fuddle bedevil confound Synonyms for sweet: henry sweet dessert afters confection sweetness sugariness angelic angelical cherubic seraphic dulcet honeyed mellifluous mellisonant gratifying odoriferous odorous perfumed scented sweet-scented sweet-smelling fresh unfermented sugared sweetened sweet-flavored sweetly Synonyms for center: centre middle heart eye center field centerfield midpoint kernel substance core essence gist heart and soul inwardness marrow meat nub pith sum nitty-gritty center of attention centre of attention nerve center nerve centre snapper plaza mall shopping mall shopping center shopping centre focus on center on revolve around revolve about concentrate on concentrate focus pore rivet halfway midway Synonyms for said: state say tell allege aver suppose read order enjoin pronounce articulate enounce sound out enunciate aforesaid aforementioned Synonyms for angry: furious raging tempestuous wild Synonyms for iPhone: Synonyms for ThisIsNotARealWorddd: Synonyms for awesome: amazing awe-inspiring awful awing Synonyms for amazing: amaze astonish astound perplex vex stick get puzzle mystify baffle beat pose bewilder flummox stupefy nonplus gravel dumbfound astonishing awe-inspiring awesome awful awing Synonyms for jim dandy: Synonyms for change: alteration modification variety alter modify vary switch shift exchange commute convert interchange transfer deepen

How do you identify synonyms?

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.

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 you create a synonym in Python?

How to Create a Synonym for a Python Function.
Right-click PYTHON on the list of configured adapters, and click Create metadata objects on the context menu. ... .
Enter or select values for the following parameters. ... .
Click the Create Synonym button on the ribbon..

What is a synonym for Python?

What is another word for python?.

Chủ đề