2022-05-22 22:17:41 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import argparse
|
|
|
|
import json
|
2023-07-09 12:29:11 +02:00
|
|
|
import sqlite3
|
|
|
|
import time
|
2022-05-22 22:17:41 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description="Program to generate JSONL files from a LibreTranslate's suggestions.db")
|
|
|
|
parser.add_argument(
|
|
|
|
"--db",
|
|
|
|
type=str,
|
|
|
|
nargs=1,
|
|
|
|
help="Path to suggestions.db file",
|
2023-01-12 17:24:18 +02:00
|
|
|
default='db/suggestions.db'
|
2022-05-22 22:17:41 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--clear",
|
|
|
|
action='store_true',
|
|
|
|
help="Clear suggestions.db after generation",
|
|
|
|
default=False
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
output_file = str(int(time.time())) + ".jsonl"
|
|
|
|
|
|
|
|
con = sqlite3.connect(args.db, check_same_thread=False)
|
|
|
|
cur = con.cursor()
|
2023-07-23 13:24:15 +02:00
|
|
|
|
2022-05-22 22:17:41 +02:00
|
|
|
with open(output_file, 'w', encoding="utf-8") as f:
|
|
|
|
for row in cur.execute('SELECT q, s, source, target FROM suggestions WHERE source != "auto" ORDER BY source'):
|
|
|
|
q, s, source, target = row
|
|
|
|
obj = {
|
|
|
|
'q': q,
|
2023-07-23 13:24:15 +02:00
|
|
|
's': s,
|
2022-05-22 22:17:41 +02:00
|
|
|
'source': source,
|
|
|
|
'target': target
|
|
|
|
}
|
|
|
|
json.dump(obj, f, ensure_ascii=False)
|
|
|
|
f.write('\n')
|
2023-07-23 13:24:15 +02:00
|
|
|
|
2022-05-22 22:17:41 +02:00
|
|
|
print("Wrote %s" % output_file)
|
|
|
|
|
|
|
|
if args.clear:
|
|
|
|
cur.execute("DELETE FROM suggestions")
|
|
|
|
con.commit()
|
|
|
|
print("Cleared " + args.db)
|