pyevoc.preprocessing.thematic_filtering#
Generic thematic corpus filtering using anchor terms and distributional expansion.
This module implements the notebook-style thematic-subsetting procedure used in PyEvoc:
normalise an external anchor inventory;
identify direct anchor matches with a compiled regular expression;
build a document-term matrix with
CountVectorizer;identify vectorizer terms containing at least one anchor string;
compute cosine similarity between all terms and anchor-related terms;
select the most similar expansion terms;
retain documents with either direct anchor hits or enough expansion hits.
The implementation is deliberately generic. It does not assume a renewable-energy
domain and therefore uses neutral column names such as theme_subset_flag and
filter_type.
- class pyevoc.preprocessing.thematic_filtering.ThematicFilterConfig(text_col='text', min_anchor_hits=1, similarity_threshold=0.1, min_expansion_hits=2, top_n_expansion_terms=150, min_df=10, max_df=0.5, max_features=50000, stop_words='english', lowercase=True, ngram_range=(1, 2), anchor_col='anchor_hit', anchor_hits_col='anchor_hits', expansion_hits_col='expansion_hits', expansion_col='expansion_hit', subset_flag_col='theme_subset_flag', filter_type_col='filter_type', retain_columns=None, verbose=True, show_progress=True)[source]#
Bases:
objectConfiguration for anchor-based thematic filtering.
- Parameters:
text_col (str)
min_anchor_hits (int)
similarity_threshold (float)
min_expansion_hits (int)
top_n_expansion_terms (int)
max_features (int | None)
lowercase (bool)
anchor_col (str)
anchor_hits_col (str)
expansion_hits_col (str)
expansion_col (str)
subset_flag_col (str)
filter_type_col (str)
verbose (bool)
show_progress (bool)
- pyevoc.preprocessing.thematic_filtering.normalise_anchor_terms(anchors)[source]#
Lowercase, strip, deduplicate and length-sort anchor terms.
- pyevoc.preprocessing.thematic_filtering.load_anchor_terms(path, column=None)[source]#
Load anchor terms from TXT, CSV/TSV, or JSON.
TXT files should contain one term per line. CSV/TSV files use
columnif provided; otherwise the first column is used. JSON files may contain a list or a dictionary with ananchorskey.
- pyevoc.preprocessing.thematic_filtering.compile_anchor_pattern(anchors)[source]#
Compile the notebook-style anchor regex pattern.
- pyevoc.preprocessing.thematic_filtering.direct_anchor_filter(df, anchors, config)[source]#
Add anchor hit indicators to a dataframe.
- Parameters:
df (DataFrame)
config (ThematicFilterConfig)
- Return type:
DataFrame
- pyevoc.preprocessing.thematic_filtering.build_count_vectorizer(config)[source]#
Construct the CountVectorizer used for expansion-term discovery.
- Parameters:
config (ThematicFilterConfig)
- Return type:
- pyevoc.preprocessing.thematic_filtering.derive_expansion_terms(df, anchors, config)[source]#
Derive expansion terms using notebook-style term-term cosine similarity.
The function returns
expansion_terms, a diagnostic dataframe, the fitted vectorizer, and the document-term matrix.- Parameters:
df (DataFrame)
config (ThematicFilterConfig)
- Return type:
tuple[set[str], DataFrame, CountVectorizer, object]
- pyevoc.preprocessing.thematic_filtering.add_expansion_hits(df, expansion_terms, vectorizer, X, config)[source]#
Add expansion hit counts and boolean expansion-hit flags.
- Parameters:
df (DataFrame)
vectorizer (CountVectorizer)
X (Any)
config (ThematicFilterConfig)
- Return type:
DataFrame
- pyevoc.preprocessing.thematic_filtering.build_thematic_subset(df, anchor_file=None, config=ThematicFilterConfig(), expansion_terms=None, anchors=None)[source]#
Build a thematic subcorpus using anchor and expansion filters.
- Parameters:
df (DataFrame) – Input document-level dataframe.
anchor_file (str | Path | None) – Optional external anchor file. Required when
anchorsis not given.config (ThematicFilterConfig) – Filtering configuration.
expansion_terms (set[str] | list[str] | None) – Optional pre-computed expansion terms. If not supplied, expansion terms are derived from the input corpus.
anchors (list[str] | None) – Optional in-memory anchor inventory. If supplied,
anchor_fileis not required.
- Return type: