Exemplo n.º 1
0
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)
Exemplo n.º 2
0
def user_article_info(user: User,
                      article: Article,
                      with_content=False,
                      with_translations=True):
    from zeeguu.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
Exemplo n.º 3
0
    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))
Exemplo n.º 4
0
def set_default_encounter_based_prob():
    zeeguu.app.test_request_context().push()
    zeeguu.db.session.commit()
    default_probability = 0.5
    languages = Language.all()
    users = User.find_all()
    for user in users:
        for lang in languages:
            marked_words_of_user_in_text = []
            words_of_all_bookmarks_content = []
            for bookmark in Bookmark.find_by_specific_user(user):
                if bookmark.origin.language == lang:
                    # bookmark_content_words = re.sub("[^\w]", " ",  bookmark.text.content).split()
                    bookmark_content_words = re.findall(r'(?u)\w+', bookmark.text.content)
                    words_of_all_bookmarks_content.extend(bookmark_content_words)
                    marked_words_of_user_in_text.append(bookmark.origin.word)
            words_known_from_user= [word for word in words_of_all_bookmarks_content if word not in marked_words_of_user_in_text]
            for word_known in words_known_from_user:
                if RankedWord.exists(word_known, lang):
                   rank = RankedWord.find(word_known, lang)
                   if EncounterBasedProbability.exists(user, rank):
                       prob = EncounterBasedProbability.find(user,rank, default_probability)
                       prob.not_looked_up_counter +=1
                   else:
                       prob = EncounterBasedProbability.find(user,rank,default_probability)
                       zeeguu.db.session.add(prob)
     		       zeeguu.db.session.commit()
    print 'job2'
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

    """

    context_str = request.form.get('context', '')
    url = request.form.get('url', '')
    #
    url = url.split('articleURL=')[-1]

    zeeguu.log(f"url before being saved: {url}")
    word_str = request.form['word']
    title_str = request.form.get('title', '')

    minimal_context, query = minimize_context(context_str, from_lang_code,
                                              word_str)

    to_lang_code = flask.g.user.native_language.code
    zeeguu.log(f'translating to... {to_lang_code}')

    translator = Translator(from_lang_code, to_lang_code)
    zeeguu.log(f"Query to translate is: {query}")
    translations = translator.translate(query).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)

    return json_result(dict(translations=translations))
Exemplo n.º 6
0
def set_default_exercise_based_prob():
    zeeguu.app.test_request_context().push()
    zeeguu.db.session.commit()
    bookmarks = Bookmark.find_all()
    for bookmark in bookmarks:
        prob = ExerciseBasedProbability.find(bookmark.user, bookmark.origin)
        zeeguu.db.session.add(prob)
        zeeguu.db.session.commit()
    print 'job1'
Exemplo n.º 7
0
def get_learnability_for_text(lang_code):
    """
    URL parameters:
    :param lang_code: the language of the text

    Json data:
    :param texts: json array that contains the texts to calculate the learnability for. Each text consists of an array
        with the text itself as 'content' and an additional 'id' which gets roundtripped unchanged

    :return learnabilities: json array, contains the learnabilities as arrays with the key 'score' for the learnability
        value (percentage of words from the text that the user is currently learning), the 'count' of the learned
        words in the text and the 'id' parameter to identify the corresponding text
    """
    language = Language.find(lang_code)
    if language is None:
        return 'FAIL'

    data = flask.request.get_json()

    texts = []
    if 'texts' in data:
        for text in data['texts']:
            texts.append(text)
    else:
        return 'FAIL'

    user = flask.g.user

    # Get the words the user is currently learning
    words_learning = {}
    bookmarks = Bookmark.find_by_specific_user(user)
    for bookmark in bookmarks:
        learning = not bookmark.check_is_latest_outcome_too_easy()
        user_word = bookmark.origin
        if learning and user_word.language == language:
            words_learning[user_word.word] = user_word.word

    learnabilities = []
    for text in texts:
        # Calculate learnability
        words = util.split_words_from_text(text['content'])
        words_learnability = []
        for word in words:
            if word in words_learning:
                words_learnability.append(word)

        count = len(words_learnability)
        learnability = count / float(len(words))

        learnabilities.append(
            dict(score=learnability, count=count, id=text['id']))

    response = json.dumps(dict(learnabilities=learnabilities))

    return flask.Response(response, status=200, mimetype='application/json')
Exemplo n.º 8
0
def get_learnability_for_text(lang_code):
    """
    URL parameters:
    :param lang_code: the language of the text

    Json data:
    :param texts: json array that contains the texts to calculate the learnability for. Each text consists of an array
        with the text itself as 'content' and an additional 'id' which gets roundtripped unchanged

    :return learnabilities: json array, contains the learnabilities as arrays with the key 'score' for the learnability
        value (percentage of words from the text that the user is currently learning), the 'count' of the learned
        words in the text and the 'id' parameter to identify the corresponding text
    """
    language = Language.find(lang_code)
    if language is None:
        return 'FAIL'

    data = flask.request.get_json()

    texts = []
    if 'texts' in data:
        for text in data['texts']:
            texts.append(text)
    else:
        return 'FAIL'

    user = flask.g.user

    # Get the words the user is currently learning
    words_learning = {}
    bookmarks = Bookmark.find_by_specific_user(user)
    for bookmark in bookmarks:
        learning = not bookmark.check_is_latest_outcome_too_easy()
        user_word = bookmark.origin
        if learning and user_word.language == language:
            words_learning[user_word.word] = user_word.word

    learnabilities = []
    for text in texts:
        # Calculate learnability
        words = util.split_words_from_text(text['content'])
        words_learnability = []
        for word in words:
            if word in words_learning:
                words_learnability.append(word)

        count = len(words_learnability)
        learnability = count / float(len(words))

        learnabilities.append(dict(score=learnability, count=count, id=text['id']))

    response = json.dumps(dict(learnabilities=learnabilities))

    return flask.Response(response, status=200, mimetype='application/json')
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
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.model import UserExerciseSession
        UserExerciseSession.update_exercise_session(exercise, db_session)

        zeeguu.log("recomputting bookmark priorities")
        BookmarkPriorityUpdater.update_bookmark_priority(
            zeeguu.db, flask.g.user)

        return "OK"
    except:
        traceback.print_exc()
        return "FAIL"
Exemplo n.º 12
0
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:
    """

    word_str = unquote_plus(request.form['word'])

    url_str = request.form.get('url', '')
    title_str = request.form.get('title', '')
    context_str = request.form.get('context', '')

    try:
        minimal_context, query = minimize_context(context_str, from_lang_code,
                                                  word_str)
        translator = Translator(from_lang_code, to_lang_code)
        translations = translator.translate(query).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)
    except ValueError as e:
        zeeguu.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))
Exemplo n.º 13
0
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"
Exemplo n.º 14
0
def create_default_bookmarks(session, user, language_code):
    bookmarks = []

    try:

        print("trying to load default bookmarks for " + str(user.name))
        for data_point in bookmark_data[language_code]:
            bookmark = Bookmark.find_or_create(session, user, data_point[0],
                                               language_code, data_point[1],
                                               "en", data_point[2],
                                               data_point[3],
                                               "Zeeguu Exercises")
            bookmarks.append(bookmark)

    except Exception as e:
        zeeguu.log(
            "could not load default bookmarks for {0}".format(language_code))
        #raise e
        return []

    return bookmarks
Exemplo n.º 15
0
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'
Exemplo n.º 16
0
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_str = request.form.get('url', '')
    context_str = request.form.get('context', '')
    title_str = request.form.get('title', '')

    # 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_str,
                                       title_str)

    return json_result(dict(bookmark_id=bookmark.id))
Exemplo n.º 17
0
    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
Exemplo n.º 18
0
    def test_exists(self):
        random_bookmark = self.user.all_bookmarks()[0]

        assert Bookmark.exists(random_bookmark)
Exemplo n.º 19
0
    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
Exemplo n.º 20
0
    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
Exemplo n.º 21
0
    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
Exemplo n.º 22
0
    def find_all_for_user_and_text(self):
        bookmark_should_be = self.user.all_bookmarks()[0]
        bookmark_to_check = Bookmark.find_all_for_user_and_text(
            bookmark_should_be.text, self.user)

        assert bookmark_should_be in bookmark_to_check
Exemplo n.º 23
0
    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
Exemplo n.º 24
0
def unstar_bookmark(bookmark_id):
    bookmark = Bookmark.find(bookmark_id)
    bookmark.starred = False
    db_session.commit()
    return "OK"
Exemplo n.º 25
0
 def all_bookmarks(self, user):
     from zeeguu.model import Bookmark
     return Bookmark.find_all_for_user_and_text(self, user)
Exemplo n.º 26
0
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"
Exemplo n.º 27
0
def delete_bookmark(bookmark_id):
    bookmark = Bookmark.find(bookmark_id)
    db_session.delete(bookmark)
    db_session.commit()
    return "OK"