liveBook · Manning

Semantic similarity

As explained in AI-Powered Search

Semantic textual similarity is the task or measure of determining how closely two text segments correspond in meaning.

Semantic similarity measures how closely two terms, phrases, documents, or queries correspond in meaning. In a search system, it estimates relatedness from the documents in which terms co-occur, following the distributional hypothesis: terms sharing unusually similar contexts tend to be semantically related. section 5.4.4

A practical relatedness calculation follows this reasoning:

  • Collect the documents matching a foreground term or query and compare them with documents matching another term. section 5.4.4
  • Reward terms that co-occur in the same documents more often than with random terms. section 5.4.4
  • Discount common stop words: frequent terms such as “the” and “a” share contexts broadly but are not treated as meaningfully similar. section 5.4.4
  • Use the resulting scores to expand queries, rank related nodes, or select terms that best represent a document’s topic. section 5.4.4 section 5.4.6

From “advil” to related health terms

A health knowledge graph can traverse from “advil” to terms occurring in similar documents. Example scores rank “motrin” at 0.70986, “aleve” at 0.4662, and “ibuprofen” at 0.38264, making the strongest relationships available for search or graph use. section 5.4.4

Content-based recommendation

For a document classified as “star wars,” score its extracted phrases against that topic. Terms such as “luke,” “force,” and “darth vader” score positively, while unrelated terms such as “banana” and “wolverine” score negatively. Keep terms above 0.25, weight them in an expanded query, and search for the five most similar documents. section 5.4.6

Listing 5.4 Discovering semantically related terms to advil
health_skg = get_skg(engine.get_collection("health"))

nodes_to_traverse = [{"field": "body",  #1
                      "values": ["advil"]},  #2
                     {"field": "body",
                      "min_occurrences": 2,  #3
                      "limit": 8}]  #4

traversal = health_skg.traverse(*nodes_to_traverse)  #5
print_graph(traversal, "advil") #6

Inspect the Python knowledge-graph traversal that discovers terms related to “advil” and limits the returned candidates. section 5.4.4

Listing 5.9 Generating a recommendation query from scored phrases
def get_scored_terms(traversal):
  return {term: data["relatedness"]
          for term, data in traversal["graph"][0]["values"]["star wars"] \
                                ["traversals"][0]["values"].items()}

rec_query = " ".join(f'"{term}"^{score}'
                     for term, score in get_scored_terms(traversal).items()
                     if score > 0.25)

print(f"Expanded Query:\n{rec_query}")

Inspect the query-generation step that converts positively scored phrases into weighted search terms. section 5.4.6

How AI-Powered Search covers it

How deep it goes
Explains distributional reasoning, stop-word effects, document-set comparison, scoring, traversal, and query construction without deriving the full equation.
Examples
Demonstrates health-term discovery from “advil” and topic-based recommendations from a “star wars” document.
What it assumes
Builds from the distributional hypothesis and document co-occurrence while assuming familiarity with terms, queries, and searchable collections.
What you can do after
Apply it via a knowledge-graph traversal and search API to expand queries and recommend contextually similar documents.
How it connects
Connects semantic similarity with the distributional hypothesis, knowledge graphs, query expansion, search ranking, and content-based recommendations.

Also explained in

Compare how 5 books explain semantic similarity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Search. The section links point to the parts of the book it draws on.