(text: Any, idf: Dict[str, float])
| 203 | |
| 204 | |
| 205 | def vectorize_text(text: Any, idf: Dict[str, float]) -> Dict[str, float]: |
| 206 | counts = Counter(tokenize(text)) |
| 207 | if not counts: |
| 208 | return {} |
| 209 | default_idf = math.log(max(2.0, len(idf) + 1.0)) |
| 210 | vector = { |
| 211 | token: (1.0 + math.log(freq)) * idf.get(token, default_idf) |
| 212 | for token, freq in counts.items() |
| 213 | } |
| 214 | return normalize_vector(vector) |
| 215 | |
| 216 | |
| 217 | def normalize_vector(vector: Dict[str, float]) -> Dict[str, float]: |
no test coverage detected