git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
b438983
Branches
Tags
develop-client_server
master
typescript
fs-words.git
tools
transform.py
transition to sqlite-db
Fenris Wolf
commited
b438983
at 2017-01-29 19:30:11
transform.py
Blame
History
Raw
import sys import json def sqlformat(x): if (x is None): return "NULL" elif (type(x) == str): return ("'%s'" % x.replace("'", "\\'")) else: return str(x) def lastid(tablename): return ("(SELECT id FROM %s ORDER BY id DESC LIMIT 1)" % (tablename)) def main(): data = json.loads(sys.stdin.read()) print(len(data)) return for entry in data[324:]: query = "" query += ("INSERT INTO\n\tconcepts(type,description)\nVALUES\n\t(%s,%s)\n;\n" % (sqlformat(entry["type"]), sqlformat(entry["description"]))) concept_id = lastid("concepts") for (iso_639_2,lemmata) in list(entry["translations"].items()): for lemma in lemmata: query += ("INSERT INTO\n\twords(language_id,lemma,pronunciation)\nVALUES\n\t((SELECT id FROM languages WHERE iso_639_2=%s),%s,%s)\n;\n" % (sqlformat(iso_639_2), sqlformat(lemma), sqlformat(None))) word_id = lastid("words") strength = None query += ("INSERT INTO\n\tcorrelations(concept_id,word_id,strength)\nVALUES\n\t(%s,%s,%s)\n;\n" % (concept_id, word_id, sqlformat(strength))) for tag in entry["tags"]: query += ("INSERT INTO tags(concept_id,tag) VALUES (%s,%s);\n" % (concept_id, sqlformat(tag))) query += "--\n" sys.stdout.write(query) main()