liveBook · Manning

Semantic parsing

As explained in AI-Powered Search

Semantic query parsing is the analysis of a query to recover its meaning, entities, relationships, constraints, and intended operation.

A search for “top kimchi near charlotte” asks for more than documents containing those words. Semantic parsing identifies known entities and interprets connecting words so the search engine can use popularity and location, rather than treating every term as a keyword. section 7.4.1 section 7.5

A domain-specific parser can turn the query into a structure the search pipeline can use:

  • Build a lookup collection of known phrases, alternative spellings, entities, and semantic functions. An FST-backed Solr text tagger supports fast extraction from a large vocabulary. section 7.4.1
  • Tag matching spans, then map each surface form to a canonical entity or function. Keep unmatched text as keywords; when a span has multiple candidates, the example parser selects the one with the highest popularity value. section 7.4.1
  • Use the parsed entities to enrich and transform the query for the target search engine, while retaining a fallback for unknown keywords. section 7.5

From words to a typed query

For “top kimchi near charlotte,” extraction tags “top,” “near,” and “charlotte”; “kimchi” remains a keyword. The resulting tree assigns “top” a popularity function, “near” a location-distance function, and “Charlotte” a city entity with coordinates. This gives later query stages typed inputs instead of four undifferentiated strings. section 7.4.1

Listing 7.8 Generating a typed query tree from a user query
def generate_query_tree(extracted_entities):
  query = extracted_entities["query"]
  entities = {entity["id"]: entity for entity  #1
              in extracted_entities["entities"]}  #1
  query_tree = []
  last_end = 0

  for tag in extracted_entities["tags"]:
    best_entity = entities[tag["ids"][0]]  #2
    for entity_id in tag["ids"]:  #2
      if (entities[entity_id]["popularity"] >  #2
          best_entity["popularity"]):  #2
        best_entity = entities[entity_id] #2

    next_text = query[last_end:tag["startOffset"]].strip()
    if next_text:
      query_tree.append({"type": "keyword",  #3
                         "surface_form": next_text,  #3
                         "canonical_form": next_text})  #3
    query_tree.append(best_entity)  #4
    last_end = tag["endOffset"] 

  if last_end < len(query):  #5
    final_text = query[last_end:len(query)].strip()  #5
    if final_text:  #5
      query_tree.append({"type": "keyword",  #5
                         "surface_form": final_text,  #5
                         "canonical_form": final_text})  #5
  return query_tree

parsed_query = generate_query_tree(query_entities)
display(parsed_query)

The Python listing shows how tagged spans and intervening keywords become a typed query tree, including popularity-based selection among candidate entities. section 7.4.1

Explicit parsing offers control over domain vocabulary and words such as “top” and “near,” but it depends on what the entity collection knows. Unlisted phrases are harder to interpret, and a popularity-based choice between meanings is only a selection rule—not proof that the user intended that meaning. section 7.4.1 section 7.5

How AI-Powered Search covers it

How deep it goes
Works through entity extraction and typed-query construction, but defers the internal workings of finite state transducers.
Examples
Uses “top kimchi near charlotte” to show tagged spans, an unmatched keyword, competing meanings, and a typed query tree.
What it assumes
Assumes a vocabulary of domain-specific phrases and introduces extraction from it, without teaching FST internals.
What you can do after
Build it from scratch as an entity-backed query parser that produces typed inputs for a search pipeline.
How it connects
Connects entity extraction, knowledge graphs, query interpretation, semantic functions, and keyword fallback.

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.