def lookup(from_lang, term, to_lang): """ Used to log a given search. TODO: See what's the relation between this and goslate, that is, /goslate should already log the search... also, this requires both from and to_lang, but goslate does not. :param from_lang: :param term: :param to_lang: :return: """ from_lang = Language.find(from_lang) if not isinstance(to_lang, Language): to_lang = Language.find(to_lang) user = flask.g.user content = flask.request.form.get("text") if content is not None: text = Text.find(content, from_lang) user.read(text) else: text = None word = decode_word(term) rank = UserWord.find_rank(word, to_lang) user.searches.append( Search(user, UserWord.find(word, from_lang), to_lang, text) ) zeeguu.db.session.commit() return "OK"
def lookup(from_lang, term, to_lang): """ Used to log a given search. TODO: See what's the relation between this and goslate, that is, /goslate should already log the search... also, this requires both from and to_lang, but goslate does not. :param from_lang: :param term: :param to_lang: :return: """ from_lang = Language.find(from_lang) if not isinstance(to_lang, Language): to_lang = Language.find(to_lang) user = flask.g.user content = flask.request.form.get("text") if content is not None: text = Text.find(content, from_lang) user.read(text) else: text = None word = decode_word(term) rank = UserWord.find_rank(word, to_lang) user.searches.append( Search(user, UserWord.find(word, from_lang), to_lang, text)) 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_bookmark(user, original_language, original_word, translation_language, translation_word, date, the_context, the_url, the_url_title): url = Url.find (the_url, the_url_title) text = Text(the_context, translation_language, url) if RankedWord.exists(original_word.lower(), original_language): rank1 = UserWord.find_rank(original_word.lower(), original_language) w1 = UserWord(original_word, original_language,rank1) else: w1 = UserWord(original_word, original_language,None) if RankedWord.exists(translation_word.lower(), translation_language): rank2 = UserWord.find_rank(translation_word.lower(), translation_language) w2 = UserWord(translation_word, translation_language,rank2) else: w2 = UserWord(translation_word, translation_language,None) zeeguu.db.session.add(url) zeeguu.db.session.add(text) zeeguu.db.session.add(w1) zeeguu.db.session.add(w2) t1= Bookmark(w1,w2, user, text, date) zeeguu.db.session.add(t1) zeeguu.db.session.commit() add_probability_to_existing_words_of_user(user,t1,original_language)
def test_importance_level(self): random_word_stats = self.__get_random_word_stats() random_language = LanguageRule().get_or_create_language( random_word_stats[1].language_id) user_word = UserWord(random_word_stats[0], random_language) assert user_word.importance_level() == int( random_word_stats[1].importance)
def test_find_or_create(self): random_word = self.faker.word() random_language = LanguageRule().random user_word_not_in_db = UserWord(random_word, random_language) user_word_created = UserWord.find_or_create(self.db.session, random_word, random_language) assert user_word_created == user_word_not_in_db
def __getitem__(self, args): word = self.cache.get(args, None) if word is None: word = UserWord(*args) zeeguu.db.session.add(word) self.cache[args] = word return word
def test_find_all(self): list_random_user_words = [ UserWordRule().user_word for _ in range(random.randint(2, 5)) ] list_retrieved = UserWord.find_all() assert all([word in list_retrieved for word in list_random_user_words])
def test_add_new_word_to_DB(self): word = "baum" rank = model.UserWord.find_rank(word, self.de) new_word = UserWord(word, self.de, rank) db.session.add(new_word) self.mir.star(new_word) db.session.commit()
def test_find_by_language(self): random_language = LanguageRule().random list_random_user_words = [ UserWordRule(word=self.faker.word(), language=random_language).user_word for _ in range(random.randint(2, 5)) ] list_retrieved = UserWord.find_by_language(random_language) assert all([word in list_retrieved for word in list_random_user_words])
def bookmark_with_context(from_lang_code, term, to_lang_code, translation): """ The preferred way of a user saving a word/translation/context to his profile. :param from_lang_code: :param term: :param to_lang_code: :param translation: :return: """ if 'title' in flask.request.form: bookmarked_url_title = flask.request.form['title'] else: bookmarked_url_title = '' bookmarked_url = flask.request.form['url'] context = flask.request.form['context'] url = Url.find(bookmarked_url, bookmarked_url_title) from_lang = Language.find(from_lang_code) to_lang = Language.find(to_lang_code) word = (decode_word(term)) translation_word = decode_word(translation) user_word = UserWord.find(word, from_lang) translation = UserWord.find(translation_word, to_lang) # search = Search.query.filter_by( # user=flask.g.user, user_word=user_word, language=to_lang # ).order_by(Search.id.desc()).first() #create the text entity first new_text = Text(context, from_lang, url) bookmark = Bookmark(user_word, translation, flask.g.user, new_text, datetime.datetime.now()) zeeguu.db.session.add(bookmark) bookmark.calculate_probabilities_after_adding_a_bookmark( flask.g.user, bookmark.origin.language) return str(bookmark.id)
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, the_url_title) text = Text(the_context, translation_language, url) if RankedWord.exists(original_word.lower(), original_language): rank1 = UserWord.find_rank(original_word.lower(), original_language) w1 = UserWord(original_word, original_language, rank1) else: w1 = UserWord(original_word, original_language, None) if RankedWord.exists(translation_word.lower(), translation_language): rank2 = UserWord.find_rank(translation_word.lower(), translation_language) w2 = UserWord(translation_word, translation_language, rank2) else: w2 = UserWord(translation_word, translation_language, None) zeeguu.db.session.add(url) zeeguu.db.session.add(text) zeeguu.db.session.add(w1) zeeguu.db.session.add(w2) t1 = Bookmark(w1, w2, 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, term, to_lang_code, translation): """ The preferred way of a user saving a word/translation/context to his profile. :param from_lang_code: :param term: :param to_lang_code: :param translation: :return: """ if 'title' in flask.request.form: bookmarked_url_title = flask.request.form['title'] else: bookmarked_url_title = '' bookmarked_url = flask.request.form['url'] context = flask.request.form['context'] url = Url.find(bookmarked_url, bookmarked_url_title) from_lang = Language.find(from_lang_code) to_lang = Language.find(to_lang_code) word = (decode_word(term)) translation_word = decode_word(translation) user_word = UserWord.find(word,from_lang) translation = UserWord.find(translation_word,to_lang) # search = Search.query.filter_by( # user=flask.g.user, user_word=user_word, language=to_lang # ).order_by(Search.id.desc()).first() #create the text entity first new_text = Text(context, from_lang, url) bookmark = Bookmark(user_word, translation, flask.g.user, new_text, datetime.datetime.now()) zeeguu.db.session.add(bookmark) bookmark.calculate_probabilities_after_adding_a_bookmark(flask.g.user, bookmark.origin.language) 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(model.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 (model.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 process_bookmarked_sentences(user_article, start_time=LONG_TIME_IN_THE_PAST, end_time=datetime.now()): """ Process all bookmarks for the user_article within the specified times Parameters: user_article = user_article class object start_time = datetime from which to take bookmarks end_time = datetime of final bookmark to process returns: list of processed bookmarks """ user = user_article.user # Get all the bookmarks for the specified article and dates query = Bookmark.query.join(Text).filter(Bookmark.user == user).filter(Text.url == user_article.article.url) query = query.filter(Bookmark.time >= start_time).filter(Bookmark.time <= end_time) bookmarks = query.order_by(Bookmark.time).all() for bookmark in bookmarks: # Get text in the sentence of the bookmark sentence = Text.query.filter(Text.id == bookmark.text_id).one().content # Get unique words in sentence unique_words_in_sentence = extract_words_from_text(sentence, user_article.article.language) for word in unique_words_in_sentence: # label the word as clicked or not clicked if word in bookmark.origin.word.lower(): event_type = WIH_READ_CLICKED else: event_type = WIH_READ_NOT_CLICKED_IN_SENTENCE # Get or create word object user_word = UserWord.find_or_create(session=session, _word=word, language=user_article.article.language) # Find a WordInteractionHistory word_interaction_history = WordInteractionHistory.find_or_create(user=user, word=user_word) # Add event word_interaction_history.insert_event(event_type, bookmark.time) word_interaction_history.save_to_db(session) return bookmarks
def set_default_exercise_based_prob(): zeeguu.app.test_request_context().push() zeeguu.db.session.commit() users = User.find_all() languages = Language.all() for user in users: for language in languages: user_words_by_language = UserWord.find_by_language(language) for word in user_words_by_language: if ExerciseBasedProbability.exists(user, word): prob = ExerciseBasedProbability.find(user, word) bookmarks_by_user_and_word = Bookmark.find_all_by_user_and_word(user, word) total_prob = 0 for bookmark in bookmarks_by_user_and_word: prob.calculate_known_bookmark_probability(bookmark) total_prob += float(prob.probability) if bookmarks_by_user_and_word: prob.probability = total_prob / len(bookmarks_by_user_and_word) zeeguu.db.session.commit() print "job1"
def set_default_exercise_based_prob(): zeeguu.app.test_request_context().push() zeeguu.db.session.commit() users = User.find_all() languages = Language.all() for user in users: for language in languages: user_words_by_language = UserWord.find_by_language(language) for word in user_words_by_language: if ExerciseBasedProbability.exists(user, word): prob = ExerciseBasedProbability.find(user, word) bookmarks_by_user_and_word = Bookmark.find_all_by_user_and_word( user, word) total_prob = 0 for bookmark in bookmarks_by_user_and_word: prob.calculate_known_bookmark_probability(bookmark) total_prob += float(prob.probability) if bookmarks_by_user_and_word: prob.probability = total_prob / len( bookmarks_by_user_and_word) zeeguu.db.session.commit() print 'job1'
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 test_find_word(self): word = "baum" assert UserWord.find(word, self.de)
def test_exists(self): user_word_in_db = UserWordRule().user_word assert UserWord.exists(user_word_in_db.word, user_word_in_db.language)
previous_fully_read_date = fully_read_date # Extract words from bookmarked sentences for bookmark in processed_bookmarks: # Get text in the sentece of the bookmark sentence = Text.query.filter(Text.id == bookmark.text_id).one().content # Get unique words in sentence unique_words_in_sentence = extract_words_from_text(sentence, ua.article.language) # Remove already processed words unique_words_in_article = unique_words_in_article.difference(unique_words_in_sentence) # Insert remaining words as not clicked out of sentence for word in unique_words_in_article: user_word = UserWord.find_or_create(session=session, _word=word, language=ua.article.language) word_interaction_history = WordInteractionHistory.find_or_create(user=user, word=user_word) word_interaction_history.insert_event(WIH_READ_NOT_CLICKED_OUT_SENTENCE, fully_read_date) word_interaction_history.save_to_db(session) # ==============================EXERCISES================================ # words encountered in exercises bmex_mapping = data = session.query(bookmark_exercise_mapping).all() for bm_id, ex_id in bmex_mapping: try: bm = Bookmark.query.filter(Bookmark.id == bm_id).one() ex = Exercise.query.filter(Exercise.id == ex_id).one()
def test_find_word(self): word = "baum" rank = model.UserWord.find_rank(word, self.de) assert UserWord.find(word, self.de, rank)
def test_find(self): user_word_should_be = UserWordRule().user_word user_word_to_check = UserWord.find(user_word_should_be.word, user_word_should_be.language) assert user_word_to_check == user_word_should_be