def get_possible_translations(from_lang_code, to_lang_code): """ Returns a list of possible translations in :param to_lang_code for :param word in :param from_lang_code. You must also specify the :param context, :param url, and :param title of the page where the word was found. The context is the sentence. :return: json array with translations """ data = {"from_lang_code": from_lang_code, "to_lang_code": to_lang_code} data["context"] = request.form.get('context', '') url = request.form.get('url', '') data["url"] = url article_id = None if 'articleID' in url: article_id = url.split('articleID=')[-1] url = Article.query.filter_by(id=article_id).one().url.as_canonical_string() elif 'articleURL' in url: url = url.split('articleURL=')[-1] else: # the url comes from elsewhere not from the reader, so we find or creat the article article = Article.find_or_create(db_session, url) article_id = article.id zeeguu_core.log(f"url before being saved: {url}") word_str = request.form['word'] data["word"] = word_str title_str = request.form.get('title', '') data["title"] = title_str zeeguu_core.log(f'translating to... {data["to_lang_code"]}') minimal_context, query = minimize_context( data["context"], data["from_lang_code"], data["word"]) zeeguu_core.log(f"Query to translate is: {query}") data["query"] = query translations = get_all_translations(data).translations zeeguu_core.log(f"Got translations: {translations}") # translators talk about quality, but our users expect likelihood. # rename the key in the dictionary for t in translations: t['likelihood'] = t.pop("quality") t['source'] = t.pop('service_name') best_guess = translations[0]["translation"] Bookmark.find_or_create(db_session, flask.g.user, word_str, from_lang_code, best_guess, to_lang_code, minimal_context, url, title_str, article_id) return json_result(dict(translations=translations))
def test_bookmarks_in_url(self): random_bookmark = BookmarkRule(self.user).bookmark url = random_bookmark.text.url # each bookmark belongs to a random text / url so the # combo of user/url will always result in one bookmark assert 1 == len(Bookmark.find_all_for_user_and_url(self.user, url))
def test_find_by_user_word_and_text(self): bookmark_should_be = self.user.all_bookmarks()[0] bookmark_to_check = Bookmark.find_by_user_word_and_text( self.user, bookmark_should_be.origin, bookmark_should_be.text ) assert bookmark_to_check == bookmark_should_be
def user_article_info(user: User, article: Article, with_content=False, with_translations=True): from zeeguu_core.model import UserArticle prior_info = UserArticle.find(user, article) ua_info = article.article_info(with_content=with_content) if not prior_info: ua_info['starred'] = False ua_info['opened'] = False ua_info['liked'] = False ua_info['translations'] = [] return ua_info ua_info['starred'] = prior_info.starred is not None ua_info['opened'] = prior_info.opened is not None ua_info['liked'] = prior_info.liked if with_translations: translations = Bookmark.find_all_for_user_and_url(user, article.url) ua_info['translations'] = [ each.serializable_dictionary() for each in translations ] return ua_info
def test_find_all_by_user_and_word(self): bookmark_should_be = self.user.all_bookmarks()[0] bookmark_to_check = Bookmark.find_all_by_user_and_word( self.user, bookmark_should_be.origin ) assert bookmark_should_be in bookmark_to_check
def find_all_for_user_and_text(self): bookmark_should_be = self.user.all_bookmarks()[0] bookmark_to_check = Bookmark.find_all_for_text_and_user( bookmark_should_be.text, self.user ) assert bookmark_should_be in bookmark_to_check
def user_article_info(cls, user: User, article: Article, with_content=False, with_translations=True): from zeeguu_core.model import Bookmark # Initialize returned info with the default article info returned_info = article.article_info(with_content=with_content) user_article_info = UserArticle.find(user, article) if not user_article_info: returned_info['starred'] = False returned_info['opened'] = False returned_info['liked'] = False returned_info['translations'] = [] return returned_info returned_info['starred'] = user_article_info.starred is not None returned_info['opened'] = user_article_info.opened is not None returned_info['liked'] = user_article_info.liked if with_translations: translations = Bookmark.find_all_for_user_and_url( user, article.url) returned_info['translations'] = [ each.serializable_dictionary() for each in translations ] return returned_info
def report_exercise_outcome(exercise_outcome, exercise_source, exercise_solving_speed, bookmark_id): """ In the model parlance, an exercise is an entry in a table that logs the performance of an exercise. Every such performance, has a source, and an outcome. :param exercise_outcome: One of: Correct, Retry, Wrong, Typo, Too easy... :param exercise_source: has been assigned to your app by zeeguu :param exercise_solving_speed: in milliseconds :param bookmark_id: the bookmark for which the data is reported :return: """ if not exercise_solving_speed.isdigit(): exercise_solving_speed = 0 try: bookmark = Bookmark.find(bookmark_id) bookmark.report_exercise_outcome(exercise_source, exercise_outcome, exercise_solving_speed, db_session) return "OK" except: traceback.print_exc() return "FAIL"
def delete_bookmark(bookmark_id): try: bookmark = Bookmark.find(bookmark_id) db_session.delete(bookmark) db_session.commit() except NoResultFound: return "Inexistent" return "OK"
def translate_and_bookmark(from_lang_code, to_lang_code): """ @deprecated This should be deprecated and /get_possible_translations used instead However, it is still used by the zeeguu chrome extension. This expects in the post parameter the following: - word (to translate) - context (surrounding paragraph of the original word ) - url (of the origin) - title (of the origin page) /get_possible_translations has very similar behavior, only that if focuses on returning the possible alternative translations :param from_lang_code: :param to_lang_code: :return: """ data = {"from_lang_code": from_lang_code, "to_lang_code": to_lang_code} word_str = unquote_plus(request.form['word']) data["word"] = word_str url_str = request.form.get('url', '') data["url"] = url_str title_str = request.form.get('title', '') data["title"] = title_str context_str = request.form.get('context', '') data["context"] = context_str # the url comes from elsewhere not from the reader, so we find or creat the article article = Article.find_or_create(db_session, url_str) article_id = article.id try: minimal_context, query = minimize_context( data["context"], data["from_lang_code"], data["word"]) data["query"] = query translations = get_all_translations(data).translations best_guess = translations[0]["translation"] bookmark = Bookmark.find_or_create(db_session, flask.g.user, word_str, from_lang_code, best_guess, to_lang_code, minimal_context, url_str, title_str, article_id) except ValueError as e: zeeguu_core.log(f"minimize context failed {e}on: {context_str} x {from_lang_code} x {word_str} ") return context_str, query return json_result(dict( bookmark_id=bookmark.id, translation=best_guess))
def report_learned_bookmark(bookmark_id): bookmark = Bookmark.find(bookmark_id) bookmark.report_exercise_outcome( ExerciseSource.TOP_BOOKMARKS_MINI_EXERCISE, ExerciseOutcome.CORRECT, -1, db_session, ) return "OK"
def contribute_translation(from_lang_code, to_lang_code): """ User contributes a translation they think is appropriate for a given :param word in :param from_lang_code in a given :param context The :param translation is in :param to_lang_code Together with the two words and the textual context, you must submit also the :param url, :param title of the page where the original word and context occurred. :return: in case of success, the bookmark_id and main translation """ # All these POST params are mandatory word_str = unquote_plus(request.form['word']) translation_str = request.form['translation'] url = request.form.get('url', '') context_str = request.form.get('context', '') title_str = request.form.get('title', '') # when a translation is added by hand, the servicename_translation is None # thus we set it to MANUAL service_name = request.form.get('servicename_translation', 'MANUAL') article_id = None if 'articleID' in url: article_id = url.split('articleID=')[-1] url = Article.query.filter_by(id=article_id).one().url.as_canonical_string() elif 'articleURL' in url: url = url.split('articleURL=')[-1] else: # the url comes from elsewhere not from the reader, so we find or creat the article article = Article.find_or_create(db_session, url) article_id = article.id # Optional POST param selected_from_predefined_choices = request.form.get('selected_from_predefined_choices', '') minimal_context, _ = minimize_context(context_str, from_lang_code, word_str) bookmark = Bookmark.find_or_create(db_session, flask.g.user, word_str, from_lang_code, translation_str, to_lang_code, minimal_context, url, title_str, article_id) # Inform apimux about translation selection data = {"word_str": word_str, "translation_str": translation_str, "url": url, "context_size": len(context_str), "service_name": service_name} contribute_trans(data) return json_result(dict(bookmark_id=bookmark.id))
def report_exercise_outcome(exercise_outcome, exercise_source, exercise_solving_speed, bookmark_id): """ In the model parlance, an exercise is an entry in a table that logs the performance of an exercise. Every such performance, has a source, and an outcome. :param exercise_outcome: One of: Correct, Retry, Wrong, Typo, Too easy :param exercise_source: has been assigned to your app by zeeguu :param exercise_solving_speed: in milliseconds :param bookmark_id: the bookmark for which the data is reported :return: """ try: bookmark = Bookmark.find(bookmark_id) new_source = ExerciseSource.find(exercise_source) new_outcome = ExerciseOutcome.find_or_create(db_session, exercise_outcome) if not bookmark: return "could not find bookmark" if not new_source: return "could not find source" if not new_outcome: return "could not find outcome" exercise = Exercise(new_outcome, new_source, exercise_solving_speed, datetime.now()) bookmark.add_new_exercise(exercise) bookmark.update_fit_for_study(db_session) bookmark.update_learned_status(db_session) db_session.add(exercise) db_session.commit() # Update the exercise session from zeeguu_core.model import UserExerciseSession UserExerciseSession.update_exercise_session(exercise, db_session) zeeguu_core.log("recomputting bookmark priorities") BookmarkPriorityUpdater.update_bookmark_priority( zeeguu_core.db, flask.g.user) return "OK" except: traceback.print_exc() return "FAIL"
def report_exercise_outcome(exercise_outcome, exercise_source, exercise_solving_speed, bookmark_id): """ In the model parlance, an exercise is an entry in a table that logs the performance of an exercise. Every such performance, has a source, and an outcome. :param exercise_outcome: One of: Correct, Retry, Wrong, Typo, Too easy :param exercise_source: has been assigned to your app by zeeguu :param exercise_solving_speed: in milliseconds :param bookmark_id: the bookmark for which the data is reported :return: """ try: bookmark = Bookmark.find(bookmark_id) new_source = ExerciseSource.find(exercise_source) new_outcome = ExerciseOutcome.find_or_create(db_session, exercise_outcome) if not bookmark: return "could not find bookmark" if not new_source: return "could not find source" if not new_outcome: return "could not find outcome" exercise = Exercise(new_outcome, new_source, exercise_solving_speed, datetime.now()) bookmark.add_new_exercise(exercise) bookmark.update_fit_for_study(db_session) bookmark.update_learned_status(db_session) db_session.add(exercise) db_session.commit() # Update the exercise session from zeeguu_core.model import UserExerciseSession UserExerciseSession.update_exercise_session(exercise, db_session) zeeguu_core.log("recomputting bookmark priorities") BookmarkPriorityUpdater.update_bookmark_priority(zeeguu_core.db, flask.g.user) return "OK" except: traceback.print_exc() return "FAIL"
def unstar_bookmark(bookmark_id): bookmark = Bookmark.find(bookmark_id) bookmark.starred = False bookmark.update_fit_for_study() db_session.commit() return "OK"
def test_exists(self): random_bookmark = self.user.all_bookmarks()[0] assert Bookmark.exists(random_bookmark)
def all_bookmarks(self, user): from zeeguu_core.model import Bookmark return Bookmark.find_all_for_user_and_text(self, user)
def test_find_all(self): list_should_be = self.user.all_bookmarks() list_to_check = Bookmark.find_all() for b in list_should_be: assert b in list_to_check
def test_find(self): bookmark_should_be = self.user.all_bookmarks()[0] bookmark_to_check = Bookmark.find(bookmark_should_be.id) assert bookmark_to_check == bookmark_should_be
def test_find_by_specific_user(self): list_should_be = self.user.all_bookmarks() list_to_check = Bookmark.find_by_specific_user(self.user) for b in list_should_be: assert b in list_to_check
def delete_bookmark(bookmark_id): bookmark = Bookmark.find(bookmark_id) db_session.delete(bookmark) db_session.commit() return "OK"
def report_learned_bookmark(bookmark_id): bookmark = Bookmark.find(bookmark_id) bookmark.learned = True bookmark.learned_time = datetime.datetime.now() db_session.commit() return "OK"
def get_one_translation(from_lang_code, to_lang_code): """ Addressing some of the problems with the get_next_translations... - it should be separated in get_first and get_alternatives - alternatively it can be get one and get all To think about: - it would also make sense to separate translation from logging; or at least, allow for situations where a translation is not associated with an url... or? :return: json array with translations """ word_str = request.form["word"] url = request.form.get("url") title_str = request.form.get("title", "") context = request.form.get("context", "") minimal_context, query = minimize_context(context, from_lang_code, word_str) translation = own_translation( flask.g.user, word_str, from_lang_code, to_lang_code, minimal_context ) if translation: return json_result(dict(translations=translation)) translations = get_next_results( { "from_lang_code": from_lang_code, "to_lang_code": to_lang_code, "url": request.form.get("url"), "word": word_str, "title": title_str, "query": query, "context": minimal_context, }, number_of_results=1, ).translations # do we really need this? # translators talk about quality, but our users expect likelihood. # rename the key in the dictionary for t in translations: t["likelihood"] = t.pop("quality") t["source"] = t["service_name"] article_id = None if "article?id=" in url: article_id = url.split("article?id=")[-1] url = Article.query.filter_by(id=article_id).one().url.as_canonical_string() else: # the url comes from elsewhere not from the reader, so we find or creat the article article = Article.find_or_create(db_session, url) article_id = article.id if len(translations) > 0: best_guess = translations[0]["translation"] Bookmark.find_or_create( db_session, flask.g.user, word_str, from_lang_code, best_guess, to_lang_code, minimal_context, url, title_str, article_id, ) return json_result(dict(translations=translations))
def get_next_translations(from_lang_code, to_lang_code): """ Returns a list of possible translations in :param to_lang_code for :param word in :param from_lang_code. You must also specify the :param context, :param url, and :param title of the page where the word was found. The context is the sentence. :return: json array with translations """ data = {"from_lang_code": from_lang_code, "to_lang_code": to_lang_code} data["context"] = request.form.get("context", "") url = request.form.get("url", "") number_of_results = int(request.form.get("numberOfResults", -1)) service_name = request.form.get("service", "") exclude_services = [] if service_name == "" else [service_name] currentTranslation = request.form.get("currentTranslation", "") exclude_results = [] if currentTranslation == "" else [currentTranslation.lower()] data["url"] = url article_id = request.form.get("articleID", None) if article_id == None: if "articleID" in url: article_id = url.split("articleID=")[-1] url = Article.query.filter_by(id=article_id).one().url.as_canonical_string() elif "articleURL" in url: url = url.split("articleURL=")[-1] else: # the url comes from elsewhere not from the reader, so we find or creat the article article = Article.find_or_create(db_session, url) article_id = article.id zeeguu_core.log(f"url before being saved: {url}") word_str = request.form["word"] data["word"] = word_str title_str = request.form.get("title", "") data["title"] = title_str zeeguu_core.log(f'translating to... {data["to_lang_code"]}') minimal_context, query = minimize_context( data["context"], data["from_lang_code"], data["word"] ) zeeguu_core.log(f"Query to translate is: {query}") data["query"] = query first_call_for_this_word = len(exclude_services) == 0 if first_call_for_this_word: translations = own_or_crowdsourced_translation( flask.g.user, word_str, from_lang_code, to_lang_code, minimal_context ) if translations: return json_result(dict(translations=translations)) translations = get_next_results( data, exclude_services=exclude_services, exclude_results=exclude_results, number_of_results=number_of_results, ).translations # translators talk about quality, but our users expect likelihood. # rename the key in the dictionary for t in translations: t["likelihood"] = t.pop("quality") t["source"] = t["service_name"] if len(translations) > 0 and first_call_for_this_word: best_guess = translations[0]["translation"] Bookmark.find_or_create( db_session, flask.g.user, word_str, from_lang_code, best_guess, to_lang_code, minimal_context, url, title_str, article_id, ) return json_result(dict(translations=translations))
def similar_words_api(bookmark_id): bookmark = Bookmark.find(bookmark_id) return json_result(similar_words(bookmark, flask.g.user))