Spaces:
Sleeping
Sleeping
Download metadata_search/corpus.py from EnvironmentalAI/WaterScopeAI-Search: direct link, hf CLI and curl.
- Browser
- Download file 3.58 kB
-
https://huggingface.co/spaces/EnvironmentalAI/WaterScopeAI-Search/resolve/main/metadata_search/corpus.py
- Command line
-
hf download hf://spaces/EnvironmentalAI/WaterScopeAI-Search/metadata_search/corpus.py
-
curl -L -o corpus.py https://huggingface.co/spaces/EnvironmentalAI/WaterScopeAI-Search/resolve/main/metadata_search/corpus.py
3.58 kB
| """Paper corpus helpers: CSV loading, embedding text, boolean tokenization.""" | |
| from __future__ import annotations | |
| import csv | |
| import re | |
| from pathlib import Path | |
| from metadata_search.config import ABSTRACT_COL, KEYWORD_COL, TITLE_COL | |
| TOKEN_RE = re.compile(r"[a-z0-9]+(?:[-'][a-z0-9]+)?") | |
| STOPWORDS = frozenset( | |
| "a an and are as at be been being by can did do does for from had has have " | |
| "how in into is it its may of on or our that the their these this those to " | |
| "using was were what when where which with within without".split() | |
| ) | |
| IRREGULAR_LEMMAS = { | |
| "analyses": "analysis", | |
| "bacteria": "bacterium", | |
| "children": "child", | |
| "data": "datum", | |
| "indices": "index", | |
| "matrices": "matrix", | |
| "media": "medium", | |
| "mice": "mouse", | |
| "species": "species", | |
| } | |
| ALIASES = { | |
| "mbr": "membrane_bioreactor", | |
| "membrane bioreactor": "membrane_bioreactor", | |
| "wwtp": "wastewater_treatment_plant", | |
| "wastewater treatment plant": "wastewater_treatment_plant", | |
| "pfas": "per_polyfluoroalkyl_substances", | |
| "per- and polyfluoroalkyl substances": "per_polyfluoroalkyl_substances", | |
| "lca": "life_cycle_assessment", | |
| "life cycle assessment": "life_cycle_assessment", | |
| "cfd": "computational_fluid_dynamics", | |
| "computational fluid dynamics": "computational_fluid_dynamics", | |
| "gis": "geographic_information_system", | |
| "geographic information system": "geographic_information_system", | |
| } | |
| def load_rows(csv_path: str | Path) -> list[dict[str, str]]: | |
| with Path(csv_path).open(newline="", encoding="utf-8-sig") as handle: | |
| return list(csv.DictReader(handle)) | |
| def paper_text( | |
| row: dict[str, str], | |
| *, | |
| title_col: str = TITLE_COL, | |
| abstract_col: str = ABSTRACT_COL, | |
| keyword_col: str = KEYWORD_COL, | |
| ) -> str: | |
| title = (row.get(title_col) or "").strip() | |
| abstract = (row.get(abstract_col) or "").strip() | |
| keywords = (row.get(keyword_col) or "").strip() | |
| return f"{title}\n\n{abstract}\n\nKeywords: {keywords}".strip() | |
| def lemmatize(token: str) -> str: | |
| if token in IRREGULAR_LEMMAS: | |
| return IRREGULAR_LEMMAS[token] | |
| if len(token) > 4 and token.endswith("ies"): | |
| return f"{token[:-3]}y" | |
| if len(token) > 5 and token.endswith(("ches", "shes", "sses", "xes", "zes")): | |
| return token[:-2] | |
| if len(token) > 3 and token.endswith("s") and not token.endswith( | |
| ("ss", "us", "is", "ics") | |
| ): | |
| return token[:-1] | |
| return token | |
| def _surface_tokens(text: str) -> list[str]: | |
| return [token for token in TOKEN_RE.findall((text or "").lower()) if token not in STOPWORDS] | |
| def tokenize(text: str, *, include_phrases: bool = True) -> list[str]: | |
| """Lexical features for boolean query matching.""" | |
| surface = _surface_tokens(text) | |
| lemmas = [lemmatize(token) for token in surface] | |
| features = list(surface) | |
| features.extend(f"lemma:{lemma}" for lemma in lemmas) | |
| if include_phrases: | |
| features.extend(f"phrase:{left}_{right}" for left, right in zip(lemmas, lemmas[1:])) | |
| lowered = " ".join(TOKEN_RE.findall((text or "").lower())) | |
| surface_set = set(surface) | |
| for alias, concept in ALIASES.items(): | |
| normalized_alias = " ".join(TOKEN_RE.findall(alias.lower())) | |
| if (" " not in normalized_alias and normalized_alias in surface_set) or ( | |
| " " in normalized_alias and f" {normalized_alias} " in f" {lowered} " | |
| ): | |
| features.append(f"concept:{concept}") | |
| return features | |