def multi_regen(nid, src_fields, dst_fields, mecab): note = mw.col.getNote(nid) if "japanese" not in note.model()['name'].lower(): return src = None for fld in src_fields: if fld in note: src = fld break if not src: # no src field return dst = None for fld in dst_fields: if fld in note: dst = fld break if not dst: # no dst field return if note[dst]: # already contains data, skip return srcTxt = mw.col.media.strip(note[src]) if not srcTxt.strip(): return try: note[dst] = mecab.reading(srcTxt) except Exception, e: mecab = None raise
def onFocusLost(flag, n, fidx): if (n.model()["name"].startswith("JP Grammar")): if mw.col.models.fieldNames(n.model()).index("Text") == fidx: text = mw.col.media.strip(n["Text"]) if not nomecab: n["Reading"] = mecab.reading(text) return True return flag
def getCompleteDefinitionsForWord(targetWord): defs = shinmeikai.getDefsOfWord(targetWord, conf) sortedDefs = sortDefinitions(defs) optimallySelectedDefs = removeOverflowDefinitions(sortedDefs) formatedDef = defListToString(optimallySelectedDefs) if conf.addFuriganaToDef: formatedDef = mecab.reading(formatedDef) completeDefinitions = correctSpacing(formatedDef) return completeDefinitions
def _checkJapaneseReading(self, note, fieldmap): # check for a reading field reading_id = fieldmap.get('Reading', None) if reading_id: try: from japanese.reading import mecab note.fields[reading_id[0]] = mecab.reading(reading) except: self.status = (u'Error: Unable to generate the reading.' + u'Please install the Japanese Support Plugin.') return False return True
def test(): defs = shinmeikai.getDefsOfWord(sys.argv[1], conf) #print(defs) sortedDefs = sortDefinitions(defs) #print(sortedDefs) optimallySelectedDefs = removeOverflowDefinitions(sortedDefs) #print(optimallySelectedDefs) formatedDef = defListToString(optimallySelectedDefs) #print(formatedDef) if conf.addFuriganaToDef: formatedDef = mecab.reading(formatedDef) #print(formatedDef) return formatedDef
def regenerateReadings(factIds): mw.deck.startProgress(max=len(factIds)) for c, id in enumerate(factIds): mw.deck.updateProgress(label="Generating readings...", value=c) fact = mw.deck.s.query(Fact).get(id) try: for i in range(len(srcFields)): fact[dstFields[i]] = mecab.reading(fact[srcFields[i]]) except: pass mw.deck.refresh() mw.deck.updateCardQACacheFromIds(factIds, type="facts") mw.deck.finishProgress()
def getExamples(term): term = mecab.reading(term) answers = [] japA = [] engA = [] jap = eng = None for item in examplesA: #sys.stderr.write("Item:" + item['Expression'] + "\n") match = (item['Reading']).find(term) #sys.stderr.write("Match?:" + str(match) + "\n") if -1 != (item['Reading']).find(term): ###IF TERM IN IN EXPRESSION########## #sys.stderr.write("Found:" + item['Expression'] + "\n") japA.append(item['Reading']) engA.append(item['Meaning']) japA = japA[:MAX_EXAMPLES] engA = engA[:MAX_EXAMPLES] return EList(japA, engA)
def getExamples(term): term = mecab.reading(term) answers = [] japA = [] engA = [] jap = eng = None for item in examplesA: # sys.stderr.write("Item:" + item['Expression'] + "\n") match = (item["Reading"]).find(term) # sys.stderr.write("Match?:" + str(match) + "\n") if -1 != (item["Reading"]).find(term): ###IF TERM IN IN EXPRESSION########## # sys.stderr.write("Found:" + item['Expression'] + "\n") japA.append(item["Reading"]) engA.append(item["Meaning"]) japA = japA[:MAX_EXAMPLES] engA = engA[:MAX_EXAMPLES] return EList(japA, engA)
def onFocusLost(flag, n, fidx): if (n.model()["name"].startswith("JP Vocabulary")): if mw.col.models.fieldNames(n.model()).index("Kanji Reading") == fidx: keyword = mw.col.media.strip(n["Kanji Reading"]) if keyword != "": conkdb = sqlite3.connect("../../addons/myjp/data/jpdb") conkdb.row_factory = sqlite3.Row c = conkdb.cursor() keywords=(keyword,) if not nomecab: n["Reading (Mecab)"] = mecab.reading(keyword) c.execute("SELECT * FROM jpdic WHERE keyword=?",keywords) row = c.fetchone() for f in fields2dbMap: if n[f] == "": if f.startswith("K "): for i in range(0,len(keyword)): k = keyword[i] c.execute("SELECT * FROM kanji WHERE kanji=?",k) krow = c.fetchone() if krow and type(fields2dbMap[f]) == type([]): for k in fields2dbMap[f]: if k not in krow.keys() or krow[k] == "": continue else: if n[f] != "": n[f] +=", " n[f] += krow[k] break if krow and fields2dbMap[f] in krow.keys(): if n[f] != "": n[f] +=", " n[f] += krow[fields2dbMap[f]] elif row and type(fields2dbMap[f]) == type([]): for k in fields2dbMap[f]: if k not in row.keys() or row[k] == "": continue else: n[f] = row[k] break elif row and fields2dbMap[f] in row.keys(): n[f] = row[fields2dbMap[f]] return True return flag
def regenerateReadings(nids): global mecab mw.checkpoint("Bulk-add Readings") mw.progress.start() for nid in nids: note = mw.col.getNote(nid) # Amend notetypes.py to add your note types _noteName = note.model()['name'].lower() if not isActiveNoteType(_noteName): continue src = None for field in srcFields: if field in note: src = field break if not src: # no src field continue # dst is the destination field for the Readings dst = None for field in dstFields: if field in note: dst = field break if not dst: # no dst field continue if note[dst]: # already contains data, skip continue srcTxt = mw.col.media.strip(note[src]) if not srcTxt.strip(): continue try: note[dst] = mecab.reading(srcTxt) except Exception as e: mecab = None raise note.flush() mw.progress.finish() mw.reset()
def regenerateReadings(nids): global mecab mw.checkpoint("Bulk-add Readings") mw.progress.start() for nid in nids: note = mw.col.getNote(nid) # Amend notetypes.py to add your note types _noteName = note.model()['name'].lower() if not isJapaneseNoteType(_noteName): continue src = None for field in srcFields: if field in note: src = field break if not src: # no src field continue # dst is the destination field for the Readings dst = None for field in dstFields: if field in note: dst = field break if not dst: # no dst field continue if note[dst]: # already contains data, skip continue srcTxt = mw.col.media.strip(note[src]) if not srcTxt.strip(): continue try: note[dst] = mecab.reading(srcTxt) except Exception as e: mecab = None raise note.flush() mw.progress.finish() mw.reset()
def regenerateReadings(nids): global mecab mw.checkpoint("Bulk-add Readings") mw.progress.start() for nid in nids: note = mw.col.getNote(nid) if "japanese" not in note.model()['name'].lower(): continue src = None for fld in srcFields: if fld in note: src = fld break if not src: # no src field continue dst = None for fld in dstFields: if fld in note: dst = fld break if not dst: # no dst field continue if note[dst]: # already contains data, skip continue srcTxt = mw.col.media.strip(note[src]) if not srcTxt.strip(): continue try: note[dst] = mecab.reading(srcTxt) except Exception, e: mecab = None raise note.flush()
def regenerateReadings(nids): global mecab mw.checkpoint("Bulk-add Readings") mw.progress.start() for nid in nids: note = mw.col.getNote(nid) if "japanese" not in note.model()["name"].lower(): continue src = None for fld in srcFields: if fld in note: src = fld break if not src: # no src field continue dst = None for fld in dstFields: if fld in note: dst = fld break if not dst: # no dst field continue if note[dst]: # already contains data, skip continue srcTxt = mw.col.media.strip(note[src]) if not srcTxt.strip(): continue try: note[dst] = mecab.reading(srcTxt) except Exception, e: mecab = None raise note.flush()
#start mecab translator #mecab = japanese.reading.MecabController() global mecab #Add the data to the dst field try: japR, engR = getExamples(srcTxt) #sys.stderr.write("got jap:" +'<br>'.join(japR)) #sys.stderr.write("got eng:" +'<br>'.join(engR)) except Exception, e: raise #results #array of "jap" and "eng" hashs Examples = mecab.reading(('<br>'.join(japR)).decode('utf-8')) Questions = makeQuestions(srcTxt, ('<br>'.join(japR)).decode('utf-8')) English = ('<br>'.join(engR)).decode('utf-8') try: result = Examples except Exception, e: mecab = None raise note[dstField] = result #next make question #check if the fields exist dst = None
def createNote(self): # create the new note note = mw.col.newNote() # set the deck if not self.deck.strip(): note.model()['did'] = 1 else: note.model()['did'] = mw.col.decks.id(self.deck) # verify this is an Anki 2 cloze model if not note.model()['type'] == MODEL_CLOZE: self.status = u'Error: '+note.model()['name']+' is not a Cloze model.' return False # create a list of cloze candidates listClozes = self._generateClozeList() # grab part of the card for the status update excerpt = self.text[:10] excerpt = excerpt.replace(u'\n', u' ') if len(self.text) > 10: excerpt += u'...' # convert the newlines to html self.text = unicode.replace( self.text, '\n', '<br>' ) self.notes = unicode.replace( self.notes, '\n', '<br>' ) self.source = unicode.replace( self.source, '\n', '<br>' ) # save the text for the reading generation reading = self.text # pre-process all of the closes for i, clz in enumerate(listClozes): self.text = self._clozePrepare( self.text, clz, i+1 ) # finalize the clozes, this two stage process prevents errors with embedded clozes for i, clz in enumerate(listClozes): self.text = self._clozeFinalize( self.text, clz, i+1 ) # set the tags note.tags = mw.col.tags.split(self.tags) # deal with the source field if len(self.source): source_id = mw.col.models.fieldMap( note.model() ).get('Source', None) if source_id: note.fields[ source_id[0] ] = self.source else: self.notes = self.notes + u'<br><br>' + self.source # check for a reading field reading_id = mw.col.models.fieldMap( note.model() ).get('Reading', None) if reading_id: try: from japanese.reading import mecab note.fields[ reading_id[0] ] = mecab.reading(reading) except: self.status = u'Error: Unable to generate the reading. Please install the Japanese Support Plugin.' return False # fill in the note fields note.fields[0] = self.text note.fields[1] = self.notes # check for errors if note.dupeOrEmpty(): self.status = u'Error: Note is empty or a duplicate' return False cards = mw.col.addNote(note) # save the collection mw.col.autosave() # set the status self.status = u'Added a new note \'{0}\' with {1} {2}.'.format(excerpt, len(listClozes), u'cloze' if len(listClozes) == 1 else u'clozes') # return success return True
# define input (change this to the values you want) input = u""" 理髪店 蝶番い 度合い 大至急 気忙しい 啜る 芒洋 辻褄 """ expressions = input.split() output = "" # loop through expressions and construct the semicolon-delimited string for expression in expressions: output += expression output += ";" output += ";" #meaning is blank... for now... for character in expression: if rtk_dict.get(character): output += character + ": " + rtk_dict.get(character) + "<br>" # use <br> instead of newline because the newline delimits each entry instead output += ";" output += mecab.reading(expression) output += "\n" print output
def makeExamples(term, sentence): # sys.stderr.write("Trying to search on: " +term) # sys.stderr.write("\n" + sentence) term = mecab.reading(term) return re.sub(re.escape(term), "<span class=focusword>" + term + "</span>", sentence)
def createNote(self): # create the new note note = self.mw.col.newNote() # set the deck if not self.deck.strip(): note.model()['did'] = 1 else: note.model()['did'] = self.mw.col.decks.id(self.deck) # verify this is an Anki 2 cloze model if not note.model()['type'] == MODEL_CLOZE: self.status = u'Error: ' + note.model( )['name'] + ' is not a Cloze model.' return False # create a list of cloze candidates listClozes = self._generateClozeList() # grab part of the card for the status update excerpt = self.text[:10] excerpt = excerpt.replace(u'\n', u' ') if len(self.text) > 10: excerpt += u'...' # convert the newlines to html self.text = unicode.replace(self.text, '\n', '<br>') self.notes = unicode.replace(self.notes, '\n', '<br>') self.source = unicode.replace(self.source, '\n', '<br>') # save the text for the reading generation reading = self.text # pre-process all of the closes for i, clz in enumerate(listClozes): self.text = self._clozePrepare(self.text, clz, i + 1) # finalize the clozes, this two stage process prevents errors with embedded clozes for i, clz in enumerate(listClozes): self.text = self._clozeFinalize(self.text, clz, i + 1) # set the tags note.tags = self.mw.col.tags.split(self.tags) # deal with the source field if len(self.source): source_id = self.mw.col.models.fieldMap(note.model()).get( 'Source', None) if source_id: note.fields[source_id[0]] = self.source else: self.notes = self.notes + u'<br><br>' + self.source # check for a reading field reading_id = self.mw.col.models.fieldMap(note.model()).get( 'Reading', None) if reading_id: try: from japanese.reading import mecab note.fields[reading_id[0]] = mecab.reading(reading) except: self.status = u'Error: Unable to generate the reading. Please install the Japanese Support Plugin.' return False # fill in the note fields note.fields[0] = self.text note.fields[1] = self.notes # check for errors if note.dupeOrEmpty(): self.status = u'Error: Note is empty or a duplicate.' return False # add the new note cards = self.mw.col.addNote(note) if not cards: self.status = u'Error: This note was not able to generate any cards.' return False # flag the queue for reset self.mw.requireReset() # save the collection self.mw.col.autosave() # set the status self.status = u'Added a new note \'{0}\' with {1} {2}.'.format( excerpt, len(listClozes), u'cloze' if len(listClozes) == 1 else u'clozes') # return success return True
def makeQuestions(term, sentence): #sys.stderr.write("Trying to mecabsearch on: " +mecab.reading(term)) #sys.stderr.write("Trying to search on: " +term) #sys.stderr.write("\n" + sentence) sentence = re.sub(re.escape(mecab.reading(term)), "<span class=focusword>___</span>", sentence) return re.sub(re.escape(term), "<span class=focusword>___</span>", sentence)
def makeExamples(term, sentence): #sys.stderr.write("Trying to search on: " +term) #sys.stderr.write("\n" + sentence) term = mecab.reading(term) return re.sub(re.escape(term), "<span class=focusword>"+term+"</span>", sentence)
def makeQuestions(term, sentence): # sys.stderr.write("Trying to mecabsearch on: " +mecab.reading(term)) # sys.stderr.write("Trying to search on: " +term) # sys.stderr.write("\n" + sentence) sentence = re.sub(re.escape(mecab.reading(term)), "<span class=focusword>___</span>", sentence) return re.sub(re.escape(term), "<span class=focusword>___</span>", sentence)