def addItemsToDbJlpt(self, jlptGrade): success = False try: jlptGrade = int(jlptGrade) if 0 < jlptGrade < 5: selection = self.db.character.filter(self.db.character.jlpt==jlptGrade).all() #time for next quiz now = datetime.now() jlpt = u'jlpt' + str(jlptGrade) for kanji in selection: #check if already exists (time consuming?) #if len(Kanji.query.filter_by(character = kanji.literal).all()) == 0: if session.query(Kanji).filter_by(character = kanji.literal).count() == 0: # for easier management Kanji(character = kanji.literal, tags = jlpt, next_quiz = now, leitner_grade = Leitner.grades.None.index, active = True, current_session = False, been_in_session = 0) success = True try: session.commit() except IntegrityError: session.rollback() #is it ok in case just one item err'd? success = False except ValueError, e: log.error(e)
def initializeCurrentSession(self, mode, sessionSize): #if mode == 'kanji': if mode == modes.kanji: selection = Kanji.query.filter_by(active = True).all() #elif mode == 'words': elif mode == modes.words: selection = Word.query.filter_by(active = True).all() else: selection = () # Kanji && Words? self.endCurrentSesion() n = sessionSize shuffle(selection) if n > len(selection) : n = len(selection) # without any try's and ValueErrors random_selection = sample(selection, n) # serious stuff ahead! for item in random_selection: # mark n random items as 'current' item.current_session = True item.been_in_session = item.been_in_session + 1 #may be useful later on try: session.commit() except Exception, e: session.rollback() log.error(e)
def parseReadings(self): items_grouped = {} for kanji in self.items: readings = {} try: lookup = self.kjd[kanji.character] for kun in lookup.kun_readings: kun = kun.replace('.', '').replace('-', '') for word in kanji.word: if kun in kata2hira(MecabTool.parseToReadingsKana(word.word)[0]): if readings.has_key(kun): readings[kun].append(word.word) else: readings[kun] = [word.word] for on in lookup.on_readings: on = kata2hira(on.replace('.', '').replace('-', '')) for word in kanji.word: if on in kata2hira(MecabTool.parseToReadingsKana(word.word)[0]): if readings.has_key(on): readings[on].append(word.word) else: readings[on] = [word.word] except Exception, e: log.error(e) # simple solution - difficult implementation # items_grouped[kanji.character] = readings # slightly more complicated solution - easier implementation for reading in readings: i = 0 if items_grouped.has_key(kanji.character): items_grouped[kanji.character + '_' + str(i)] = (reading, readings[reading]); i += 1 else: items_grouped[kanji.character] = (reading, readings[reading])
def addSentenceToDb(self, kanji, sentence, translation): Kanji.query.filter_by(character = kanji).one().example.append(Example(sentence = sentence,translation = translation)) try: session.commit() except Exception, e: session.rollback() log.error(e)
def toggleActive(self, item): item.active = not item.active item.current_session = not item.current_session try: session.commit() except Exception, e: session.rollback() log.error(e)
def addExamplesForKanji(self, kanji, examples): for example in examples: kanji.example.append(Example(sentence = example,translation = examples[example])) try: session.commit() except Exception, e: session.rollback() log.error(e)
def getBijinImages(name, limit): url = BEHOIMI_MAIN + str(limit) + BEHOIMI_QUERY + name try: responseTree = etree.parse(StringIO.StringIO(urllib2.urlopen(url).read())) posts = responseTree.findall('post') for post in posts: image_url = post.get('file_url') image_name = image_url.split('/')[-1] # http://behoimi.org/data/ad/8b/ad8bf4363dd992abae9bb672bc499d4c.jpg # http://behoimi.org/data/sample/ad/8b/samplead8bf4363dd992abae9bb672bc499d4c.jpg #=============================================================== # post_url = BEHOIMI_POST + post.get('id') # # soup = BeautifulSoup(urllib2.urlopen(post_url)) # parsed = list(urlparse.urlparse(post_url)) # # out_folder = './' # # for image in soup.findAll("img"): # print "Image: %(src)s" % image # filename = image["src"].split("/")[-1] # parsed[2] = image["src"] # outpath = os.path.join(out_folder, filename) # try: # urlretrieve(urlparse.urlunparse(parsed), outpath) # except Exception, e: # print e #=============================================================== #=============================================================== # lookup = soup.findAll('li', text='Size: ') # if len(lookup) > 0: # image_url = lookup[0].next['href'] #=============================================================== image_name = image_url.split('/')[-1] folder_path = '../' + PATH_TO_RES + IMAGES + name + '/' file_path = folder_path + image_name if not os.path.exists(folder_path): os.makedirs(folder_path) if not os.path.exists(file_path): local_file = open(file_path, 'wb') local_file.write(urllib2.urlopen(image_url).read()) local_file.close() except Exception, e: log.error(e) return False #ImageGetter.getBijinImages('horikita_maki', 3)
def loadJmdictFromDumpRegex(self): try: dump = open(PATH_TO_RES + JMDICT_DUMP + '_rx', 'r') self.dictionaryR = pickle.load(dump) dump.close() return True except Exception, e: log.error(e) return False
def loadgroupsFromDump(self): try: dump = open(PATH_TO_RES + GROUPS_DUMP, 'r') self.groups = pickle.load(dump) dump.close() return True except Exception, e: log.error(e) return False
def addKanjiToDb(self, character): if(len(Kanji.query.filter_by(character = character).all()) == 0): Kanji(character = character, tags = u'user', next_quiz = datetime.now(), leitner_grade = Leitner.grades.None.index, active = True, current_session = False, been_in_session = 0) try: session.commit() except Exception, e: session.rollback() log.error(e)
def updateCompounds(self): compounds = u'' for compound in self.items[self.current_item][1]: compounds += compound + u'\t~\t' try: lookup = self.edict[compound] compounds += lookup.readings[0] + '<br/>' compounds += "<i><font style='font-family: Calibri; font-size: 11pt'>" + ", ".join(lookup.senses).rstrip(",(P)") + "</font></i><br/>" except Exception, e: log.error(e)
def addExamples(self, item, examples): '''Item is either kanji or word''' if item is not None: for example in examples: item.example.append(Example(sentence = example,translation = examples[example])) try: session.commit() except Exception, e: session.rollback() log.error(e)
def updateActive(self, criteria, flag): lookup = Kanji.query.filter(Kanji.tags.like(u'%' + criteria + '%' )).all() for item in lookup: item.active = flag try: session.commit() except Exception, e: session.rollback() log.error(e)
def addWordToDb(self, kanji, word): """Adds word corresponding to kanji from quiz/example""" if len(word) > 1: if(len(Word.query.filter_by(word = word).all()) == 0): kanji.word.append(Word(word = word, next_quiz = datetime.now(), leitner_grade = Leitner.grades.None.index, active = True, current_session = False, been_in_session = 0)) try: session.commit() except Exception, e: session.rollback() log.error(e)
def addWordToDbAndLinkToExample(self, kanji, word, sentence): if len(word) > 1: if(len(Word.query.filter_by(word = word).all()) == 0): newWord = Word(word = word, next_quiz = datetime.now(), leitner_grade = Leitner.grades.None.index, active = True, current_session = False, been_in_session = 0) kanji.word.append(newWord) sentence.word.append(newWord) try: session.commit() except Exception, e: session.rollback() log.error(e)
def clearDB(self): # Kanji.query.delete() # Word.query.delete() # Example.query.delete() #NB: for table in reversed(meta.Base.metadata.sorted_tables): meta.Session.execute(table.delete()); meta.Session.commit() try: for table in reversed(metadata.sorted_tables) : session.execute(table.delete()); session.commit() # session.commit() except Exception, e: session.rollback() log.error(e)
def endCurrentSesion(self): for kanji in Kanji.query.all(): kanji.current_session = False kanji.wrong_in_current_session = 0 for word in Word.query.all(): word.current_session = False word.wrong_in_current_session = 0 try: session.commit() except Exception, e: session.rollback() log.error(e)
def saveSessionStats(self, stats): stats.calculateActiveTime() try: current_session = Session.query.filter_by(date = date.today()).one() current_session.items_correct += stats.answeredCorrect current_session.items_wrong += (stats.totalItemSeen - stats.answeredCorrect) current_session.time_running += (datetime.now() - stats.startedQuiz).seconds current_session.time_active += stats.totalQuizActiveTime.seconds current_session.time_paused += stats.totalPauseTime.seconds current_session.times_launched += 1 except NoResultFound: Session(date = date.today(), items_correct = stats.answeredCorrect, items_wrong = (stats.totalItemSeen - stats.answeredCorrect), time_running = (datetime.now() - stats.startedQuiz).seconds, time_active = stats.totalQuizActiveTime.seconds, time_paused = stats.totalPauseTime.seconds, times_launched = 1) try: session.commit() except Exception, e: session.rollback() log.error(e)
def updateQuizItem(self, item): try: session.commit() except Exception, e: session.rollback() log.error(e)