-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanniftk.py
159 lines (140 loc) · 6.27 KB
/
anniftk.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
import os, sys
import argparse
import tkinter as tk
from tkinter import filedialog
from kit.csv_tool import read_csv, vocab_csv
from kit.excel_tool import read_excel, vocab_excel
from kit.pdf_tool import read_pdf
from kit.json_tool import read_json, vocab_json
from kit.vocab_tool import create_vocab
def process_file(path: str):
try:
if path.endswith('.csv'):
vocab_array = read_csv(path)
elif path.endswith('.xlsx'):
vocab_array = read_excel(path)
elif path.endswith('.pdf'):
vocab_array = read_pdf(path)
elif path.endswith('.json'):
vocab_array = read_json(path)
else:
print('Errore: formato ' + path.split('/')[-1].split('.')[1] + ' non supportato')
return vocab_array
except:
raise Exception()
def process_vocab(path: str):
try:
if path.endswith('.csv'):
vocab_array = vocab_csv(path)
elif path.endswith('.xlsx'):
vocab_array = vocab_excel(path)
elif path.endswith('.json'):
vocab_array = vocab_json(path)
else:
print('Errore: formato ' + path.split('/')[-1].split('.')[1] + ' non supportato')
return vocab_array
except:
raise Exception()
def main():
root = tk.Tk()
root.withdraw()
parser = argparse.ArgumentParser(
prog='anniftk.py',
formatter_class=argparse.RawDescriptionHelpFormatter,
description='Annif ToolKit, software a supporto della preparazione di corpora e vocabolari per l\'addestramento di Annif',
#usage='%(prog)s [options]'
)
parser.add_argument('-i', '--file', action='store_true', help='Selezione di un singolo file per la conversione in coppie di file .txt/.key. Per selezionare più file utilizzare le opzioni --folder oppure --cli. Può essere utilizzato in combinazione con il parametro -v o --vocab per creare anche il file vocabolario')
parser.add_argument('-o', '--folder', action='store_true', help='Selezione di una cartella contenente più file in formato PDF. Per ogni file verrà creato un file omonimo con estensione .txt')
parser.add_argument('-c', '--cli', action='store_true', help='Selezione di file o cartelle tramite riga di comando, non richiede interfaccia grafica. Verrà richiesto di inserire il PATH a un file o a una cartella. Può essere utilizzato in combinazione al parametro -v o --vocab per creare anche il file vocabolario')
parser.add_argument('-v', '--vocab', action='store_true', help='Selezione di un singolo file per la trasformazione in un file vocabolario compatibile con Annif. Partendo dai valori KEYS contenuti nel file di input, per ogni etichetta viene cercato l\'URI corrispettivo in Wikidata e salvato all\'interno del vocabolario *_voc_it.csv. Può essere utilizzato in combinazione ai parametri --file o --cli')
args = parser.parse_args()
# Help
if not args.file and not args.folder and not args.cli and not args.vocab:
print('')
logo = r'''
/\ (_)/ _| |__ __| |/ /
/ \ _ __ _ __ _| |_ | | | ' /
/ /\ \ | '_ \| '_ \| | _| | | | <
/ ____ \| | | | | | | | | | | | . \
/_/ \_\_| |_|_| |_|_|_| |_| |_|\_\
'''
print(logo)
parser.print_help()
print('')
sys.exit()
# -i | --file
if args.file and not args.vocab and not args.folder and not args.cli:
file = filedialog.askopenfilename(title = "Seleziona file",filetypes= (("Tutti i file","*.*"),("CSV","*.csv"),("Excel", "*.xlsx"),("JSON", "*.json")), multiple=False)
try:
process_file(file)
except Exception as err:
print(str(err))
sys.exit()
# -i -v | --file --vocab
elif args.file and args.vocab and not args.folder and not args.cli:
file = filedialog.askopenfilename(title = "Seleziona file",filetypes= (("Tutti i file","*.*"),("CSV","*.csv"),("Excel", "*.xlsx"),("JSON", "*.json")), multiple=False)
try:
vocab_array = process_file(file)
except:
print('Errore durante la conversione del file')
sys.exit()
try:
create_vocab(file, vocab_array)
except Exception as err:
print('Errore durante la creazione del vocabolario')
print(str(err))
sys.exit()
# -v | --vocab
elif not args.file and args.vocab and not args.folder and not args.cli:
file = filedialog.askopenfilename(title = "Seleziona file",filetypes= (("Tutti i file","*.*"),("CSV","*.csv"),("Excel", "*.xlsx"),("JSON", "*.json")), multiple=False)
try:
vocab_array = process_vocab(file)
except:
print('Errore durante la conversione del file')
sys.exit()
try:
create_vocab(file, vocab_array)
except:
print('Errore durante la creazione del vocabolario')
sys.exit()
# -o | --folder
elif args.folder and not args.file and not args.vocab and not args.cli:
folder = filedialog.askdirectory()
read_pdf(folder)
# -c | --cli
elif args.cli and not args.vocab and not args.file and not args.folder:
path = input('Full-path: ')
if '.' in path:
try:
process_file(path)
except Exception as err:
print(str(err))
sys.exit()
else:
read_pdf(path)
# -c -v | --cli --vocab
elif args.cli and args.vocab and not args.file and not args.folder:
path = input('Full-path: ')
if not '.' in path:
print('Errore: il parametro --vocab richiede un file, non una cartella')
sys.exit()
else:
try:
vocab_array = process_vocab(path)
except:
print('Errore durante la conversione del file')
sys.exit()
try:
create_vocab(path, vocab_array)
except:
print('Errore durante la creazione del vocabolario')
sys.exit()
# Controllo input
else:
print('Errore: combinazione di comandi non valida')
sys.exit()
return print('Operazione completata')
if __name__ == '__main__':
main()