WordNet: Search Synsets

[1] Find word association, return associated synsets, part-of-speech and sense number

import nltk
from nltk.corpus import wordnet

# Ensure you have the WordNet data
nltk.download('wordnet')

def find_associations(word):
    synsets = wn.synsets(word)
    associations = {
        'synonyms': set(),
        'hypernyms': set(),
        'hyponyms': set(),
        'similar_words': set()
    }

    for synset in synsets:
        # Get the formatted synset name (e.g., 'trout.n.02')
        synset_name = synset.name()

        # Get synonyms
        synonyms = synset.lemma_names()
        for synonym in synonyms:
            associations['synonyms'].add(f"{synonym} ({synset_name})")

        # Get hypernyms
        for hypernym in synset.hypernyms():
            hypernym_name = hypernym.name()
            associations['hypernyms'].add(f"{hypernym_name} ({hypernym_name})")

        # Get hyponyms
        for hyponym in synset.hyponyms():
            hyponym_name = hyponym.name()
            associations['hyponyms'].add(f"{hyponym_name} ({hyponym_name})")

        # Get similar words
        for similar in synset.similar_tos():
            similar_name = similar.name()
            associations['similar_words'].add(f"{similar_name} ({similar_name})")

    return associations

# Find associations for "positive"
positive_associations = find_associations('positive')
print("Associations for 'positive':")
for relation_type, words in positive_associations.items():
    print(f"{relation_type.capitalize()}: {list(words)}")

# Find associations for "trust"
trust_associations = find_associations('confident')
print("\nAssociations for 'confident':")
for relation_type, words in trust_associations.items():
    print(f"{relation_type.capitalize()}: {list(words)}")

output:

Associations for 'positive':
Synonyms: ['positive (positive.a.08)', 'irrefutable (incontrovertible.s.01)', 'prescribed (positive.s.05)', 'positively_charged (positive.s.10)', 'electropositive (positive.s.10)', 'confirming (positive.a.04)', 'positive (cocksure.s.01)', 'positive (positive.s.05)', 'positive (positive.n.01)', 'overconfident (cocksure.s.01)', 'plus (plus.s.02)', 'confident (convinced.s.01)', 'positive (positive.s.10)', 'positivistic (positivist.a.01)', 'positivist (positivist.a.01)', 'positive (incontrovertible.s.01)', 'cocksure (cocksure.s.01)', 'positive (positivist.a.01)', 'positive_degree (positive.n.01)', 'positive (plus.s.02)', 'positive (positive.a.01)', 'positive (convinced.s.01)', 'positive (positive.a.04)', 'convinced (convinced.s.01)', 'positive (positive.n.02)', 'incontrovertible (incontrovertible.s.01)', 'positive (positive.s.09)']
Hypernyms: ['film.n.03 (film.n.03)', 'adverb.n.02 (adverb.n.02)', 'adjective.n.01 (adjective.n.01)']
Hyponyms: []
Similar_words: ['advantageous.a.01 (advantageous.a.01)', 'formal.a.01 (formal.a.01)', 'certain.a.02 (certain.a.02)', 'affirmative.s.02 (affirmative.s.02)', 'plus.a.01 (plus.a.01)', 'gram-positive.s.01 (gram-positive.s.01)', 'constructive.s.02 (constructive.s.02)', 'confident.a.01 (confident.a.01)', 'charged.a.01 (charged.a.01)', 'undeniable.a.01 (undeniable.a.01)']

Associations for 'confident':
Synonyms: ['surefooted (confident.s.03)', 'confident (confident.a.01)', 'positive (convinced.s.01)', 'convinced (convinced.s.01)', 'confident (convinced.s.01)', 'confident (confident.s.03)', 'sure-footed (confident.s.03)']
Hypernyms: []
Hyponyms: []
Similar_words: ['reassured.s.01 (reassured.s.01)', 'certain.a.02 (certain.a.02)', 'capable.a.01 (capable.a.01)', 'self-assured.s.01 (self-assured.s.01)', 'cocksure.s.01 (cocksure.s.01)', 'assured.s.01 (assured.s.01)']

Breakdown of the Output

e.g. Synonyms: ['positive (positive.a.08)', 'irrefutable (incontrovertible.s.01)', 'prescribed (positive.s.05)', 'positively_charged (positive.s.10)', 'electropositive (positive.s.10)', 'confirming (positive.a.04)', 'positive (cocksure.s.01)', 'positive (positive.s.05)', 'positive (positive.n.01)', 'overconfident (cocksure.s.01)', 'plus (plus.s.02)', 'confident (convinced.s.01)', 'positive (positive.s.10)', 'positivistic (positivist.a.01)', 'positivist (positivist.a.01)', 'positive (incontrovertible.s.01)', 'cocksure (cocksure.s.01)', 'positive (positivist.a.01)', 'positive_degree (positive.n.01)', 'positive (plus.s.02)', 'positive (positive.a.01)', 'positive (convinced.s.01)', 'positive (positive.a.04)', 'convinced (convinced.s.01)', 'positive (positive.n.02)', 'incontrovertible (incontrovertible.s.01)', 'positive (positive.s.09)']

  1. General Format:

    • Each entry in the list has the format word (synset_name), where:

      • word is the synonym.

      • synset_name indicates the synset to which the word belongs, with:

        • The first part (e.g., positive) being the lemma (the base form of the word).

        • The second part (e.g., a for adjective, n for noun) indicating the part of speech.

        • The last part (e.g., 01, 02) indicating the sense number, which distinguishes between different meanings of the same word.

  2. Example Entries:

    • 'positive (positive.a.08)':

      • This means that "positive" is an adjective (indicated by a) and belongs to the eighth sense of the word "positive" in WordNet.
    • 'irrefutable (incontrovertible.s.01)':

      • "Irrefutable" is a synonym for "positive" and is associated with the first sense of the adjective "incontrovertible" (s indicating it's an adjective).
    • 'prescribed (positive.s.05)':

      • "Prescribed" is a synonym that is associated with the fifth sense of "positive."
    • 'confident (convinced.s.01)':

      • "Confident" is synonymous with "positive" and is linked to the first sense of "convinced."
  3. Multiple Entries:

    • Some synonyms appear multiple times with different senses. For instance, "positive" itself appears several times with different sense numbers (positive.a.01, positive.s.05, etc.), indicating that it has multiple meanings or usages in different contexts.
  4. Diverse Synonyms:

    • The list includes a mix of direct synonyms (words that can often replace "positive" in a sentence) and related terms that capture similar meanings or connotations but may not be interchangeable in all contexts.

[2] Find word association, return associated synsets

def find_associations(word):
    synsets = wn.synsets(word)
    associations = {
        'synonyms': set(),
        'hypernyms': set(),
        'hyponyms': set(),
        'similar_words': set()
    }

    for synset in synsets:
        # Get synonyms
        synonyms = synset.lemma_names()
        associations['synonyms'].update(synonyms)

        # Get hypernyms
        for hypernym in synset.hypernyms():
            associations['hypernyms'].update(hypernym.lemma_names())

        # Get hyponyms
        for hyponym in synset.hyponyms():
            associations['hyponyms'].update(hyponym.lemma_names())

        # Get similar words
        for similar in synset.similar_tos():
            associations['similar_words'].update(similar.lemma_names())

    return associations

# Find associations for "positive"
positive_associations = find_associations('positive')
print("Associations for 'positive':")
for relation_type, words in positive_associations.items():
    print(f"{relation_type.capitalize()}: {list(words)}")

# Find associations for "trust"
trust_associations = find_associations('confident')
print("\nAssociations for 'confident':")
for relation_type, words in trust_associations.items():
    print(f"{relation_type.capitalize()}: {list(words)}")

Output:

Associations for 'positive':
Synonyms: ['cocksure', 'irrefutable', 'prescribed', 'confident', 'positivistic', 'positive_degree', 'positivist', 'electropositive', 'incontrovertible', 'overconfident', 'plus', 'convinced', 'positive', 'positively_charged', 'confirming']
Hypernyms: ['adverb', 'photographic_film', 'adjective', 'film']
Hyponyms: []
Similar_words: ['plus', 'undeniable', 'affirmative', 'advantageous', 'sure', 'charged', 'optimistic', 'confident', 'certain', 'Gram-positive', 'constructive', 'formal']

Associations for 'confident':
Synonyms: ['sure-footed', 'confident', 'surefooted', 'convinced', 'positive']
Hypernyms: []
Hyponyms: []
Similar_words: ['cocksure', 'capable', 'assured', 'self-confident', 'sure', 'reassured', 'overconfident', 'self-assured', 'certain', 'positive']

[3] Part of Speech Codes in WordNet

  1. n = Noun

    • Represents a person, place, thing, or idea.

    • Example: dog.n.01 refers to the first sense of "dog" as a noun.

  2. v = Verb

    • Represents an action, occurrence, or state of being.

    • Example: run.v.01 refers to the first sense of "run" as a verb.

  3. a = Adjective

    • Describes or modifies a noun, providing more information about it.

    • Example: happy.a.01 refers to the first sense of "happy" as an adjective.

  4. s = Adjective Satellite

    • A type of adjective that provides additional descriptive information, often used in comparative forms.

    • Example: taller.s.01 refers to the comparative form of "tall."

  5. r = Adverb

    • Modifies verbs, adjectives, or other adverbs, often indicating manner, place, time, frequency, degree, etc.

    • Example: quickly.r.01 refers to the first sense of "quickly" as an adverb.

Explanation of Each Part of Speech

  • Noun (n): Nouns are fundamental to language, representing entities and concepts. They can be further categorized into concrete nouns (physical objects) and abstract nouns (ideas, qualities).

  • Verb (v): Verbs are crucial for expressing actions and states. They can indicate physical actions (like "run"), mental actions (like "think"), or states of being (like "exist").

  • Adjective (a): Adjectives enrich language by providing descriptive details about nouns. They help to convey qualities, quantities, or characteristics, enhancing the specificity of communication.

  • Adjective Satellite (s): Satellite adjectives often modify the meaning of another adjective or provide additional context. They are particularly useful when comparing or contrasting qualities.

  • Adverb (r): Adverbs add depth to sentences by modifying verbs, adjectives, or other adverbs. They can describe how, when, where, or to what extent an action occurs, thereby adding nuance to the action or description.