Skip to content

tdfs4ds.agent.aggregate_question

tdfs4ds.agent.aggregate_question

Questions that ask for a number computed over data (0.3.7.34).

"by joining and the dataset dealing with , could you calculate the number of fibers with a length greater than 1 km?" is neither a dashboard (it asks for ONE figure, not a table) nor a lookup. With MCP enabled it goes to the MCP tools with the objects the question names, resolved here; without MCP the agent cannot run it, so the answer is the SQL to run, written by code from verified names only: the documented view the question names, the dataset built on the entity it names, the join columns both sides document, and a measure column chosen among the documented columns - flagged as an interpretation, with the unit assumption written out. A column is never invented.

Pure helpers + one reader of the business dictionary / dataset catalog (resolve_targets); everything is fail-open (an unreadable catalog gives no target, so the question keeps its usual routing).

_AGGREGATE_RE module-attribute

_AGGREGATE_RE = re.compile('\\bcalculat(?:e|ing)\\b|\\bcompute\\b|\\bhow\\s+many\\b|\\bnumber\\s+of\\b|\\bcount\\b|\\bsum\\s+of\\b|\\baverage\\s+of\\b|\\bavg\\b|\\bcalcul(?:e|er|ez)\\b|\\bcompt(?:e|er|ez)\\b|\\bcombien\\b|\\bnombre\\s+de\\b|\\bsomme\\s+(?:de|des)\\b|\\bmoyenne\\s+(?:de|des)\\b', re.IGNORECASE)

_CATALOG_COUNT_RE module-attribute

_CATALOG_COUNT_RE = re.compile('\\b(?:how\\s+many|number\\s+of|count|combien|nombre\\s+de)\\s+(?:\\w+\\s+){0,2}?(?:features?|datasets?|entit(?:y|ies)|data\\s+domains?|domains?|processes|process|tables?|views?|columns?|runs?|jeux?\\s+de\\s+donn[ée]es|entit[ée]s?|processus|colonnes?)\\b', re.IGNORECASE)

_OPERATORS module-attribute

_OPERATORS = ((re.compile('\\b(?:greater|more|larger|longer|bigger|higher|above|over|exceed\\w*|plus\\s+(?:grand|long)\\w*|sup[ée]rieur\\w*|au-dessus)\\b|>', re.I), '>'), (re.compile('\\b(?:less|lower|smaller|shorter|below|under|fewer|moins|inf[ée]rieur\\w*|au-dessous)\\b|<', re.I), '<'))

_OPERATOR_WORD_RE module-attribute

_OPERATOR_WORD_RE = re.compile('\\b(?:greater|more|larger|longer|bigger|higher|above|over|exceed\\w*|less|lower|smaller|shorter|below|under|fewer|sup[ée]rieur\\w*|inf[ée]rieur\\w*)\\b', re.I)

_QUANTITY_RE module-attribute

_QUANTITY_RE = re.compile('(?P<n>\\d+(?:[.,]\\d+)?)\\s*(?P<u>km|kilomet\\w*|kilom[èe]tres?|m|met\\w*|m[èe]tres?|cm|mm)?\\b', re.I)

_UNIT_FACTOR module-attribute

_UNIT_FACTOR = {'km': 1000.0, 'kilomet': 1000.0, 'kilom': 1000.0, 'm': 1.0, 'met': 1.0, 'cm': 0.01, 'mm': 0.001}

_GENERIC_WORDS module-attribute

_GENERIC_WORDS = frozenset({'calculate', 'compute', 'count', 'number', 'many', 'could', 'you', 'the', 'and', 'with', 'that', 'have', 'has', 'joining', 'join', 'joined', 'by', 'of', 'a', 'an', 'to', 'is', 'are', 'dealing', 'deal', 'dataset', 'datasets', 'view', 'table', 'greater', 'than', 'less', 'more', 'fewer', 'entity', 'from', 'for', 'in', 'on', 'please', 'can', 'what', 'which', 'whose', 'where', 'when', 'per', 'each', 'all', 'any', 'me', 'give', 'above', 'below', 'over', 'under', 'exceed', 'exceeds', 'lower', 'higher', 'larger', 'smaller', 'longer', 'shorter', 'bigger', 'using', 'use', 'between', 'total', 'id', 'ids'})

is_aggregate_question

is_aggregate_question(question: str) -> bool

The question asks for a figure computed over data, not for the catalog's content.

_ca

_ca()

_words

_words(text: str) -> list

_stem

_stem(w: str) -> str

_name_words

_name_words(name: str) -> set

entities_named

entities_named(question: str, candidates: list) -> list

The entity (columns joined by commas) whose EVERY column the question writes; the widest wins. candidates is [{entity, domain, ...}]. Pure.

comparison

comparison(question: str) -> Optional[dict]

{op, value, unit, sql_value, assumption} of "greater than 1 km", else None. Pure.

measure_word

measure_word(question: str) -> list

The words naming what is compared: those just before the comparison word ("a fiber length greater than ..." -> ['length', 'fiber']), most specific first.

rank_measure_columns

rank_measure_columns(columns: list, words: list) -> list

columns = [{name, description, source}] -> those matching the measure words, best first: a word in the column's name beats one in its description; the first (most specific) word weighs most. Pure.

_documented_views

_documented_views(question: str) -> list

Documented tables / views (not datasets, process views, FS_ objects) the question names.

_columns_of

_columns_of(db: str, name: str) -> list

resolve_targets

resolve_targets(question: str) -> dict

{views: [...], entity: {...}|None, datasets: [...], sample} of what an aggregate question names. A dataset is kept when its entity is the one named (or it is named itself); among several of that entity, the ones sharing most words with the question come first. Fail-open.

has_targets

has_targets(t: dict) -> bool

is_aggregate_data_question

is_aggregate_data_question(question: str) -> bool

An aggregate question that names a documented view, a dataset or an entity. Reads the catalogs.

mcp_hint

mcp_hint(question: str) -> str

The objects to give the MCP tools, as one sentence ('' when none): the views and datasets are database-qualified, the entity key says which dataset is meant.

_fq

_fq(db, name)

aggregate_answer

aggregate_answer(question: str, targets: Optional[dict] = None) -> Optional[str]

The MCP-less answer: the SQL to run, from verified names. None when nothing was resolved.