(text: Any, idf: Dict[str, float])
| 257 | |
| 258 | |
| 259 | def vectorize_text(text: Any, idf: Dict[str, float]) -> Dict[str, float]: |
| 260 | counts = Counter(tokenize(text)) |
| 261 | if not counts: |
| 262 | return {} |
| 263 | default_idf = math.log(max(2.0, len(idf) + 1.0)) |
| 264 | vector = { |
| 265 | token: (1.0 + math.log(freq)) * idf.get(token, default_idf) |
| 266 | for token, freq in counts.items() |
| 267 | } |
| 268 | return normalize_vector(vector) |
| 269 | |
| 270 | |
| 271 | def normalize_vector(vector: Dict[str, float]) -> Dict[str, float]: |
no test coverage detected