Example #1
0
 def do_rdngs(self, elems, entr, fmap):
     if elems is None: return
     rdngs = getattr(entr, '_rdng', [])
     kanjs = getattr(entr, '_kanj', [])
     rdngs = []
     dupchk = {}
     for ord, elem in enumerate(elems):
         txt = elem.find('reb').text
         if not jdb.unique(txt, dupchk):
             self.warn("Duplicate reb text: '%s'" % txt)
             continue
         if not jdb.jstr_reb(txt):
             self.warn("reb text '%s' not kana." % txt)
         rdng = jdb.Rdng(rdng=ord + 1, txt=txt)
         self.do_kws(elem.findall('re_inf'), rdng, '_inf', 'RINF')
         for x in elem.findall('re_pri'):
             freqtuple = self.parse_freq(x.text, "re_pri")
             if not freqtuple: continue
             rlist = fmap[freqtuple][0]
             if not jdb.isin(rdng, rlist): rlist.append(rdng)
             else: self.freq_warn("Duplicate", rdng, None, x.text)
         nokanji = elem.find('re_nokanji')
         self.do_restr(elem.findall('re_restr'), rdng, kanjs, 'restr',
                       nokanji)
         self.do_audio(elem.findall("audio"), rdng, jdb.Rdngsnd)
         rdngs.append(rdng)
     if rdngs: entr._rdng = rdngs
Example #2
0
def doedit (entr, hist, cmd):
        # entr -- A jdb.Entr() instance to be edited. 
        # hist --  A jdb.Hist instance that will be edited (if the edit
        #   is to add a comment of refs.)
        # cmd -- A Cmd instance that describes changes to be made to entry.
        # 
        # Apply the change described by <cmd> to <entr> and /or <hist>.
        #
        # Should return True if <entr> or <hist> were actually changed,
        # False if not, but currently always retuns True.

        new = None
        if cmd.operand in ('kanj', 'rdng'): 
            tlist = getattr (entr, '_'+cmd.operand)
            if cmd.new:
                if cmd.operand == 'kanj': new = jdb.Kanj (txt=cmd.new)
                else: new = jdb.Rdng (txt=cmd.new)
            edit (tlist, 'txt', cmd.old, new or cmd.new, cmd.operand, cmd.old, cmd.new)
        elif cmd.operand == 'gloss':
            tlist = getattr (getattr (entr, '_sens')[cmd.sens-1], '_'+cmd.operand)
            if cmd.new: new = jdb.Gloss (txt=cmd.new, lang=jdb.KW.LANG['eng'].id, 
                                                      ginf=jdb.KW.GINF['equ'].id)
            edit (tlist, 'txt', cmd.old, new or cmd.new, cmd.operand, cmd.old, cmd.new)
        elif cmd.operand in ('pos','misc','fld','dial'):
            tlist = getattr (getattr (entr, '_sens')[cmd.sens-1], '_'+cmd.operand)
            new, old = kw2id (cmd.operand, cmd.new, cmd.old)
            edit (tlist, 'kw', old, new, cmd.operand, cmd.old, cmd.new)
        elif cmd.operand == 'entr':
            if cmd.cmd == 'del': entr.stat = jdb.KW.STAT['D'].id
        elif cmd.operand == 'comment': hist.notes = cmd.new
        elif cmd.operand == 'refs': hist.refs = cmd.new
        else: raise ValueError (cmd.operand)

        return True #FIXME: how to determine if no change was made to entry?
Example #3
0
def mkentr(jtxt, etxt):
    global Lnnum
    # Create an entry object to represent the "A" line text of the
    # example sentence.
    e = jdb.Entr(stat=KW.STAT_A, unap=False)
    e.srcnote = str(Lnnum)
    if jdb.jstr_reb(jtxt): e._rdng = [jdb.Rdng(txt=jtxt)]
    else: e._kanj = [jdb.Kanj(txt=jtxt)]
    e._sens = [
        jdb.Sens(
            _gloss=[jdb.Gloss(txt=etxt, ginf=KW.GINF_equ, lang=KW.LANG_eng)])
    ]
    return e
Example #4
0
def mkentr (jtxt, etxt, kwds):
        global Lnnum
          # Create an entry object to represent the "A" line text of the
          # example sentence.
        e = jdb.Entr (stat=KW.STAT_A, unap=False)
        e.srcnote = str (Lnnum)
          # Each @$kwds item is a 2-array consisting of the kw
          # id number and optionally a note string.
        kws = [x[0] for x in kwds]
        sens_note = "; ".join ([x[1] for x in kwds if len(x)>1]) or None
        if jdb.jstr_reb (jtxt): e._rdng = [jdb.Rdng (txt=jtxt)]
        else:                   e._kanj = [jdb.Kanj (txt=jtxt)]
        e._sens = [jdb.Sens (notes=sens_note,
                    _gloss=[jdb.Gloss (lang=KW.LANG_eng,
                                     ginf=KW.GINF_equ, txt=etxt)],
                    _misc=[jdb.Misc (kw=x) for x in kws])]
        return e
Example #5
0
def rmgroup(rmg, langs=None):
    rdngs = []
    glosses = []
    cinf = []
    dupchk = {}
    for x in rmg.findall('reading'):
        rtype = None
        rstat = None
        cinfrec = None
        for aname, aval in list(x.items()):
            if aname == 'r_type': rtype = aval
            if aname == 'on_type': rtype = aval
            if aname == 'r_status': rstat = aval
        if rtype in ('pinyin', 'korean_r', 'korean_h', 'vietnam'):
            if (rtype, x.text) in dupchk:
                warn("Duplicate reading ignored: %s, %s" % (rtype, x.text))
                continue
            dupchk[(rtype, x.text)] = True
            cinf.append(jdb.Cinf(kw=KW.CINF[rtype].id, value=x.text))
        elif rtype == 'ja_on' or rtype == 'ja_kun':
            if x.text in dupchk:
                warn('Duplicate reading ignored: %s' % x.text)
                continue
            dupchk[x.text] = True
            rdng = jdb.Rdng(txt=x.text, _inf=[])
            rdng._inf.append(
                jdb.Rinf(kw=KW.RINF[Xml2db.RINF.get(aval, aval)].id))
            if rstat:
                rdng._inf.append(
                    jdb.Rinf(kw=KW.RINF[Xml2db.RINF.get(rstat, rstat)].id))
            rdngs.append(rdng)
        else:
            raise KeyError('Unkown r_type attribute: %s' % rtype)

    dupchk = {}
    for x in rmg.findall('meaning'):
        lang = x.get('m_lang', 'en')
        langkw = KW.LANG[Xml2db.LANG.get(lang, lang)].id
        if (lang, x.text) in dupchk:
            warn("Duplicate lang,meaning pair ignored: %s:%s" % (lang, x.text))
            continue
        dupchk[(lang, x.text)] = True
        if not langs or langkw in langs:
            glosses.append(jdb.Gloss(txt=x.text, lang=langkw, ginf=1))
    return rdngs, glosses, cinf
Example #6
0
def reading_meaning(rm, rdng, sens, cinf, langs):
    KW_NANORI = KW.RINF[Xml2db.RINF.get('nanori', 'nanori')].id
    for x in rm.findall('rmgroup'):
        r, g, c = rmgroup(x, langs)
        rdng.extend(r)
        sens.append(jdb.Sens(_gloss=g))
    # Make a dict keyed by the readings already parsed.
    rlookup = dict([(r.txt, r) for r in rdng])
    # Get the nanori readings...
    for x in rm.findall('nanori'):
        # There may be nanori readings that are the same as
        # the on/kun readings we've already parsed.  Lookup
        # the nanori reading in the readings dict.  If we
        # already have the reading, just add the nanori RINF
        # tag to it.  Otherwise create a new reading record.
        try:
            # Check if reading has already been seen.
            r = rlookup[x.text]
            # It has.  See if it occured as a nanori reading.
            wasnanori = False
            for i in getattr(r, '_inf', []):
                if i.kw == KW_NANORI:
                    wasnanori = True
                    break
            # It occured previously as a nanori reading so this
            # instance must be a duplicate.
            if wasnanori:
                warn('Duplicate nanori reading: "%s"' % x.text)
                continue
            # At this point, the nanori reading occured previously
            # but as a jp-on or jp-kun reading.  'r' is set to
            # that previous reading, and we will (below) just
            # add a nanori tag to 'r'.
        except KeyError:
            # This nanori reading has not been seen before.
            # Create a new Rdng object for it.
            r = jdb.Rdng(txt=x.text)
            rdng.append(r)
            # Add it to the previously seen readings dict.
            rlookup[r.txt] = r
        if not hasattr(r, '_inf'): r._inf = []
        r._inf.append(jdb.Rinf(kw=KW_NANORI))
    cinf.extend(c)
Example #7
0
def p_rdngitem_2(p):
    '''rdngitem : krtext taglists'''
    rdng = jdb.Rdng(txt=p[1])
    err = bld_rdng (rdng, p[2])
    if err: perror (p, err)
    p[0] = rdng
Example #8
0
def p_rdngitem_1(p):
    '''rdngitem : krtext'''
    p[0] = jdb.Rdng(txt=p[1])