fix
Browse files
text_analyzer/analyzer.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
# text_analyzer/analyzer.py (WERSJA POPRAWIONA)
|
| 2 |
-
|
| 3 |
"""
|
| 4 |
Główny moduł biblioteki zawierający klasę TextAnalyzer.
|
| 5 |
"""
|
|
@@ -25,18 +23,15 @@ class TextAnalyzer:
|
|
| 25 |
Np. ["parser", "ner"] jeśli nie są potrzebne.
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
# Wyłączamy komponenty, jeśli użytkownik tego zażąda
|
| 29 |
self.nlp = spacy.load(constants.SPACY_MODEL_PL, disable=disable_pipelines or [])
|
| 30 |
self.nlp.max_length = constants.NLP_MAX_LENGTH
|
| 31 |
except OSError:
|
| 32 |
-
# ... (obsługa błędu bez zmian) ...
|
| 33 |
print(f"Błąd: Nie znaleziono modelu spaCy '{constants.SPACY_MODEL_PL}'.")
|
| 34 |
print(f"python -m spacy download {constants.SPACY_MODEL_PL}")
|
| 35 |
raise
|
| 36 |
textstat.set_lang('pl_PL')
|
| 37 |
|
| 38 |
def _preprocess(self, text: str) -> Tuple:
|
| 39 |
-
# Ta metoda jest teraz bardziej pomocnicza, doc jest przekazywany z zewnątrz
|
| 40 |
text_lower = text.lower()
|
| 41 |
words = text.split()
|
| 42 |
words_lower = text_lower.split()
|
|
@@ -45,7 +40,7 @@ class TextAnalyzer:
|
|
| 45 |
return text_lower, words, words_lower, lines, sentences
|
| 46 |
|
| 47 |
def analyze(self, text: str) -> Dict[str, float]:
|
| 48 |
-
"""Analizuje pojedynczy tekst
|
| 49 |
doc = self.nlp(text)
|
| 50 |
return self._analyze_single_doc(text, doc)
|
| 51 |
|
|
@@ -77,10 +72,9 @@ class TextAnalyzer:
|
|
| 77 |
Iterable[Dict[str, float]]: Generator zwracający słownik cech dla każdego tekstu.
|
| 78 |
"""
|
| 79 |
# Używamy nlp.pipe, który jest generatorem i przetwarza teksty wsadowo
|
| 80 |
-
# as_tuples=True pozwala przekazać teksty razem z ich oryginalnym kontekstem (choć tu nie używamy)
|
| 81 |
docs = self.nlp.pipe(texts, batch_size=batch_size)
|
| 82 |
|
| 83 |
# Przetwarzamy każdy dokument z generatora
|
| 84 |
for i, doc in enumerate(docs):
|
| 85 |
-
original_text = texts[i]
|
| 86 |
yield self._analyze_single_doc(original_text, doc)
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Główny moduł biblioteki zawierający klasę TextAnalyzer.
|
| 3 |
"""
|
|
|
|
| 23 |
Np. ["parser", "ner"] jeśli nie są potrzebne.
|
| 24 |
"""
|
| 25 |
try:
|
|
|
|
| 26 |
self.nlp = spacy.load(constants.SPACY_MODEL_PL, disable=disable_pipelines or [])
|
| 27 |
self.nlp.max_length = constants.NLP_MAX_LENGTH
|
| 28 |
except OSError:
|
|
|
|
| 29 |
print(f"Błąd: Nie znaleziono modelu spaCy '{constants.SPACY_MODEL_PL}'.")
|
| 30 |
print(f"python -m spacy download {constants.SPACY_MODEL_PL}")
|
| 31 |
raise
|
| 32 |
textstat.set_lang('pl_PL')
|
| 33 |
|
| 34 |
def _preprocess(self, text: str) -> Tuple:
|
|
|
|
| 35 |
text_lower = text.lower()
|
| 36 |
words = text.split()
|
| 37 |
words_lower = text_lower.split()
|
|
|
|
| 40 |
return text_lower, words, words_lower, lines, sentences
|
| 41 |
|
| 42 |
def analyze(self, text: str) -> Dict[str, float]:
|
| 43 |
+
"""Analizuje pojedynczy tekst"""
|
| 44 |
doc = self.nlp(text)
|
| 45 |
return self._analyze_single_doc(text, doc)
|
| 46 |
|
|
|
|
| 72 |
Iterable[Dict[str, float]]: Generator zwracający słownik cech dla każdego tekstu.
|
| 73 |
"""
|
| 74 |
# Używamy nlp.pipe, który jest generatorem i przetwarza teksty wsadowo
|
|
|
|
| 75 |
docs = self.nlp.pipe(texts, batch_size=batch_size)
|
| 76 |
|
| 77 |
# Przetwarzamy każdy dokument z generatora
|
| 78 |
for i, doc in enumerate(docs):
|
| 79 |
+
original_text = texts[i]
|
| 80 |
yield self._analyze_single_doc(original_text, doc)
|
text_analyzer/features/base_features.py
CHANGED
|
@@ -84,20 +84,17 @@ def analyze_advanced_char_features(text: str) -> Dict[str, float]:
|
|
| 84 |
word_freq = Counter(words_found)
|
| 85 |
most_common = word_freq.most_common(10)
|
| 86 |
|
| 87 |
-
# Polish diacritics
|
| 88 |
polish_diacritics = 'ąćęłńóśźżĄĆĘŁŃÓŚŹŻ'
|
| 89 |
char_counts = Counter(text)
|
| 90 |
diac_count = sum(char_counts.get(ch, 0) for ch in polish_diacritics)
|
| 91 |
letters_count = sum(1 for ch in text if ch.isalpha())
|
| 92 |
|
| 93 |
-
# Single char words
|
| 94 |
single_chars = [w for w in words_found if len(w) == 1]
|
| 95 |
single_char_freq = Counter(single_chars)
|
| 96 |
top_3_single = single_char_freq.most_common(3)
|
| 97 |
top_codes = [ord(w) for w, _ in top_3_single]
|
| 98 |
while len(top_codes) < 3: top_codes.append(0)
|
| 99 |
|
| 100 |
-
# Encoding
|
| 101 |
replacement_count = char_counts.get('\uFFFD', 0)
|
| 102 |
not_allowed_count = sum(1 for ch in text if not ALLOWED_CHARS_PATTERN.match(ch))
|
| 103 |
replacement_ratio = safe_divide(replacement_count, total_chars)
|
|
@@ -143,7 +140,7 @@ def analyze_word_stats(words: List[str], words_lower: List[str]) -> Dict[str, fl
|
|
| 143 |
if not total_words: return {'mean_word_length': 0.0, 'lexical_diversity': 0.0, 'count_caps': 0.0, 'word_isupper<5': 0, 'word_isupper>5': 0, 'count_digit_to_caps': 0.0}
|
| 144 |
|
| 145 |
digit_count = sum(1 for w in words if any(ch.isdigit() for ch in w))
|
| 146 |
-
caps_count = sum(1 for w in words if w.isupper())
|
| 147 |
|
| 148 |
return {
|
| 149 |
'mean_word_length': safe_divide(sum(len(w) for w in words_lower), total_words),
|
|
@@ -222,7 +219,7 @@ def analyze_line_content(lines: List[str]) -> Dict[str, float]:
|
|
| 222 |
}
|
| 223 |
|
| 224 |
def count_lorem_ipsum(text_lower: str) -> Dict[str, float]:
|
| 225 |
-
"""Oblicza stosunek
|
| 226 |
count = text_lower.count('lorem ipsum')
|
| 227 |
return {'lorem_ipsum_ratio': safe_divide(count, len(text_lower))}
|
| 228 |
|
|
|
|
| 84 |
word_freq = Counter(words_found)
|
| 85 |
most_common = word_freq.most_common(10)
|
| 86 |
|
|
|
|
| 87 |
polish_diacritics = 'ąćęłńóśźżĄĆĘŁŃÓŚŹŻ'
|
| 88 |
char_counts = Counter(text)
|
| 89 |
diac_count = sum(char_counts.get(ch, 0) for ch in polish_diacritics)
|
| 90 |
letters_count = sum(1 for ch in text if ch.isalpha())
|
| 91 |
|
|
|
|
| 92 |
single_chars = [w for w in words_found if len(w) == 1]
|
| 93 |
single_char_freq = Counter(single_chars)
|
| 94 |
top_3_single = single_char_freq.most_common(3)
|
| 95 |
top_codes = [ord(w) for w, _ in top_3_single]
|
| 96 |
while len(top_codes) < 3: top_codes.append(0)
|
| 97 |
|
|
|
|
| 98 |
replacement_count = char_counts.get('\uFFFD', 0)
|
| 99 |
not_allowed_count = sum(1 for ch in text if not ALLOWED_CHARS_PATTERN.match(ch))
|
| 100 |
replacement_ratio = safe_divide(replacement_count, total_chars)
|
|
|
|
| 140 |
if not total_words: return {'mean_word_length': 0.0, 'lexical_diversity': 0.0, 'count_caps': 0.0, 'word_isupper<5': 0, 'word_isupper>5': 0, 'count_digit_to_caps': 0.0}
|
| 141 |
|
| 142 |
digit_count = sum(1 for w in words if any(ch.isdigit() for ch in w))
|
| 143 |
+
caps_count = sum(1 for w in words if w.isupper())
|
| 144 |
|
| 145 |
return {
|
| 146 |
'mean_word_length': safe_divide(sum(len(w) for w in words_lower), total_words),
|
|
|
|
| 219 |
}
|
| 220 |
|
| 221 |
def count_lorem_ipsum(text_lower: str) -> Dict[str, float]:
|
| 222 |
+
"""Oblicza stosunek lorem ipsum"""
|
| 223 |
count = text_lower.count('lorem ipsum')
|
| 224 |
return {'lorem_ipsum_ratio': safe_divide(count, len(text_lower))}
|
| 225 |
|
text_analyzer/features/linguistic_features.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# text_analyzer/features/linguistic_features.py
|
| 2 |
"""
|
| 3 |
Moduł do ekstrakcji cech lingwistycznych i stylistycznych tekstu.
|
| 4 |
"""
|
|
@@ -45,7 +44,6 @@ def calculate_symbol_to_word_ratio(words: List[str], text: str) -> Dict[str, flo
|
|
| 45 |
symbol_count = char_counts.get('#', 0) + triple_dot_count + char_counts.get('…', 0)
|
| 46 |
return {'symbol_to_word_ratio': safe_divide(symbol_count, total_words)}
|
| 47 |
|
| 48 |
-
|
| 49 |
# --- Funkcje analizujące n-gramy ---
|
| 50 |
|
| 51 |
def calculate_ngram_fractions(words: List[str]) -> Dict[str, float]:
|
|
@@ -77,7 +75,6 @@ def calculate_ngram_fractions(words: List[str]) -> Dict[str, float]:
|
|
| 77 |
# --- Funkcje analizujące styl tekstu ---
|
| 78 |
|
| 79 |
def analyze_stylistic_metrics(text: str, words: List[str], sentences: List[str]) -> Dict[str, float]:
|
| 80 |
-
# Używamy prostej tokenizacji, aby zachować zgodność ze starym kodem
|
| 81 |
sentences_from_regex = re.findall(r'[^.!?]+[.!?]', text)
|
| 82 |
num_sentences = len(sentences_from_regex)
|
| 83 |
words_per_sentence = [len(s.split()) for s in sentences_from_regex]
|
|
@@ -114,9 +111,9 @@ def calculate_all_linguistic_features(text: str, text_lower: str, words: List[st
|
|
| 114 |
features.update(calculate_stop_word_ratio(words_lower))
|
| 115 |
features.update(count_bad_words(words_lower))
|
| 116 |
features.update(calculate_unigram_entropy(words_lower))
|
| 117 |
-
features.update(count_non_alpha_words(text))
|
| 118 |
features.update(calculate_symbol_to_word_ratio(words, text))
|
| 119 |
features.update(calculate_ngram_fractions(words))
|
| 120 |
features.update(analyze_stylistic_metrics(text, words, sentences))
|
| 121 |
-
features['javascript_counts_per_line'] = text_lower.count('javascript')
|
| 122 |
return features
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Moduł do ekstrakcji cech lingwistycznych i stylistycznych tekstu.
|
| 3 |
"""
|
|
|
|
| 44 |
symbol_count = char_counts.get('#', 0) + triple_dot_count + char_counts.get('…', 0)
|
| 45 |
return {'symbol_to_word_ratio': safe_divide(symbol_count, total_words)}
|
| 46 |
|
|
|
|
| 47 |
# --- Funkcje analizujące n-gramy ---
|
| 48 |
|
| 49 |
def calculate_ngram_fractions(words: List[str]) -> Dict[str, float]:
|
|
|
|
| 75 |
# --- Funkcje analizujące styl tekstu ---
|
| 76 |
|
| 77 |
def analyze_stylistic_metrics(text: str, words: List[str], sentences: List[str]) -> Dict[str, float]:
|
|
|
|
| 78 |
sentences_from_regex = re.findall(r'[^.!?]+[.!?]', text)
|
| 79 |
num_sentences = len(sentences_from_regex)
|
| 80 |
words_per_sentence = [len(s.split()) for s in sentences_from_regex]
|
|
|
|
| 111 |
features.update(calculate_stop_word_ratio(words_lower))
|
| 112 |
features.update(count_bad_words(words_lower))
|
| 113 |
features.update(calculate_unigram_entropy(words_lower))
|
| 114 |
+
features.update(count_non_alpha_words(text))
|
| 115 |
features.update(calculate_symbol_to_word_ratio(words, text))
|
| 116 |
features.update(calculate_ngram_fractions(words))
|
| 117 |
features.update(analyze_stylistic_metrics(text, words, sentences))
|
| 118 |
+
features['javascript_counts_per_line'] = text_lower.count('javascript')
|
| 119 |
return features
|
text_analyzer/features/regex_features.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# text_analyzer/features/regex_features.py
|
| 2 |
"""
|
| 3 |
Moduł do ekstrakcji cech opartych na wyrażeniach regularnych.
|
| 4 |
|
|
@@ -37,7 +36,6 @@ def calculate_all_regex_features(text: str) -> Dict[str, int]:
|
|
| 37 |
matches = pattern.findall(text)
|
| 38 |
features[name] = len(matches)
|
| 39 |
except Exception as e:
|
| 40 |
-
# Dodajemy obsługę błędów na wypadek problemów z regexem
|
| 41 |
print(f"Błąd podczas przetwarzania wzorca '{name}': {e}")
|
| 42 |
features[name] = 0
|
| 43 |
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Moduł do ekstrakcji cech opartych na wyrażeniach regularnych.
|
| 3 |
|
|
|
|
| 36 |
matches = pattern.findall(text)
|
| 37 |
features[name] = len(matches)
|
| 38 |
except Exception as e:
|
|
|
|
| 39 |
print(f"Błąd podczas przetwarzania wzorca '{name}': {e}")
|
| 40 |
features[name] = 0
|
| 41 |
|
text_analyzer/features/structural_features.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# text_analyzer/features/structural_features.py
|
| 2 |
"""
|
| 3 |
Moduł do ekstrakcji cech strukturalnych i formatowania tekstu.
|
| 4 |
"""
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Moduł do ekstrakcji cech strukturalnych i formatowania tekstu.
|
| 3 |
"""
|