Spaces:
Running
Running
Fix count_words: isolated logic, total includes stop-words and digits
Browse files- 2026-02-08_00-16-10.png +0 -0
- logic.py +21 -15
2026-02-08_00-16-10.png
ADDED
|
logic.py
CHANGED
|
@@ -169,36 +169,42 @@ def generate_ngrams_safe(text: str, lang: str, n: int) -> List[str]:
|
|
| 169 |
|
| 170 |
def count_words(text: str, lang: str) -> Dict[str, int]:
|
| 171 |
"""
|
| 172 |
-
Считает с
|
| 173 |
-
|
| 174 |
-
{
|
| 175 |
-
"total": Общее кол-во слов (включая стоп-слова и цифры, как в Word).
|
| 176 |
-
"significant": Кол-во слов без стоп-слов и цифр (SEO-вес).
|
| 177 |
-
}
|
| 178 |
"""
|
| 179 |
if not text.strip():
|
| 180 |
return {"total": 0, "significant": 0}
|
| 181 |
|
|
|
|
|
|
|
| 182 |
clean_text = " ".join(text.split())
|
| 183 |
doc = get_doc(clean_text, lang)
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
|
| 188 |
for t in doc:
|
| 189 |
-
|
|
|
|
|
|
|
| 190 |
continue
|
| 191 |
|
| 192 |
-
|
| 193 |
-
total += 1
|
| 194 |
|
| 195 |
-
#
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
continue
|
| 198 |
|
| 199 |
-
|
| 200 |
|
| 201 |
-
return {"total":
|
| 202 |
|
| 203 |
# --- ANALYTICS (N-grams & BM25) ---
|
| 204 |
|
|
|
|
| 169 |
|
| 170 |
def count_words(text: str, lang: str) -> Dict[str, int]:
|
| 171 |
"""
|
| 172 |
+
Считает слова. ПОЛНОСТЬЮ ИЗОЛИРОВАННАЯ ЛОГИКА.
|
| 173 |
+
Не зависит от N-грамм или BM25.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
"""
|
| 175 |
if not text.strip():
|
| 176 |
return {"total": 0, "significant": 0}
|
| 177 |
|
| 178 |
+
# 1. Получаем чистый объект spaCy (без всяких re.sub и вырезаний)
|
| 179 |
+
# Просто убираем лишние пробелы/энтеры
|
| 180 |
clean_text = " ".join(text.split())
|
| 181 |
doc = get_doc(clean_text, lang)
|
| 182 |
|
| 183 |
+
total_count = 0
|
| 184 |
+
significant_count = 0
|
| 185 |
|
| 186 |
for t in doc:
|
| 187 |
+
# --- ФИЛЬТР ДЛЯ ОБЩЕГО КОЛИЧЕСТВА ---
|
| 188 |
+
# Считаем всё, кроме пунктуации и пробелов
|
| 189 |
+
if t.is_punct or t.is_space:
|
| 190 |
continue
|
| 191 |
|
| 192 |
+
total_count += 1
|
|
|
|
| 193 |
|
| 194 |
+
# --- ФИЛЬТР ДЛЯ ЗНАЧИМЫХ СЛОВ ---
|
| 195 |
+
# Если это стоп-слово (der, die, das...) - не считаем в значимые
|
| 196 |
+
if t.is_stop:
|
| 197 |
+
continue
|
| 198 |
+
# Если это цифра - не считаем
|
| 199 |
+
if t.is_digit or t.like_num:
|
| 200 |
+
continue
|
| 201 |
+
# Если это спецсимвол - не считаем
|
| 202 |
+
if t.pos_ == "SYM":
|
| 203 |
continue
|
| 204 |
|
| 205 |
+
significant_count += 1
|
| 206 |
|
| 207 |
+
return {"total": total_count, "significant": significant_count}
|
| 208 |
|
| 209 |
# --- ANALYTICS (N-grams & BM25) ---
|
| 210 |
|