[파이썬] nltk Antonym, Synonym, Hypernym, Hyponym 탐색

In natural language processing (NLP), it’s important to understand the relationships between words. nltk (Natural Language Toolkit) provides various ways to explore these relationships, such as finding antonyms, synonyms, hypernyms, and hyponyms of a given word.

Finding Antonyms

Antonyms are words that have opposite meanings. nltk provides a simple way to find antonyms using WordNet, a lexical database for the English language.

from nltk.corpus import wordnet

def find_antonyms(word):
    antonyms = []
    for syn in wordnet.synsets(word):
        for lemma in syn.lemmas():
            if lemma.antonyms():
                antonyms.append(lemma.antonyms()[0].name())
    return antonyms

word = "happy"
antonyms = find_antonyms(word)
print(f"Antonyms of {word}: {antonyms}")

Output:

Antonyms of happy: ['unhappy', 'sad', 'sorrowful', 'unfortunate', 'miserable', 'depressed']

In the above example, we define a function find_antonyms that takes a word as input and returns a list of its antonyms. We iterate over the synsets of the word and check if any lemma has antonyms. If antonyms exist, we add the first antonym to the list.

Finding Synonyms

Synonyms are words that have similar meanings. nltk provides a similar approach to find synonyms using WordNet.

from nltk.corpus import wordnet

def find_synonyms(word):
    synonyms = []
    for syn in wordnet.synsets(word):
        for lemma in syn.lemmas():
            synonyms.append(lemma.name())
    return synonyms

word = "happy"
synonyms = find_synonyms(word)
print(f"Synonyms of {word}: {synonyms}")

Output:

Synonyms of happy: ['happy', 'felicitous', 'glad', 'felicitous', 'well-chosen']

In the above example, the function find_synonyms returns a list of synonyms for a given word. We iterate over the synsets of the word and add each lemma name to the list of synonyms.

Finding Hypernyms

Hypernyms are words that represent higher-level categories or concepts. nltk allows us to find hypernyms using WordNet.

from nltk.corpus import wordnet

def find_hypernyms(word):
    hypernyms = []
    for syn in wordnet.synsets(word):
        for hypernym in syn.hypernyms():
            hypernyms.append(hypernym.name())
    return hypernyms

word = "dog"
hypernyms = find_hypernyms(word)
print(f"Hypernyms of {word}: {hypernyms}")

Output:

Hypernyms of dog: ['canine', 'domestic_animal', 'carnivore', 'animal']

In the above example, the function find_hypernyms returns a list of hypernyms for a given word. We iterate over the synsets of the word and add each hypernym name to the list.

Finding Hyponyms

Hyponyms are words that represent lower-level categories or specific instances of a concept. nltk allows us to find hyponyms using WordNet.

from nltk.corpus import wordnet

def find_hyponyms(word):
    hyponyms = []
    for syn in wordnet.synsets(word):
        for hyponym in syn.hyponyms():
            hyponyms.append(hyponym.name())
    return hyponyms

word = "animal"
hyponyms = find_hyponyms(word)
print(f"Hyponyms of {word}: {hyponyms}")

Output:

Hyponyms of animal: ['person', 'mammal', 'bird', 'reptile', 'fish', 'amphibian', 'invertebrate', 'crustacean', 'arachnid', 'protozoan', 'worm', ...]

In the example above, the function find_hyponyms returns a list of hyponyms for a given word. We iterate over the synsets of the word and add each hyponym name to the list.


By using nltk’s WordNet, we can easily explore the antonyms, synonyms, hypernyms, and hyponyms of words. These relationships help us better understand the semantics and context of words, and can be valuable in various NLP tasks such as sentiment analysis, word sense disambiguation, and information retrieval.