(text: Any, idf: Dict[str, float])
| 286 | |
| 287 | |
| 288 | def vectorize_text(text: Any, idf: Dict[str, float]) -> Dict[str, float]: |
| 289 | counts = Counter(tokenize(text)) |
| 290 | if not counts: |
| 291 | return {} |
| 292 | default_idf = math.log(max(2.0, len(idf) + 1.0)) |
| 293 | vector = { |
| 294 | token: (1.0 + math.log(freq)) * idf.get(token, default_idf) |
| 295 | for token, freq in counts.items() |
| 296 | } |
| 297 | return normalize_vector(vector) |
| 298 | |
| 299 | |
| 300 | def normalize_vector(vector: Dict[str, float]) -> Dict[str, float]: |
no test coverage detected