-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_util.py
More file actions
45 lines (38 loc) · 1.41 KB
/
text_util.py
File metadata and controls
45 lines (38 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import re
def text_quality(text):
total_chars = len(text)
if total_chars == 0:
return 0
non_alpha = re.findall(r'[^a-zA-Z0-9\s,.!?;:áéíóúãõçÁÉÍÓÚÃÕÇ]', text)
weird_ratio = len(non_alpha) / total_chars
words = text.split()
avg_word_len = sum(len(w) for w in words) / len(words) if words else 0
avg_words_per_line = len(words) / (text.count('\n') + 1)
return {
'weird_char_ratio': weird_ratio,
'avg_word_len': avg_word_len,
'avg_words_per_line': avg_words_per_line,
'empty': total_chars < 30
}
def apply_ocr(text) -> list:
""" Verifica a qualidade do texto e retorna uma lista de possíveis causas de baixa qualidade.
Args:
text (str): Texto a ser analisado.
Returns:
list: Lista de possíveis causas de baixa qualidade do texto.
"""
couse = []
quality = text_quality(text)
if quality == 0:
couse = ["texto vazio"]
return couse
# Verifica se a qualidade do texto é ruim e o motivo
if quality['weird_char_ratio'] > 0.1 :
couse.append("mais de 10% de caracteres 'estranhos'")
if quality['avg_word_len'] < 3 :
couse.append("palavras médias muito curtas")
if quality['avg_words_per_line'] < 1.5:
couse.append("média de palavras por linha muito baixa")
if quality['empty'] :
couse.append("página quase vazia")
return couse