def get_possible_translations(from_lang_code, to_lang_code): """ Returns a list of possible translations for this :param word: word to be translated :param from_lang_code: :param to_lang_code: :return: json array with dictionaries. each of the dictionaries contains at least one 'translation' and one 'translation_id' key. In the future we envision that the dict will contain other types of information, such as relative frequency, """ translations_json = [] context = request.form.get('context', '') url = request.form.get('url', '') word = request.form['word'] main_translation, alternatives = zeeguu.api.translation_service.translate_from_to(word, from_lang_code, to_lang_code) lan = Language.find(from_lang_code) likelihood = 1.0 for translation in alternatives: wor = UserWord.find(translation, lan) zeeguu.db.session.add(wor) zeeguu.db.session.commit() t_dict = dict(translation_id= wor.id, translation=translation, likelihood=likelihood) translations_json.append(t_dict) likelihood -= 0.01 return json_result(dict(translations=translations_json))
def get_possible_translations(from_lang_code, to_lang_code): """ Returns a list of possible translations for this :param word: word to be translated :param from_lang_code: :param to_lang_code: :return: json array with dictionaries. each of the dictionaries contains at least one 'translation' and one 'translation_id' key. In the future we envision that the dict will contain other types of information, such as relative frequency, """ translations_json = [] context = request.form.get('context', '') url = request.form.get('url', '') word = request.form['word'] main_translation, alternatives = zeeguu.api.translation_service.translate_from_to( word, from_lang_code, to_lang_code) lan = Language.find(from_lang_code) likelihood = 1.0 for translation in alternatives: wor = UserWord.find(translation, lan) zeeguu.db.session.add(wor) zeeguu.db.session.commit() t_dict = dict(translation_id=wor.id, translation=translation, likelihood=likelihood) translations_json.append(t_dict) likelihood -= 0.01 return json_result(dict(translations=translations_json))
def add_bookmark(user, original_language, original_word, translation_language, translation_word, date, the_context, the_url, the_url_title): url = Url.find (the_url) text = Text.find_or_create(the_context, translation_language, url) origin = UserWord.find(original_word.lower(), original_language) translation = UserWord.find(translation_word.lower(), translation_language) zeeguu.db.session.add(url) zeeguu.db.session.add(text) zeeguu.db.session.add(origin) zeeguu.db.session.add(translation) t1= Bookmark(origin, translation, user, text, date) zeeguu.db.session.add(t1) zeeguu.db.session.commit() add_probability_to_existing_words_of_user(user,t1,original_language)
def bookmark_with_context(from_lang_code, to_lang_code, word_str, url_str, title_str, context_str, translation_str): """ This function will lookup a given word-text pair, and if found, it will return that bookmark rather than a new one :param from_lang_code: :param to_lang_code: :param word_str: :param url_str: :param title_str: :param context_str: :param translation_str: :return: """ from_lang = Language.find(from_lang_code) to_lang = Language.find(to_lang_code) user_word = UserWord.find(word_str, from_lang) url = Url.find(url_str, title_str) zeeguu.db.session.add(url) zeeguu.db.session.commit() context = Text.find_or_create(context_str, from_lang, url) zeeguu.db.session.add(context) zeeguu.db.session.commit() translation = UserWord.find(translation_str, to_lang) try: bookmark = Bookmark.find_all_by_user_word_and_text( flask.g.user, user_word, context)[0] # TODO: Think about updating the date of this bookmark, or maybe creating a duplicate # otherwise, in the history this translation will not be visible! except Exception: bookmark = Bookmark(user_word, translation, flask.g.user, context, datetime.now()) zeeguu.db.session.add(bookmark) bookmark.calculate_probabilities_after_adding_a_bookmark( flask.g.user, bookmark.origin.language) zeeguu.db.session.commit() return str(bookmark.id)
def bookmark_with_context(from_lang_code, to_lang_code, word_str, url_str, title_str, context_str, translation_str): """ This function will lookup a given word-text pair, and if found, it will return that bookmark rather than a new one :param from_lang_code: :param to_lang_code: :param word_str: :param url_str: :param title_str: :param context_str: :param translation_str: :return: """ from_lang = Language.find(from_lang_code) to_lang = Language.find(to_lang_code) user_word = UserWord.find(word_str, from_lang) url = Url.find(url_str, title_str) zeeguu.db.session.add(url) zeeguu.db.session.commit() context = Text.find_or_create(context_str, from_lang, url) zeeguu.db.session.add(context) zeeguu.db.session.commit() translation = UserWord.find(translation_str, to_lang) try: bookmark = Bookmark.find_all_by_user_word_and_text(flask.g.user, user_word, context)[0] # TODO: Think about updating the date of this bookmark, or maybe creating a duplicate # otherwise, in the history this translation will not be visible! except Exception: bookmark = Bookmark(user_word, translation, flask.g.user, context, datetime.now()) zeeguu.db.session.add(bookmark) bookmark.calculate_probabilities_after_adding_a_bookmark(flask.g.user, bookmark.origin.language) zeeguu.db.session.commit() return str(bookmark.id)
def add_new_translation_to_bookmark(word_translation, bookmark_id): bookmark = Bookmark.query.filter_by(id=bookmark_id).first() translations_of_bookmark = bookmark.translations_list for transl in translations_of_bookmark: if transl.word == word_translation: return 'FAIL' translation_user_word = UserWord.find(word_translation, translations_of_bookmark[0].language) bookmark.add_new_translation(translation_user_word) zeeguu.db.session.add(translation_user_word) zeeguu.db.session.commit() return "OK"
def set_know_word_prob(): zeeguu.app.test_request_context().push() zeeguu.db.session.commit() enc_probs = EncounterBasedProbability.find_all() ex_probs = ExerciseBasedProbability.find_all() for prob in enc_probs: user = prob.user word = prob.ranked_word.word language = prob.ranked_word.language user_word = None if UserWord.exists(word, language): user_word = UserWord.find(word, language) if ExerciseBasedProbability.exists(user, user_word): ex_prob = ExerciseBasedProbability.find(user, user_word) known_word_prob = KnownWordProbability.calculateKnownWordProb( ex_prob.probability, prob.probability) known_word_probability_obj = KnownWordProbability.find( user, user_word, prob.ranked_word, known_word_prob) else: known_word_probability_obj = KnownWordProbability.find( user, None, prob.ranked_word, prob.probability) zeeguu.db.session.add(known_word_probability_obj) zeeguu.db.session.commit() for prob in ex_probs: user = prob.user language = prob.user_word.language word = prob.user_word.word ranked_word = None if RankedWord.exists(word, language): ranked_word = RankedWord.find(word, language) if not EncounterBasedProbability.exists(user, ranked_word): if UserWord.exists(word, language): user_word = UserWord.find(word, language) known_word_probability_obj = KnownWordProbability( user, user_word, ranked_word, prob.probability) zeeguu.db.session.add(known_word_probability_obj) zeeguu.db.session.commit() print('job3')
def add_new_translation_to_bookmark(word_translation, bookmark_id): bookmark = Bookmark.query.filter_by( id=bookmark_id ).first() translations_of_bookmark = bookmark.translations_list for transl in translations_of_bookmark: if transl.word == word_translation: return 'FAIL' translation_user_word = UserWord.find(word_translation, translations_of_bookmark[0].language) bookmark.add_new_translation(translation_user_word) zeeguu.db.session.add(translation_user_word) zeeguu.db.session.commit() return "OK"
def test_importance_level(self): word = "beschloss" if zeeguu.model.ranked_word.RankedWord.exists(word.lower(), self.de): rank = model.UserWord.find_rank(word.lower(), self.de) new_word = model.UserWord.find(word,self.de) else: new_word = model.UserWord.find(word,self.de) db.session.add(new_word) db.session.commit() word = "unexistingword" beschloss = UserWord.find(word, self.de) assert beschloss assert beschloss.importance_level() == 0
def test_importance_level(self): word = "beschloss" if zeeguu.model.ranked_word.RankedWord.exists(word.lower(), self.de): rank = model.UserWord.find_rank(word.lower(), self.de) new_word = model.UserWord.find(word, self.de) else: new_word = model.UserWord.find(word, self.de) db.session.add(new_word) db.session.commit() word = "unexistingword" beschloss = UserWord.find(word, self.de) assert beschloss assert beschloss.importance_level() == 0
def test_find_word(self): assert UserWord.find(self.random_origin_word, self.random_origin_language)
def words(self): for word in re.split(re.compile("[^\\w]+", re.U), self.content): yield UserWord.find(word, self.language)
def test_search_1(self): word = UserWord.find("hauen345", self.de) s = model.Search(self.mir, word, self.de) db.session.add(s) db.session.commit()
def calculate_probabilities_after_adding_a_bookmark(self, user, language): """ ML: This has to be refactored. It's a mess. The idea is: you've just added a bookmark. There are two things to do: 1. update the probabilities of the context words (they have been encountered, and not translated) 2. update the probabilities of the word itself - :param user: :param language: :return: """ # 1. computations for adding encounter based probability for the context words for word in self.context_words_with_rank(): enc_prob = EncounterBasedProbability.find_or_create( word, user, language) zeeguu.db.session.add(enc_prob) zeeguu.db.session.commit() user_word = None ranked_word = enc_prob.ranked_word if UserWord.exists(word, language): user_word = UserWord.find(word, language) if ExerciseBasedProbability.exists( user, user_word ): #checks if exercise based probability exists for words in context ex_prob = ExerciseBasedProbability.find(user, user_word) known_word_prob = KnownWordProbability.find( user, user_word, ranked_word) known_word_prob.probability = known_word_prob.calculateKnownWordProb( ex_prob.probability, enc_prob.probability ) #updates known word probability as exercise based probability already existed. else: if KnownWordProbability.exists(user, user_word, ranked_word): known_word_prob = KnownWordProbability.find( user, user_word, ranked_word) known_word_prob.probability = enc_prob.probability # updates known word probability as encounter based probability already existed else: known_word_prob = KnownWordProbability.find( user, user_word, ranked_word, enc_prob.probability ) # new known word probability created as it did not exist zeeguu.db.session.add(known_word_prob) # 2. Update the probabilities of the word itself # 2.a) exercise based prob # ML: Should this thing change? # The ex based probability should probably not change after I add a bookmark # Commenting out the following lines: s # ex_prob = ExerciseBasedProbability.find(user, self.origin) # if ex_prob: # ex_prob.update_probability_after_adding_bookmark_with_same_word(self,user) # zeeguu.db.session.add(ex_prob) # 2.b) encounter based prob ranked_word = RankedWord.find(self.origin.word, language) if ranked_word: #checks if ranked_word exists for that looked up word if EncounterBasedProbability.exists( user, ranked_word ): # checks if encounter based probability exists for that looked up word enc_prob = EncounterBasedProbability.find(user, ranked_word) enc_prob.word_has_just_beek_bookmarked() db.session.add(enc_prob) db.session.commit() # 2.c) update known word probability if it exists if KnownWordProbability.exists(user, self.origin, ranked_word): known_word_prob = KnownWordProbability.find( user, self.origin, ranked_word) known_word_prob.word_has_just_beek_bookmarked() db.session.add(known_word_prob) db.session.commit()
def test_find_word(self): word = "baum" assert UserWord.find(word, self.de)
def calculate_probabilities_after_adding_a_bookmark(self, user,language): """ ML: This has to be refactored. It's a mess. The idea is: you've just added a bookmark. There are two things to do: 1. update the probabilities of the context words (they have been encountered, and not translated) 2. update the probabilities of the word itself - :param user: :param language: :return: """ # 1. computations for adding encounter based probability for the context words for word in self.context_words_with_rank(): enc_prob = EncounterBasedProbability.find_or_create(word, user, language) zeeguu.db.session.add(enc_prob) zeeguu.db.session.commit() user_word = None ranked_word = enc_prob.ranked_word if UserWord.exists(word,language): user_word = UserWord.find(word,language) if ExerciseBasedProbability.exists(user,user_word): #checks if exercise based probability exists for words in context ex_prob = ExerciseBasedProbability.find(user,user_word) known_word_prob = KnownWordProbability.find(user,user_word,ranked_word) known_word_prob.probability = known_word_prob.calculateKnownWordProb(ex_prob.probability, enc_prob.probability) #updates known word probability as exercise based probability already existed. else: if KnownWordProbability.exists(user, user_word,ranked_word): known_word_prob = KnownWordProbability.find(user,user_word,ranked_word) known_word_prob.probability = enc_prob.probability # updates known word probability as encounter based probability already existed else: known_word_prob = KnownWordProbability.find(user,user_word,ranked_word, enc_prob.probability) # new known word probability created as it did not exist zeeguu.db.session.add(known_word_prob) # 2. Update the probabilities of the word itself # 2.a) exercise based prob # ML: Should this thing change? # The ex based probability should probably not change after I add a bookmark # Commenting out the following lines: s # ex_prob = ExerciseBasedProbability.find(user, self.origin) # if ex_prob: # ex_prob.update_probability_after_adding_bookmark_with_same_word(self,user) # zeeguu.db.session.add(ex_prob) # 2.b) encounter based prob ranked_word = RankedWord.find(self.origin.word, language) if ranked_word: #checks if ranked_word exists for that looked up word if EncounterBasedProbability.exists(user, ranked_word): # checks if encounter based probability exists for that looked up word enc_prob = EncounterBasedProbability.find(user, ranked_word) enc_prob.word_has_just_beek_bookmarked() db.session.add(enc_prob) db.session.commit() # 2.c) update known word probability if it exists if KnownWordProbability.exists(user, self.origin,ranked_word): known_word_prob = KnownWordProbability.find(user,self.origin,ranked_word) known_word_prob.word_has_just_beek_bookmarked() db.session.add(known_word_prob) db.session.commit()