Esempio n. 1
0
    def do_senss (self, elems, entr, xlit=False, xlang=None):
        XKW = self.XKW
        rdngs = getattr (entr, '_rdng', [])
        kanjs = getattr (entr, '_kanj', [])
        senss = [];  last_pos = None
        for ord, elem in enumerate (elems):
            sens = jdb.Sens (sens=ord+1)
            snotes = elem.find ('s_inf')
            if snotes is not None and snotes.text: sens.notes = snotes.text

            pelems = elem.findall('pos')
            if pelems:
                last_pos = self.do_kws (pelems, sens, '_pos', 'POS')
            elif last_pos:
                sens._pos = [jdb.Pos(kw=x.kw) for x in last_pos]

            self.do_kws   (elem.findall('name_type'), sens, '_misc', 'NAME_TYPE')
            self.do_kws   (elem.findall('misc'),      sens, '_misc', 'MISC')
            self.do_kws   (elem.findall('field'),     sens, '_fld',  'FLD')
            self.do_kws   (elem.findall('dial'),      sens, '_dial', 'DIAL')
            self.do_lsrc  (elem.findall('lsource'),   sens,)
            self.do_gloss (elem.findall('gloss'),     sens, xlit, xlang)
            self.do_gloss (elem.findall('trans_det'), sens,)
            self.do_restr (elem.findall('stagr'),     sens, rdngs, 'stagr')
            self.do_restr (elem.findall('stagk'),     sens, kanjs, 'stagk')
            self.do_xref  (elem.findall('xref'),      sens, jdb.KW.XREF['see'].id)
            self.do_xref  (elem.findall('ant'),       sens, jdb.KW.XREF['ant'].id)

            if not getattr (sens, '_gloss', None):
                self.warn ("Sense %d has no glosses." % (ord+1))
            senss.append (sens)
        if senss: entr._sens = senss
Esempio n. 2
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
Esempio n. 3
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
Esempio n. 4
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)
Esempio n. 5
0
def p_sense_1(p):
    '''sense : SNUM glosses'''
    sens = jdb.Sens()
    err = bld_sens (sens, p[2])
    if err: perror (p, "Unable to build sense %s\n%s" % (p[1], err))
    p[0] = sens
Esempio n. 6
0
def main(args, opts):
    jdb.reset_encoding(sys.stdout, 'utf-8')
    errs = []
    entrs = []
    try:
        form, svc, dbg, cur, sid, sess, parms, cfg = jmcgi.parseform()
    except Exception as e:
        jmcgi.err_page([str(e)])

    fv = form.getfirst
    fl = form.getlist
    is_editor = jmcgi.is_editor(sess)
    dbg = fv('dbg')
    meth = fv('meth')
    def_corp = fv('c')  # Default corpus for new entries.
    defcorpid = None
    if def_corp:
        try:
            def_corp = int(def_corp)
        except ValueError:
            pass
        try:
            defcorpid = jdb.KW.SRC[def_corp].id
        except KeyError:
            errs.append("Bad url parameter: c=%s" % def_corp)
    force_corp = fv('f')  # Force default corpus for new entries.

    sentrs = fl("entr")
    for sentr in sentrs:
        try:
            entrs = serialize.unserialize(sentr)
        except Exception as e:
            errs.append("Bad 'entr' value, unable to unserialize: %s" % str(e))
        else:
            entrs.append(entr)

    jentrs = fl('j')
    for jentr in jentrs:
        try:
            entr = edparse.entr(jentr)
        except Exception as e:
            errs.append("Bad 'j' value, unable to parse: %s" % str(e))
        else:
            entr.src = None
            entrs.append(entr)

    elist, qlist, active = fl('e'), fl('q'), fv('a')
    if elist or qlist:
        entrs.extend(
            jmcgi.get_entrs(cur,
                            elist or [],
                            qlist or [],
                            errs,
                            active=active,
                            corpus=def_corp) or [])
    cur.close()

    if (elist or qlist or jentrs or sentrs) and not entrs:
        # The caller explictly specified and entry to edit but we
        # didn't find it (or them).  Rather than treating this as
        # though no entries were given and displaying a blank edit
        # form, show an error message.
        errs.append("No matching entries were found")
    if errs: jmcgi.err_page(errs)

    srcs = sorted(jdb.KW.recs('SRC'), key=lambda x: x.kw.lower())
    #srcs.insert (0, jdb.Obj (id=0, kw='', descr=''))
    if not entrs:
        # This is a blank new entry.
        # The following dummy entry will produce the default
        # text for new entries: no kanji, no reading, and sense
        # text "[1][n]".
        entr = jdb.Entr(
            _sens=[jdb.Sens(_pos=[jdb.Pos(kw=jdb.KW.POS['n'].id)])], src=None)
        entrs = [entr]
    for e in entrs:
        if not is_editor: remove_freqs(e)
        e.ISDELETE = (e.stat == jdb.KW.STAT['D'].id) or None
        # Provide a default corpus.
        if not e.src: e.src = defcorpid
        e.NOCORPOPT = force_corp

    if errs: jmcgi.err_page(errs)

    for e in entrs:
        e.ktxt = fmtjel.kanjs(e._kanj)
        e.rtxt = fmtjel.rdngs(e._rdng, e._kanj)
        e.stxt = fmtjel.senss(e._sens, e._kanj, e._rdng)

    if errs: jmcgi.err_page(errs)

    jmcgi.jinja_page('edform.jinja',
                     parms=parms,
                     extra={},
                     entrs=entrs,
                     srcs=srcs,
                     is_editor=is_editor,
                     svc=svc,
                     dbg=dbg,
                     sid=sid,
                     session=sess,
                     cfg=cfg,
                     this_page='edform.py')