30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 | class TfidfConceptSearcher(AbstractConceptSearcher):
"""Concept searcher based on a word-level TF-IDF index built from downloaded terms."""
def __init__(self, settings: TfidfSearchSettings):
self.settings = settings
self._sorted_substrings_to_remove = sorted(settings.substrings_to_remove, key=len, reverse=True)
self.vectorizer: TfidfVectorizer
self.term_matrix = None
self.term_metadata = pd.DataFrame()
if os.path.exists(self.settings.tfidf_index_file):
self._load_index()
else:
self._create_index()
def _load_index(self) -> None:
with open(self.settings.tfidf_index_file, "rb") as handle:
index_data = pickle.load(handle)
self.vectorizer = index_data["vectorizer"]
self.term_matrix = index_data["term_matrix"]
self.term_metadata = index_data["term_metadata"]
def _create_index(self) -> None:
if not os.path.exists(self.settings.terms_folder) or not os.listdir(self.settings.terms_folder):
download_terms_for_tfidf(settings=self.settings)
all_files = [
os.path.join(self.settings.terms_folder, f)
for f in os.listdir(self.settings.terms_folder)
if f.endswith(".parquet")
]
if not all_files:
raise FileNotFoundError(
f"No parquet files found in {self.settings.terms_folder} after attempting download."
)
frames = [pd.read_parquet(file) for file in all_files]
terms_df = pd.concat(frames, ignore_index=True)
terms_df["normalized_term"] = terms_df["term"].fillna("").map(self._normalize_text)
terms_df = terms_df[terms_df["normalized_term"] != ""].copy()
terms_df["concept_id"] = terms_df["concept_id"].astype(int)
if terms_df.empty:
raise ValueError("Cannot build TF-IDF index because all source terms are empty after normalization.")
self.vectorizer = TfidfVectorizer(
analyzer="word",
ngram_range=(1, 1),
sublinear_tf=True,
min_df=1,
)
self.term_matrix = self.vectorizer.fit_transform(terms_df["normalized_term"].tolist())
self.term_metadata = terms_df[["concept_id", "concept_name", "vocabulary_id", "normalized_term"]].reset_index(drop=True)
os.makedirs(os.path.dirname(self.settings.tfidf_index_file), exist_ok=True)
with open(self.settings.tfidf_index_file, "wb") as handle:
pickle.dump(
{
"vectorizer": self.vectorizer,
"term_matrix": self.term_matrix,
"term_metadata": self.term_metadata,
},
handle,
)
def _normalize_text(self, text: str) -> str:
normalized = text.lower()
for substring in self._sorted_substrings_to_remove:
normalized = re.sub(re.escape(substring), " ", normalized, flags=re.IGNORECASE)
normalized = re.sub(r"[^a-z0-9]+", " ", normalized)
normalized = re.sub(r"\s+", " ", normalized).strip()
return normalized
def search_term(self, term: str) -> Optional[pd.DataFrame]:
query = self._normalize_text(term)
if not query:
return pd.DataFrame(columns=["concept_id", "concept_name", "vocabulary_id", "score"])
query_vector = self.vectorizer.transform([query])
scores = (self.term_matrix @ query_vector.T).toarray().ravel()
if scores.size == 0:
return pd.DataFrame(columns=["concept_id", "concept_name", "vocabulary_id", "score"])
scored = self.term_metadata.copy()
scored["score"] = scores
scored = scored[scored["score"] > 0].copy()
if self.settings.filter.vocabulary_ids:
scored = scored[scored["vocabulary_id"].isin(self.settings.filter.vocabulary_ids)]
if scored.empty:
return pd.DataFrame(columns=["concept_id", "concept_name", "vocabulary_id", "score"])
scored = scored.sort_values(by=["score", "concept_id"], ascending=[False, True])
deduped = scored.drop_duplicates(subset=["concept_id"], keep="first")
deduped = deduped.sort_values(by=["score", "concept_id"], ascending=[False, True])
top_matches = deduped.head(self.settings.max_candidates).reset_index(drop=True)
return top_matches[["concept_id", "concept_name", "vocabulary_id", "score"]]
def search_terms(
self,
df: pd.DataFrame,
term_column: str,
matched_concept_id_column: str = "matched_concept_id",
matched_concept_name_column: str = "matched_concept_name",
match_score_column: str = "match_score",
match_rank_column: str = "match_rank",
) -> pd.DataFrame:
all_results = []
for _, row in df.iterrows():
term = row[term_column]
matches = self.search_term(term)
if matches is None or matches.empty:
continue
rows = []
for rank, (_, concept) in enumerate(matches.iterrows(), start=1):
rows.append(
{
matched_concept_id_column: int(concept["concept_id"]),
matched_concept_name_column: concept["concept_name"],
match_score_column: float(concept["score"]),
match_rank_column: rank,
}
)
result_df = pd.DataFrame(rows)
original_columns = list(df.columns)
result_columns = list(result_df.columns)
for col in original_columns:
result_df[col] = row[col]
result_df = result_df[original_columns + result_columns]
all_results.append(result_df)
if not all_results:
return pd.DataFrame()
return pd.concat(all_results, ignore_index=True)
|