Spaces:
Sleeping
Sleeping
Upload spacy2.py
Browse files
spacy2.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spacy
|
2 |
+
import shutil
|
3 |
+
import os
|
4 |
+
import pdfplumber
|
5 |
+
import pytesseract
|
6 |
+
from PIL import Image
|
7 |
+
from pdf2image import convert_from_path
|
8 |
+
|
9 |
+
# Configura il percorso di tesseract
|
10 |
+
pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/tesseract'
|
11 |
+
|
12 |
+
# Carica il modello linguistico italiano di spaCy
|
13 |
+
nlp = spacy.load('it_core_news_sm')
|
14 |
+
nlp.max_length = 1500000
|
15 |
+
|
16 |
+
# Mappatura delle categorie e delle relative parole chiave
|
17 |
+
categorie_keywords = {
|
18 |
+
"Fatture": ["fattura", "invoice", "parcella"],
|
19 |
+
"Visure": ["visura"],
|
20 |
+
"Contratti": ["contratto", "accordo", "incarico"],
|
21 |
+
"Bilanci": ["bilancio", "bilanci", "bilancio di esercizio", "ricavi", "costi"],
|
22 |
+
"Spese di Personale": ["cedolino", "paga"],
|
23 |
+
"Pagamenti": ["bonifico", "transazione", "pagamento", "disposizione", "cro", "ordinante", "beneficiario"],
|
24 |
+
"Preventivi": ["preventivo", "offerta", "proposta"],
|
25 |
+
"DSAN": ["dichiarazione", "dsan", "dichiarazione sostitutiva", "dichiara"],
|
26 |
+
"E.C.": ["estratto conto", "movimenti"]
|
27 |
+
}
|
28 |
+
|
29 |
+
def estrai_testo_da_pdf_con_pdfplumber(file_path):
|
30 |
+
text = ''
|
31 |
+
try:
|
32 |
+
with pdfplumber.open(file_path) as pdf:
|
33 |
+
for page in pdf.pages:
|
34 |
+
try:
|
35 |
+
page_text = page.extract_text()
|
36 |
+
if page_text:
|
37 |
+
text += page_text
|
38 |
+
except Exception as e:
|
39 |
+
print(f"Errore nell'estrazione del testo dalla pagina PDF: {e}")
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Errore nell'apertura o elaborazione del file PDF: {e}")
|
42 |
+
return text.lower()
|
43 |
+
|
44 |
+
def estrai_testo_da_pdf_con_ocr(file_path):
|
45 |
+
text = ''
|
46 |
+
pages = convert_from_path(file_path)
|
47 |
+
for page in pages:
|
48 |
+
text += pytesseract.image_to_string(page, lang='ita')
|
49 |
+
return text.lower()
|
50 |
+
|
51 |
+
def estrai_testo_da_immagine(file_path):
|
52 |
+
image = Image.open(file_path)
|
53 |
+
return pytesseract.image_to_string(image, lang='ita').lower()
|
54 |
+
|
55 |
+
def assegna_categoria_con_spacy(testo):
|
56 |
+
doc = nlp(testo)
|
57 |
+
for token in doc:
|
58 |
+
for categoria, keywords in categorie_keywords.items():
|
59 |
+
if token.text.lower() in keywords:
|
60 |
+
return categoria
|
61 |
+
return None
|
62 |
+
|
63 |
+
def classifica_e_sposta_documenti(cartella_origine, file_name):
|
64 |
+
file_path = os.path.join(cartella_origine, file_name)
|
65 |
+
testo = ''
|
66 |
+
|
67 |
+
# Determina il tipo di file e estrai il testo appropriatamente
|
68 |
+
if file_path.endswith('.pdf'):
|
69 |
+
testo = estrai_testo_da_pdf_con_pdfplumber(file_path)
|
70 |
+
if not testo:
|
71 |
+
testo = estrai_testo_da_pdf_con_ocr(file_path)
|
72 |
+
elif file_path.lower().endswith(('.png', '.jpg', '.jpeg')):
|
73 |
+
testo = estrai_testo_da_immagine(file_path)
|
74 |
+
else:
|
75 |
+
print(f"Il formato del file {file_path} non è supportato.")
|
76 |
+
return
|
77 |
+
|
78 |
+
# Assegna la categoria con spaCy
|
79 |
+
categoria = assegna_categoria_con_spacy(testo)
|
80 |
+
|
81 |
+
if categoria:
|
82 |
+
destination = os.path.join(cartella_origine, categoria, file_name)
|
83 |
+
destination_folder = os.path.join(cartella_origine, categoria)
|
84 |
+
if not os.path.exists(destination_folder):
|
85 |
+
os.makedirs(destination_folder)
|
86 |
+
|
87 |
+
# Controlla se il file esiste già e gestiscilo come preferisci
|
88 |
+
if not os.path.exists(destination):
|
89 |
+
shutil.move(file_path, destination)
|
90 |
+
else:
|
91 |
+
print(f"Il file {destination} esiste già.")
|
92 |
+
# Qui puoi decidere di sovrascriverlo, rinominarlo, o ignorarlo.
|
93 |
+
# Ad esempio, per rinominarlo:
|
94 |
+
# new_destination = os.path.join(destination_folder, "new_" + file_name)
|
95 |
+
# shutil.move(file_path, new_destination)
|
96 |
+
|
97 |
+
else:
|
98 |
+
print("Nessuna categoria trovata per il documento.")
|