def short_expansion(cur, abbrev): database.execute(cur, ''' SELECT short_expansion FROM abbreviations WHERE abbrev = ? ;''', [abbrev]) row = cur.fetchone() if row: return row[0] else: return None
def short_expansion(cur, abbrev): database.execute( cur, ''' SELECT short_expansion FROM abbreviations WHERE abbrev = ? ;''', [abbrev]) row = cur.fetchone() if row: return row[0] else: return None
def abbrevs_table(cur): database.execute(cur, ''' SELECT abbrev FROM abbreviations ORDER BY abbrev ;''') abbrevs=[] for row in cur.fetchall(): abbrevs.append(row[0]) return "\n".join([abbrev_line(cur, abbrev) for abbrev in abbrevs])
def abbrevs_table(cur): database.execute( cur, ''' SELECT abbrev FROM abbreviations ORDER BY abbrev ;''') abbrevs = [] for row in cur.fetchall(): abbrevs.append(row[0]) return "\n".join([abbrev_line(cur, abbrev) for abbrev in abbrevs])
def search_by(cur, cond): '''Main search function. Take a SearchCondition object, return list of ent_seqs. ''' if ((cond.field == 'gloss' and cond.case_sensitive) or cond.extent in ('whole', 'partial')): fts = False query_s = cond.query_s[:] else: fts = True query_prep = [] for cmdline_arg in cond.query: # unify Japanese spaces, newlines etc. to a single space re.sub('\\s+', ' ', cmdline_arg) # if the user called # # myougiden 'full phrase' # # or # myougiden "full phrase" # # we'll get a single argv with spaces in it. in this case we # translate the argv to the string # # "full phrase" # # (including the double quotes), which is what sqlite FTS looks # for. if ' ' in cmdline_arg: cmdline_arg = '"' + cmdline_arg + '"' if cond.extent == 'beginning': cmdline_arg += '*' query_prep.append(cmdline_arg) query_s = ' '.join(query_prep) if fts: if cond.field == 'kanji': table = 'kanjis_fts' elif cond.field == 'reading': table = 'readings_fts' elif cond.field == 'gloss': table = 'glosses_fts' else: if cond.field == 'kanji': table = 'kanjis' elif cond.field == 'reading': table = 'readings' elif cond.field == 'gloss': table = 'glosses' where_extra = '' if cond.regexp or (not fts and cond.extent == 'word'): # case sensitivity set for operator in opendb() operator = 'REGEXP ?' if cond.extent == 'whole': query_s = '^' + query_s + '$' elif cond.extent == 'beginning': query_s = '^' + query_s elif cond.extent == 'word': query_s = r'\b' + query_s + r'\b' else: if fts: operator = 'MATCH ?' else: if cond.extent == 'whole': operator = '= ?' query_s = query_s.replace('\\', '\\\\') if cond.case_sensitive and cond.field == 'gloss': where_extra = 'COLLATE BINARY' else: # extent = 'partial # "\" seems to be the least common character in EDICT. operator = r"LIKE ? ESCAPE '\'" # my editor doesn't like raw strings # query_s = query_s.replace(r'\', r'\\') query_s = query_s.replace('\\', '\\\\') query_s = query_s.replace('%', r'\%') query_s = query_s.replace('_', r'\_') query_s = '%' + query_s + '%' if cond.frequent: where_extra += ' AND %s.frequent = 1' % table database.execute(cur, ''' SELECT DISTINCT ent_seq FROM %s WHERE %s %s %s ;''' % (table, cond.field, operator, where_extra), [query_s]) res = [] for row in cur.fetchall(): res.append(row[0]) return res
def fetch_entry(cur, ent_seq): '''Return Entry object..''' kanjis = [] readings = [] senses = [] database.execute(cur, '''SELECT kanji_id, kanji, ke_inf, frequent FROM kanjis WHERE ent_seq = ?;''', [ent_seq]) for row in cur.fetchall(): kanjis.append(Kanji( kanji_id=row[0], text=row[1], ke_inf=row[2], frequent=row[3], )) database.execute(cur, '''SELECT reading_id, reading, re_nokanji, frequent, re_inf FROM readings WHERE ent_seq = ?;''', [ent_seq]) for row in cur.fetchall(): reading = Reading( reading_id=row[0], text=row[1], re_nokanji=row[2], frequent=row[3], re_inf=row[4], ) database.execute(cur, '''SELECT re_restr FROM reading_restrictions WHERE reading_id = ?;''', [reading.reading_id]) for row in cur.fetchall(): reading.re_restr.append(row[0]) readings.append(reading) senses = [] database.execute(cur, '''SELECT sense_id, pos, field, misc, dial, s_inf FROM senses WHERE ent_seq = ?;''', [ent_seq] ) for row in cur.fetchall(): sense = Sense(sense_id=row[0], pos=row[1], field=row[2], misc=row[3], dial=row[4], s_inf=row[5]) database.execute(cur, ''' SELECT stagk FROM sense_kanji_restrictions WHERE sense_id = ?; ''', [sense.sense_id] ) for row in cur.fetchall(): sense.stagk.append(row[0]) database.execute(cur, ''' SELECT stagr FROM sense_reading_restrictions WHERE sense_id = ?; ''', [sense.sense_id] ) for row in cur.fetchall(): sense.stagr.append(row[0]) database.execute(cur, 'SELECT gloss FROM glosses WHERE sense_id = ?;', [sense.sense_id]) for row in cur.fetchall(): sense.glosses.append(row[0]) senses.append(sense) database.execute(cur, 'SELECT frequent FROM entries WHERE ent_seq = ?;', [ent_seq]) frequent=cur.fetchone()[0] return Entry(ent_seq=ent_seq, frequent=frequent, kanjis=kanjis, readings=readings, senses=senses, )
def fetch_entry(cur, ent_seq): '''Return Entry object..''' kanjis = [] readings = [] senses = [] database.execute( cur, '''SELECT kanji_id, kanji, ke_inf, frequent FROM kanjis WHERE ent_seq = ?;''', [ent_seq]) for row in cur.fetchall(): kanjis.append( Kanji( kanji_id=row[0], text=row[1], ke_inf=row[2], frequent=row[3], )) database.execute( cur, '''SELECT reading_id, reading, re_nokanji, frequent, re_inf FROM readings WHERE ent_seq = ?;''', [ent_seq]) for row in cur.fetchall(): reading = Reading( reading_id=row[0], text=row[1], re_nokanji=row[2], frequent=row[3], re_inf=row[4], ) database.execute( cur, '''SELECT re_restr FROM reading_restrictions WHERE reading_id = ?;''', [reading.reading_id]) for row in cur.fetchall(): reading.re_restr.append(row[0]) readings.append(reading) senses = [] database.execute( cur, '''SELECT sense_id, pos, field, misc, dial, s_inf FROM senses WHERE ent_seq = ?;''', [ent_seq]) for row in cur.fetchall(): sense = Sense(sense_id=row[0], pos=row[1], field=row[2], misc=row[3], dial=row[4], s_inf=row[5]) database.execute( cur, ''' SELECT stagk FROM sense_kanji_restrictions WHERE sense_id = ?; ''', [sense.sense_id]) for row in cur.fetchall(): sense.stagk.append(row[0]) database.execute( cur, ''' SELECT stagr FROM sense_reading_restrictions WHERE sense_id = ?; ''', [sense.sense_id]) for row in cur.fetchall(): sense.stagr.append(row[0]) database.execute(cur, 'SELECT gloss FROM glosses WHERE sense_id = ?;', [sense.sense_id]) for row in cur.fetchall(): sense.glosses.append(row[0]) senses.append(sense) database.execute(cur, 'SELECT frequent FROM entries WHERE ent_seq = ?;', [ent_seq]) frequent = cur.fetchone()[0] return Entry( ent_seq=ent_seq, frequent=frequent, kanjis=kanjis, readings=readings, senses=senses, )
def version(cur): import argparse import romkan # import termcolor # no version import platform try: import psutil psutil_version = psutil.__version__ except ImportError: psutil_version = None if cur: from myougiden import database database.execute( cur, ''' SELECT dbversion, jmdict_mtime FROM versions; ''') row = cur.fetchone() dbversion = fmt(row[0], 'parameter') jmdict_mtime = fmt(row[1], 'parameter') else: dbversion = fmt('Database not found!', 'error') jmdict_mtime = fmt('Database not found!', 'error') if config: version = fmt(config.get('core', 'version'), 'parameter') prefix = fmt(config.get('paths', 'prefix'), 'parameter') else: version = fmt('Config not found!', 'error') prefix = fmt('Config not found!', 'error') scripts = {} for s in ('gzip', 'rsync', 'nice', 'ionice'): path = which(s) if path: scripts[s] = fmt(path, 'parameter') else: scripts[s] = fmt(path, 'warning') return (''' myougiden version %s , database v. %s JMdict last modified %s Python version %s (%s) Prefix: %s Libraries: romkan: %s argparse: %s psutil: %s External programs: gzip: %s rsync: %s nice: %s ionice: %s '''.strip() % ( version, dbversion, jmdict_mtime, fmt(platform.python_version(), 'parameter'), fmt(platform.platform(), 'parameter'), prefix, fmt(romkan.__version__, 'parameter'), # fmt(termcolor.__version__, 'parameter'), fmt(argparse.__version__, 'parameter'), fmt(psutil_version, 'parameter'), scripts['gzip'], scripts['rsync'], scripts['nice'], scripts['ionice'], ))
def version(cur): import argparse import romkan # import termcolor # no version import platform try: import psutil psutil_version = psutil.__version__ except ImportError: psutil_version = None if cur: from myougiden import database database.execute(cur, ''' SELECT dbversion, jmdict_mtime FROM versions; ''') row = cur.fetchone() dbversion = fmt(row[0], 'parameter') jmdict_mtime = fmt(row[1], 'parameter') else: dbversion = fmt('Database not found!', 'error') jmdict_mtime = fmt('Database not found!', 'error') if config: version = fmt(config.get('core','version'), 'parameter') prefix = fmt(config.get('paths','prefix'), 'parameter') else: version = fmt('Config not found!', 'error') prefix = fmt('Config not found!', 'error') scripts = {} for s in ('gzip', 'rsync', 'nice', 'ionice'): path = which(s) if path: scripts[s] = fmt(path, 'parameter') else: scripts[s] = fmt(path, 'warning') return (''' myougiden version %s , database v. %s JMdict last modified %s Python version %s (%s) Prefix: %s Libraries: romkan: %s argparse: %s psutil: %s External programs: gzip: %s rsync: %s nice: %s ionice: %s '''.strip() % ( version, dbversion, jmdict_mtime, fmt(platform.python_version(), 'parameter'), fmt(platform.platform(), 'parameter'), prefix, fmt(romkan.__version__, 'parameter'), # fmt(termcolor.__version__, 'parameter'), fmt(argparse.__version__, 'parameter'), fmt(psutil_version, 'parameter'), scripts['gzip'], scripts['rsync'], scripts['nice'], scripts['ionice'], ))
def search_by(cur, cond): '''Main search function. Take a SearchCondition object, return list of ent_seqs. ''' if ((cond.field == 'gloss' and cond.case_sensitive) or cond.extent in ('whole', 'partial')): fts=False query_s = cond.query_s[:] else: fts=True query_prep = [] for cmdline_arg in cond.query: # unify Japanese spaces, newlines etc. to a single space re.sub('\\s+', ' ', cmdline_arg) # if the user called # # myougiden 'full phrase' # # or # myougiden "full phrase" # # we'll get a single argv with spaces in it. in this case we # translate the argv to the string # # "full phrase" # # (including the double quotes), which is what sqlite FTS looks # for. if ' ' in cmdline_arg: cmdline_arg = '"' + cmdline_arg + '"' if cond.extent == 'beginning': cmdline_arg += '*' query_prep.append(cmdline_arg) query_s = ' '.join(query_prep) if fts: if cond.field == 'kanji': table = 'kanjis_fts' elif cond.field == 'reading': table = 'readings_fts' elif cond.field == 'gloss': table = 'glosses_fts' else: if cond.field == 'kanji': table = 'kanjis' elif cond.field == 'reading': table = 'readings' elif cond.field == 'gloss': table = 'glosses' where_extra = '' if cond.regexp or (not fts and cond.extent == 'word'): # case sensitivity set for operator in opendb() operator = 'REGEXP ?' if cond.extent == 'whole': query_s = '^' + query_s + '$' elif cond.extent == 'beginning': query_s = '^' + query_s elif cond.extent == 'word': query_s = r'\b' + query_s + r'\b' else: if fts: operator = 'MATCH ?' else: if cond.extent == 'whole': operator = '= ?' query_s = query_s.replace('\\', '\\\\') if cond.case_sensitive and cond.field == 'gloss': where_extra = 'COLLATE BINARY'; else: # extent = 'partial # "\" seems to be the least common character in EDICT. operator = r"LIKE ? ESCAPE '\'" # my editor doesn't like raw strings # query_s = query_s.replace(r'\', r'\\') query_s = query_s.replace('\\', '\\\\') query_s = query_s.replace('%', r'\%') query_s = query_s.replace('_', r'\_') query_s = '%' + query_s + '%' if cond.frequent: where_extra += ' AND %s.frequent = 1' % table database.execute(cur, ''' SELECT DISTINCT ent_seq FROM %s WHERE %s %s %s ;''' % (table, cond.field, operator, where_extra), [query_s]) res = [] for row in cur.fetchall(): res.append(row[0]) return res