Spaces:
Sleeping
Sleeping
Update spacy2.py
Browse files
spacy2.py
CHANGED
@@ -2,13 +2,6 @@ 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 |
-
os.environ["PATH"] += os.pathsep + "https://huggingface.co/spaces/pippobertin/Ordina_Documenti/tree/main/poppler-24.01.0"
|
10 |
-
# Configura il percorso di tesseract
|
11 |
-
pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/tesseract'
|
12 |
|
13 |
# Carica il modello linguistico italiano di spaCy
|
14 |
nlp = spacy.load('it_core_news_sm')
|
@@ -42,17 +35,6 @@ def estrai_testo_da_pdf_con_pdfplumber(file_path):
|
|
42 |
print(f"Errore nell'apertura o elaborazione del file PDF: {e}")
|
43 |
return text.lower()
|
44 |
|
45 |
-
def estrai_testo_da_pdf_con_ocr(file_path):
|
46 |
-
text = ''
|
47 |
-
pages = convert_from_path(file_path)
|
48 |
-
for page in pages:
|
49 |
-
text += pytesseract.image_to_string(page, lang='ita')
|
50 |
-
return text.lower()
|
51 |
-
|
52 |
-
def estrai_testo_da_immagine(file_path):
|
53 |
-
image = Image.open(file_path)
|
54 |
-
return pytesseract.image_to_string(image, lang='ita').lower()
|
55 |
-
|
56 |
def assegna_categoria_con_spacy(testo):
|
57 |
doc = nlp(testo)
|
58 |
for token in doc:
|
@@ -68,10 +50,6 @@ def classifica_e_sposta_documenti(cartella_origine, file_name):
|
|
68 |
# Determina il tipo di file e estrai il testo appropriatamente
|
69 |
if file_path.endswith('.pdf'):
|
70 |
testo = estrai_testo_da_pdf_con_pdfplumber(file_path)
|
71 |
-
if not testo:
|
72 |
-
testo = estrai_testo_da_pdf_con_ocr(file_path)
|
73 |
-
elif file_path.lower().endswith(('.png', '.jpg', '.jpeg')):
|
74 |
-
testo = estrai_testo_da_immagine(file_path)
|
75 |
else:
|
76 |
print(f"Il formato del file {file_path} non è supportato.")
|
77 |
return
|
|
|
2 |
import shutil
|
3 |
import os
|
4 |
import pdfplumber
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Carica il modello linguistico italiano di spaCy
|
7 |
nlp = spacy.load('it_core_news_sm')
|
|
|
35 |
print(f"Errore nell'apertura o elaborazione del file PDF: {e}")
|
36 |
return text.lower()
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
def assegna_categoria_con_spacy(testo):
|
39 |
doc = nlp(testo)
|
40 |
for token in doc:
|
|
|
50 |
# Determina il tipo di file e estrai il testo appropriatamente
|
51 |
if file_path.endswith('.pdf'):
|
52 |
testo = estrai_testo_da_pdf_con_pdfplumber(file_path)
|
|
|
|
|
|
|
|
|
53 |
else:
|
54 |
print(f"Il formato del file {file_path} non è supportato.")
|
55 |
return
|