Skip to content

Instantly share code, notes, and snippets.

@msikma
Created February 14, 2016 15:53
Show Gist options
  • Save msikma/1a5a9b62becd6b030a3c to your computer and use it in GitHub Desktop.
Save msikma/1a5a9b62becd6b030a3c to your computer and use it in GitHub Desktop.
'''
Schema for JMdict files. There will be only a single entry: the currently
used JMdict file. If needed, it's possible to create a new one, parse and
insert a whole new JMdict file, and then delete the old.
Every dictionary entry is in a 1-n relationship with the DictDictionary
entry.
'''
from tqdm import tqdm
from reisan.flask import db
from reisan.util.tformat import tprint
from .entity import DictEntity
from .entry import DictEntry
class DictDictionary(db.Model):
'''
Database table for JMdict files.
'''
__tablename__ = 'dict_dictionary'
id = db.Column(db.Integer, primary_key=True)
file = db.Column(db.Text)
pub = db.Column(db.Date())
active = db.Column(db.Boolean())
# Bind to entries and entities.
entries = db.relationship('DictEntry')
entities = db.relationship('DictEntity')
def __init__(self, dict, file, pub, entities, active=True, debug=False):
'''
Runs through the entire JMdict file and inserts all the entries.
This is only done when a new dictionary file needs to be bootstrapped
into the database during development. It's not used during deployment;
after bootstrapping is complete, the database can be exported and
then imported during deployment.
'''
self.file = file
self.pub = pub
self.active = active
for name in entities:
self.entities.append(DictEntity(name=name, desc=entities[name]))
if debug:
tprint('Inserted {} named entities.'.format(len(entities)))
tprint('Found {} dictionary entries.'.format(len(dict)))
# Wrap our dictionary in tqdm() to display the progress bar.
dict = tqdm(dict)
for entry_data in dict:
self.entries.append(DictEntry(entry_data))
db.session.commit()
def __repr__(self):
return '<db.Model.DictDictionary({!r}, {!s})>'.format(
self.file,
self.pub
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment