def lookup(surface): items = latindic.lookup(surface) if items: return Word(surface, items) if char.isupper(surface[0]): surface_lower = char.tolower(surface) items = latindic.lookup(surface_lower) if items: return Word(surface, items) if surface[-3:] == u'que': items = latindic.lookup(surface[:-3]) if items: return Word(surface[:-3], items, {'enclitic':'que'}) return None
def do_command(line, options=None): fs = line.split(' ') cmd = fs[0] def surface_tr(): surface = ' '.join(fs[1:]) if options and options.capital_to_macron_mode: surface = char.trans(surface) return surface # 辞書検索 if cmd in ('l', 'lookup'): surface = surface_tr() print "lookup", surface, items = latindic.lookup(surface.decode('utf-8')) util.pp(items) # 動詞の活用を見る elif cmd in ('c', 'conjug'): surface_uc = surface_tr().decode('utf-8') table = {} ja = None moods = set() voices = set() tenses = set() for word, items in latindic.LatinDic.dic.items(): for item in items: pres1sg = item.get('pres1sg', None) if pres1sg == surface_uc: if not ja: ja = item['ja'] mood = item.get('mood', '-') moods.add(mood) voice = item.get('voice', '-') voices.add(mood + voice) tense = item.get('tense', '-') tenses.add(mood + voice + tense) person = item.get('person', None) number = item.get('number', None) key = (mood,voice,tense,person,number) item_surface = item['surface'] if table.has_key(key): table[key].append(item_surface) else: table[key] = [item_surface] def surfaces(key): if table.has_key(key): return ', '.join([surface.encode('utf-8') for surface in table[key]]) else: return '-' print "%s, %s" % (surface_uc.encode('utf-8'), ja) for mood in ['indicative', 'subjunctive', 'imperative']: if mood not in moods: continue print " %s" % mood for voice in ['active', 'passive']: if mood + voice not in voices: continue print " %s" % voice for tense in ['present', 'imperfect', 'future', 'perfect', 'past-perfect', 'future-perfect']: if mood + voice + tense not in tenses: continue print " %s" % tense for number in ['sg','pl']: print " %s" % number for person in [1,2,3]: key = (mood, voice, tense, person, number) if table.has_key(key): print " %d: %s" % (person, surfaces(key)) print " infinitive" print " present: %s" % surfaces(('infinitive','active','present',None,None)) print " perfect: %s" % surfaces(('infinitive','active','perfect',None,None)) print " future: %s" % surfaces(('infinitive','active','future',None,None)) # 名詞の変化形を見る elif cmd in ('d', 'decl'): surface_uc = surface_tr().decode('utf-8') table = {} base = None ja = None pos = None gender = None for word, items in latindic.LatinDic.dic.items(): for item in items: item_base = item.get('base', None) if item_base == surface_uc: for case, number, gender in item['_']: key = (case,number,gender) item_surface = item['surface'] if table.has_key(item_surface): table[key].append(item_surface) else: table[key] = [item_surface] if not pos: pos = item['pos'] gender = item['_'][0][2] base = item['base'] ja = item['ja'] def surfaces(key): if table.has_key(key): return ', '.join([surface.encode('utf-8') for surface in table[key]]) else: return '-' if pos == 'noun': print "%s (%s, %s), %s" % (base.encode('utf-8'), pos, gender, ja) for number in ['sg', 'pl']: print " %s:" % number for case in ['Nom', 'Voc', 'Acc', 'Gen', 'Dat', 'Abl', 'Loc']: key = (case, number, gender) if table.has_key(key): print " %s: %s" % (case, surfaces(key)) else: print "COMMAND NOT SUPPORTED: %s, with \"%s\"" % (cmd, surface_tr())
def translate(token): # print "translating \"%s\"..." % token.encode('utf-8') items = latindic.lookup(token) return items[0]['ja'] if items else '*'