foley.index.taxonomy.resolver#

Resolve free tags/caption/labels to a UCS CatID (the EnvSound-UCS pipeline).

Staged precedence, highest wins (report 04 §5.3, [11 §3.1] — normalize heterogeneous labels onto UCS via mapping/synonym tables):

filename-parse > declared CatID > keyword/synonym > audioset-map > none

A declared CatID is one the file states about itself (the iXML CATID of a pro pack, foley.embedded_metadata); like a filename’s, it counts only when the table knows it. A keyword match weaker than an AudioSet-map hit (its band is below MatchPolicy.audioset_confidence) yields to one.

The keyword stage, in three pure steps over the table’s TermIndex (built once per table):

  1. collect — each term (subcategory, category name, synonym) found in one field of the input, word-boundary aware and across function words, becomes a Hit; the hits of one CatID fold into its Evidence;

  2. pick — the highest score wins (one word counts once, whatever it hits); on a tie, the stronger evidence, with a category name as strong as a subcategory, then the category’s generic (GENERAL/MISC) row, then table order;

  3. band — the confidence says how much evidence there is (MatchPolicy.band()), not which row won.

Everything tunable is one frozen MatchPolicy (policy=). The always-available default is this stdlib KeywordResolver; a richer CLAP zero-shot resolver (embed once, argmax over UCS label prompts, report 03 Part 2) can be dropped in behind the same TaxonomyResolver protocol.

Module Attributes

STATED_CONFIDENCE

the sound says what it is.

DEFAULT_STOP_WORDS

English function words, dropped from both sides before matching, so a phrase matches across them ("rain on window" in "rain on a window").

DEFAULT_AMBIGUOUS_WORDS

Common narration words that the UCS master also lists as a subcategory or synonym (quiet is CRWDQuiet, release an AIR synonym): they count inside a multi-word term ("glass break", "wind up"), never alone ("take a break", "the price went up").

DEFAULT_KIND_WEIGHTS

Base weight of a matched term, by where it came from.

STRONG

Evidence strengths, strongest first (indices into MatchPolicy.bands).

SINGLE

Evidence strengths, strongest first (indices into MatchPolicy.bands).

WEAK

Evidence strengths, strongest first (indices into MatchPolicy.bands).

RECORD_MIN_CONFIDENCE

an AudioSet-map hit's.

Functions

idf_share(shared_by, n_rows)

A synonym weighs its BM25 IDF over the n_rows UCS rows (each row a document), relative to a synonym only one row lists (foley.index.indexes.bm25_idf(), the IDF MemoryIndex ranks with).

inverse_sqrt_share(shared_by, n_rows)

A synonym listed by shared_by rows weighs 1/sqrt(shared_by) of its kind's weight: "car" (32 rows) a sixth, "doorknob" (one row) all of it.

keyword_resolution([tags, caption, table, ...])

The keyword stage alone: the best row for tags/caption, banded.

resolve_catid(*[, tags, caption, ...])

Resolve inputs to a best UCS CatID by the staged precedence.

term_index(table)

The TermIndex of table, built once per table.

Classes

Evidence(catid, score, matched, words, ...)

Everything the input says for one CatID: the folded Hit s.

Hit(catid, kind, text, words, weight, shared_by)

One term of a row found in the input.

KeywordResolver(*[, table, audioset_map, ...])

The default stdlib resolver — staged keyword/synonym/AudioSet resolution.

MatchPolicy([stop_words, ambiguous_words, ...])

How the keyword stage matches, ranks and bands (one strategy object).

TaxonomyResolver(*args, **kwargs)

Resolve a SoundRecord to a UCS CatID.

foley.index.taxonomy.resolver.DEFAULT_AMBIGUOUS_WORDS: frozenset = frozenset({'arc', 'bang', 'beat', 'big', 'blue', 'break', 'catch', 'day', 'down', 'drop', 'fall', 'general', 'handle', 'heavy', 'hit', 'hold', 'large', 'light', 'little', 'long', 'meeting', 'misc', 'night', 'off', 'open', 'out', 'over', 'part', 'person', 'piece', 'place', 'quiet', 'release', 'run', 'set', 'shot', 'side', 'small', 'sound', 'thing', 'time', 'tone', 'up', 'way', 'work'})#

Common narration words that the UCS master also lists as a subcategory or synonym (quiet is CRWDQuiet, release an AIR synonym): they count inside a multi-word term ("glass break", "wind up"), never alone ("take a break", "the price went up").

foley.index.taxonomy.resolver.DEFAULT_KIND_WEIGHTS: Mapping[str, float] = mappingproxy({'subcategory': 3.0, 'category': 3.0, 'synonym': 2.0})#

Base weight of a matched term, by where it came from. A category name is its rows’ own name, as strong as a subcategory.

foley.index.taxonomy.resolver.DEFAULT_STOP_WORDS: frozenset = frozenset({'a', 'about', 'all', 'also', 'am', 'an', 'and', 'any', 'are', 'as', 'at', 'be', 'been', 'being', 'but', 'by', 'can', 'could', 'did', 'do', 'does', 'each', 'every', 'few', 'for', 'from', 'had', 'has', 'have', 'he', 'her', 'here', 'him', 'his', 'how', 'i', 'if', 'in', 'into', 'is', 'it', 'its', 'just', 'may', 'me', 'might', 'more', 'most', 'must', 'my', 'no', 'nor', 'not', 'of', 'on', 'only', 'onto', 'or', 'other', 'our', 'own', 'same', 'she', 'should', 'so', 'some', 'such', 'than', 'that', 'the', 'their', 'them', 'then', 'there', 'these', 'they', 'this', 'those', 'to', 'too', 'under', 'us', 'very', 'was', 'we', 'were', 'what', 'when', 'where', 'which', 'who', 'why', 'will', 'with', 'without', 'would', 'you', 'your'})#

English function words, dropped from both sides before matching, so a phrase matches across them ("rain on window" in “rain on a window”). Particles that carry sound meaning (up, down, out, off, over: “wind up” is a toy, “the wind picked up” is weather) are not here but in the ambiguous words.

class foley.index.taxonomy.resolver.Evidence(catid, score, matched, words, kinds, phrase, unique_synonym)[source]#

Bases: object

Everything the input says for one CatID: the folded Hit s.

property rank_strength: int#

strength for ranking, where a category name is as strong as a subcategory: it is the row’s own name ("glass" is the GLASS category before it is DOORGlas’s subcategory).

property strength: int#

STRONG, SINGLE or WEAK.

A category name alone is a brush (WEAK): it says which category, not which row. See MatchPolicy for the bands.

Type:

How much the input says

class foley.index.taxonomy.resolver.Hit(catid, kind, text, words, weight, shared_by)[source]#

Bases: object

One term of a row found in the input.

words are the term’s content lemmas (stop words dropped); shared_by is how many rows list the term.

class foley.index.taxonomy.resolver.KeywordResolver(*, table=None, audioset_map=None, policy=MatchPolicy(stop_words=frozenset({'your', 'me', 'into', 'those', 'on', 'very', 'just', 'then', 'am', 'been', 'more', 'without', 'nor', 'has', 'why', 'that', 'how', 'own', 'at', 'in', 'is', 'were', 'under', 'any', 'will', 'most', 'should', 'such', 'their', 'a', 'us', 'not', 'can', 'if', 'these', 'had', 'where', 'may', 'each', 'its', 'could', 'he', 'of', 'my', 'our', 'the', 'who', 'does', 'as', 'must', 'it', 'what', 'this', 'she', 'might', 'other', 'also', 'they', 'some', 'all', 'him', 'same', 'from', 'with', 'to', 'so', 'for', 'there', 'his', 'we', 'no', 'and', 'here', 'being', 'which', 'about', 'are', 'i', 'have', 'an', 'you', 'onto', 'be', 'or', 'too', 'them', 'but', 'was', 'when', 'her', 'only', 'did', 'than', 'by', 'do', 'would', 'every', 'few'}), ambiguous_words=frozenset({'over', 'small', 'down', 'tone', 'release', 'part', 'thing', 'break', 'sound', 'night', 'off', 'work', 'up', 'meeting', 'long', 'little', 'bang', 'light', 'large', 'misc', 'shot', 'hold', 'day', 'person', 'heavy', 'general', 'hit', 'set', 'out', 'way', 'beat', 'quiet', 'drop', 'blue', 'time', 'fall', 'run', 'handle', 'big', 'place', 'side', 'arc', 'piece', 'open', 'catch'}), skip_categories=frozenset({'ARCHIVED'}), kind_weights=mappingproxy({'subcategory': 3.0, 'category': 3.0, 'synonym': 2.0}), phrase_bonus=1.0, synonym_weight=<function inverse_sqrt_share>, strong_confidence=0.8, single_confidence=0.6, weak_confidence=0.4, audioset_confidence=0.5))[source]#

Bases: object

The default stdlib resolver — staged keyword/synonym/AudioSet resolution.

Zero heavy dependencies. Bind a different TaxonomyResolver (e.g. a future CLAP zero-shot resolver) by keyword injection to upgrade quality.

resolve(record)[source]#

Resolve a SoundRecord’s tags/caption/labels/uri.

Return type:

CatIdResolution

class foley.index.taxonomy.resolver.MatchPolicy(stop_words=frozenset({'a', 'about', 'all', 'also', 'am', 'an', 'and', 'any', 'are', 'as', 'at', 'be', 'been', 'being', 'but', 'by', 'can', 'could', 'did', 'do', 'does', 'each', 'every', 'few', 'for', 'from', 'had', 'has', 'have', 'he', 'her', 'here', 'him', 'his', 'how', 'i', 'if', 'in', 'into', 'is', 'it', 'its', 'just', 'may', 'me', 'might', 'more', 'most', 'must', 'my', 'no', 'nor', 'not', 'of', 'on', 'only', 'onto', 'or', 'other', 'our', 'own', 'same', 'she', 'should', 'so', 'some', 'such', 'than', 'that', 'the', 'their', 'them', 'then', 'there', 'these', 'they', 'this', 'those', 'to', 'too', 'under', 'us', 'very', 'was', 'we', 'were', 'what', 'when', 'where', 'which', 'who', 'why', 'will', 'with', 'without', 'would', 'you', 'your'}), ambiguous_words=frozenset({'arc', 'bang', 'beat', 'big', 'blue', 'break', 'catch', 'day', 'down', 'drop', 'fall', 'general', 'handle', 'heavy', 'hit', 'hold', 'large', 'light', 'little', 'long', 'meeting', 'misc', 'night', 'off', 'open', 'out', 'over', 'part', 'person', 'piece', 'place', 'quiet', 'release', 'run', 'set', 'shot', 'side', 'small', 'sound', 'thing', 'time', 'tone', 'up', 'way', 'work'}), skip_categories=frozenset({'ARCHIVED'}), kind_weights=mappingproxy({'subcategory': 3.0, 'category': 3.0, 'synonym': 2.0}), phrase_bonus=1.0, synonym_weight=<function inverse_sqrt_share>, strong_confidence=0.8, single_confidence=0.6, weak_confidence=0.4, audioset_confidence=0.5)[source]#

Bases: object

How the keyword stage matches, ranks and bands (one strategy object).

stop_words#

Words dropped from both sides before matching.

Type:

frozenset

ambiguous_words#

Words that match only inside a multi-word term.

Type:

frozenset

skip_categories#

UCS categories the keyword stage never picks (a filename or declared CatID still may).

Type:

frozenset

kind_weights#

Base weight of a hit by term kind (subcategory, category, synonym).

Type:

Mapping[str, float]

phrase_bonus#

Added per content word past the first of a multi-word term.

Type:

float

synonym_weight#

(rows listing the synonym, rows in the table) -> factor on a synonym hit’s weight: inverse_sqrt_share() (default) or idf_share() (foley#290).

Type:

Callable[[int, int], float]

Return type:

float

strong_confidence#

Band of strong evidence: a subcategory hit, a multi-word phrase, or two distinct content words on the row (a category name plus another word counts).

Type:

float

single_confidence#

Band of one content word that only this row lists as a synonym.

Type:

float

weak_confidence#

Band of a brush: a category name alone, or a synonym many rows share. A confident=False row drops one band (floored here).

Type:

float

audioset_confidence#

Band of an AudioSet-map hit; a weaker keyword match yields to one.

Type:

float

band(strength, *, confident=True)[source]#

The band of evidence of strength; an unverified row drops one.

Return type:

float

property bands: tuple[float, float, float]#

The keyword bands, by STRONG, SINGLE, WEAK.

synonym_weight(n_rows)#

A synonym listed by shared_by rows weighs 1/sqrt(shared_by) of its kind’s weight: “car” (32 rows) a sixth, “doorknob” (one row) all of it.

Return type:

float

foley.index.taxonomy.resolver.RECORD_MIN_CONFIDENCE: float = 0.5#

an AudioSet-map hit’s. A weak keyword brush (a category name alone, a synonym many rows share) is below it.

Type:

The least confidence worth writing onto a record

foley.index.taxonomy.resolver.SINGLE = 1#

Evidence strengths, strongest first (indices into MatchPolicy.bands).

foley.index.taxonomy.resolver.STATED_CONFIDENCE: float = 1.0#

the sound says what it is.

Type:

A filename’s or the file’s own metadata’s CatID

foley.index.taxonomy.resolver.STRONG = 0#

Evidence strengths, strongest first (indices into MatchPolicy.bands).

class foley.index.taxonomy.resolver.TaxonomyResolver(*args, **kwargs)[source]#

Bases: Protocol

Resolve a SoundRecord to a UCS CatID.

resolve(record)[source]#

Return the CatID resolution for record.

Return type:

CatIdResolution

foley.index.taxonomy.resolver.WEAK = 2#

Evidence strengths, strongest first (indices into MatchPolicy.bands).

foley.index.taxonomy.resolver.idf_share(shared_by, n_rows)[source]#

A synonym weighs its BM25 IDF over the n_rows UCS rows (each row a document), relative to a synonym only one row lists (foley.index.indexes.bm25_idf(), the IDF MemoryIndex ranks with). Gentler on shared words than inverse_sqrt_share(): “car” (32 of 753 rows) keeps about half its weight.

Return type:

float

foley.index.taxonomy.resolver.inverse_sqrt_share(shared_by, n_rows)[source]#

A synonym listed by shared_by rows weighs 1/sqrt(shared_by) of its kind’s weight: “car” (32 rows) a sixth, “doorknob” (one row) all of it.

Return type:

float

foley.index.taxonomy.resolver.keyword_resolution(tags=(), caption=None, *, table=None, policy=MatchPolicy(stop_words=frozenset({'your', 'me', 'into', 'those', 'on', 'very', 'just', 'then', 'am', 'been', 'more', 'without', 'nor', 'has', 'why', 'that', 'how', 'own', 'at', 'in', 'is', 'were', 'under', 'any', 'will', 'most', 'should', 'such', 'their', 'a', 'us', 'not', 'can', 'if', 'these', 'had', 'where', 'may', 'each', 'its', 'could', 'he', 'of', 'my', 'our', 'the', 'who', 'does', 'as', 'must', 'it', 'what', 'this', 'she', 'might', 'other', 'also', 'they', 'some', 'all', 'him', 'same', 'from', 'with', 'to', 'so', 'for', 'there', 'his', 'we', 'no', 'and', 'here', 'being', 'which', 'about', 'are', 'i', 'have', 'an', 'you', 'onto', 'be', 'or', 'too', 'them', 'but', 'was', 'when', 'her', 'only', 'did', 'than', 'by', 'do', 'would', 'every', 'few'}), ambiguous_words=frozenset({'over', 'small', 'down', 'tone', 'release', 'part', 'thing', 'break', 'sound', 'night', 'off', 'work', 'up', 'meeting', 'long', 'little', 'bang', 'light', 'large', 'misc', 'shot', 'hold', 'day', 'person', 'heavy', 'general', 'hit', 'set', 'out', 'way', 'beat', 'quiet', 'drop', 'blue', 'time', 'fall', 'run', 'handle', 'big', 'place', 'side', 'arc', 'piece', 'open', 'catch'}), skip_categories=frozenset({'ARCHIVED'}), kind_weights=mappingproxy({'subcategory': 3.0, 'category': 3.0, 'synonym': 2.0}), phrase_bonus=1.0, synonym_weight=<function inverse_sqrt_share>, strong_confidence=0.8, single_confidence=0.6, weak_confidence=0.4, audioset_confidence=0.5))[source]#

The keyword stage alone: the best row for tags/caption, banded.

>>> keyword_resolution(caption="a heavy wooden door creaks open").catid
'DOORWood'
>>> r = keyword_resolution(caption="the wind picked up")
>>> r.catid, r.confidence
('WIND', 0.4)
Return type:

CatIdResolution

foley.index.taxonomy.resolver.resolve_catid(*, tags=(), caption=None, audioset_labels=(), filename=None, catid=None, table=None, audioset_map=None, policy=MatchPolicy(stop_words=frozenset({'your', 'me', 'into', 'those', 'on', 'very', 'just', 'then', 'am', 'been', 'more', 'without', 'nor', 'has', 'why', 'that', 'how', 'own', 'at', 'in', 'is', 'were', 'under', 'any', 'will', 'most', 'should', 'such', 'their', 'a', 'us', 'not', 'can', 'if', 'these', 'had', 'where', 'may', 'each', 'its', 'could', 'he', 'of', 'my', 'our', 'the', 'who', 'does', 'as', 'must', 'it', 'what', 'this', 'she', 'might', 'other', 'also', 'they', 'some', 'all', 'him', 'same', 'from', 'with', 'to', 'so', 'for', 'there', 'his', 'we', 'no', 'and', 'here', 'being', 'which', 'about', 'are', 'i', 'have', 'an', 'you', 'onto', 'be', 'or', 'too', 'them', 'but', 'was', 'when', 'her', 'only', 'did', 'than', 'by', 'do', 'would', 'every', 'few'}), ambiguous_words=frozenset({'over', 'small', 'down', 'tone', 'release', 'part', 'thing', 'break', 'sound', 'night', 'off', 'work', 'up', 'meeting', 'long', 'little', 'bang', 'light', 'large', 'misc', 'shot', 'hold', 'day', 'person', 'heavy', 'general', 'hit', 'set', 'out', 'way', 'beat', 'quiet', 'drop', 'blue', 'time', 'fall', 'run', 'handle', 'big', 'place', 'side', 'arc', 'piece', 'open', 'catch'}), skip_categories=frozenset({'ARCHIVED'}), kind_weights=mappingproxy({'subcategory': 3.0, 'category': 3.0, 'synonym': 2.0}), phrase_bonus=1.0, synonym_weight=<function inverse_sqrt_share>, strong_confidence=0.8, single_confidence=0.6, weak_confidence=0.4, audioset_confidence=0.5))[source]#

Resolve inputs to a best UCS CatID by the staged precedence.

Parameters:
  • tags (Sequence[str]) – Free tags on the sound.

  • caption (Optional[str]) – Free-text caption/description.

  • audioset_labels (Sequence[str]) – AudioSet MIDs or names (e.g. from PANNs).

  • filename (Optional[str]) – Optional UCS-style filename/path (its token-0 CatID wins if recognized).

  • catid (Optional[str]) – Optional CatID the sound declares (e.g. its iXML CATID); wins over keywords when the table knows it, else it is ignored.

  • table (Optional[UcsTable]) – UCS table (defaults to default_ucs_table()).

  • audioset_map (Optional[AudioSetUcsMap]) – AudioSet->UCS map (defaults to default_audioset_ucs_map()).

  • policy (MatchPolicy) – How keywords match and band (default DEFAULT_MATCH_POLICY; vary it with dataclasses.replace).

Return type:

CatIdResolution

Returns:

A CatIdResolution (falsy when nothing resolved).

foley.index.taxonomy.resolver.term_index(table)[source]#

The TermIndex of table, built once per table.

Return type:

TermIndex