-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
239 lines (188 loc) · 7.83 KB
/
main.py
File metadata and controls
239 lines (188 loc) · 7.83 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python3
# Imports
import os, sys, re, csv, io
from datetime import datetime
import tkinter as tk
from tkinter import filedialog
import subprocess
import platform
from typing import Optional
from urllib.parse import unquote
from csv import Sniffer
# Function to format date strings into QuickStatements date format
def format_date(date_string):
# Define possible date formats
DATE_FORMATS = [
('%d-%m-%Y', 11), # 10-06-2024
('%Y-%m-%d', 11), # 2024-06-10
('%Y-%m', 10), # 2024-06
]
clean_date = date_string.strip().replace('/', '-').replace('.', '-')
# Year only
if re.match(r'^\d{4}$', clean_date):
year = clean_date
return f"+{year}-00-00T00:00:00Z/9"
# Year-Month / Year-Month-Day
dt: Optional[datetime] = None
precision: int = 11
for fmt, prec in DATE_FORMATS:
try:
dt = datetime.strptime(clean_date, fmt)
precision = prec
break
except ValueError:
continue
if dt is None:
print(f"ATTENZIONE: Impossibile interpretare la data '{date_string}'. Riga ignorata.")
return ''
if precision == 11:
iso_date = f"+{dt.strftime('%Y-%m-%d')}T00:00:00Z/11"
elif precision == 10:
iso_date = f"+{dt.strftime('%Y-%m')}-00T00:00:00Z/10"
else:
iso_date = f"+{dt.strftime('%Y-%m-%d')}T00:00:00Z/11"
return iso_date
# Function to detect CSV delimiter
def detect_delimiter(content: str) -> str:
try:
sample = content[:min(len(content), 1024)]
dialect = Sniffer().sniff(sample)
return dialect.delimiter
except:
print("Impossibile rilevare il delimitatore, si usa il ';' (punto e virgola) come fallback.")
return ';'
# Function to convert CSV to QuickStatements TSV
def csv_to_qs():
# Define standard string fields
STRING_FIELDS = [
'Lit', 'Len', 'Lfr', 'Lde',
'Ait', 'Aen', 'Afr', 'Ade',
'Dit', 'Den', 'Dfr', 'Dde'
]
SITELINK_FIELDS = [
'itwiki', 'enwiki', 'frwiki', 'dewiki',
'commonswiki', 'itwikisource', 'enwikisource'
]
# File selection dialog
root = tk.Tk()
root.withdraw()
file = filedialog.askopenfilename(title = "Seleziona file",filetypes= (("Tutti i file","*.*"),("CSV","*.csv")), multiple=False)
if not file:
print("Nessun file selezionato")
return
# Prepare output file
filename = os.path.basename(file)
filepath = os.path.dirname(file)
new_file = os.path.join(filepath, f"{filename.split('.')[0]}_qs.tsv")
# Read the input CSV file
with open(file, 'r', encoding='utf-8') as f:
# Normalize BOM and read content
content = f.read().lstrip('\ufeff')
delimiter = detect_delimiter(content)
csv_file = io.StringIO(content)
reader = csv.reader(csv_file, delimiter=delimiter)
with open(new_file, 'w', encoding='utf-8', newline='') as write_file:
# Header mapping
header_map = {}
try:
header = next(reader)
except StopIteration:
print("Impossible to read empty file")
for idx, col_name in enumerate(header):
name = col_name.strip()
cell_type = None
if name in STRING_FIELDS:
cell_type = 'string'
elif name in SITELINK_FIELDS:
cell_type = 'sitelink'
elif name.endswith('_STR'):
name = name[:-4]
cell_type = 'string'
elif name.endswith('_NUM'):
name = name[:-4]
cell_type = 'number'
elif name.endswith('_DATE'):
name = name[:-5]
cell_type = 'date'
elif name.endswith('_GEO'):
name = name[:-4]
cell_type = 'coordinates'
header_map[idx] = {'name': name, 'cell_type': cell_type}
# Row processing
for row in reader:
# Skip empty rows
if not any(row):
continue
qid_value = None
qs_commands = []
main_statement = ""
qualifiers = []
# Process each cell in the row
for column_index, cell in enumerate(row):
if column_index not in header_map:
continue
data = header_map[column_index]
col_name = data['name'].strip()
value = cell.strip()
if not value:
continue
# Handle QID
if col_name.lower() == 'qid':
if value.startswith('http'):
value = value.split('/')[-1]
qid_value = value
continue
if qid_value:
prefix = qid_value
else:
prefix = 'LAST'
formatted_value = value
# Handle sitelinks
if data['cell_type'] == 'sitelink':
title = unquote(value).replace('_', ' ')
qs_commands.append(f'{prefix}|sitelink|{col_name}|"{title}"')
continue
# Handle other columns
if data['cell_type'] == 'string':
formatted_value = f'"{value}"'
elif data['cell_type'] == 'date':
formatted_value = format_date(value)
if not formatted_value: continue
elif data['cell_type'] == 'number':
formatted_value = value.replace(',', '.')
elif data['cell_type'] == 'coordinates':
formatted_value = f'@{value}'
if not formatted_value:
continue
# Add qualifiers to last property
if col_name.startswith('S'):
qs_qualifier = f'|{col_name}|{formatted_value}'
qualifiers.append(qs_qualifier)
else:
if not main_statement:
main_statement = f'{prefix}|{col_name}|{formatted_value}'
else:
qs_commands.append(f'{prefix}|{col_name}|{formatted_value}')
if not qid_value:
write_file.write('CREATE\n')
if main_statement:
full_command = main_statement + "".join(qualifiers)
write_file.write(f'{full_command}\n')
for cmd in qs_commands:
write_file.write(f'{cmd}\n')
write_file.write('\n')
print(f"File saved as {new_file}")
# Open the output file with the default application
if platform.system() == 'Darwin': # macOS
subprocess.call(('open', new_file))
elif platform.system() == 'Windows': # Windows
os.startfile(new_file)
else: # linux variants
subprocess.call(('xdg-open', new_file))
return
if __name__ == '__main__':
try:
csv_to_qs()
except Exception as err:
print(str(err))
sys.exit()