예제 #1
0
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"
예제 #2
0
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"
예제 #3
0
    def setUp(self):
        # Superclass does prepare the DB before each of the tests
        super(Dbtest, self).setUp()

        # Some common test fixtures
        self.mir = model.User.find("*****@*****.**")
        assert self.mir
        self.de = Language.find("de")
예제 #4
0
    def setUp(self):
        # Superclass does prepare the DB before each of the tests
        super(Dbtest, self).setUp()

        # Some common test fixtures
        self.mir = model.User.find("*****@*****.**")
        assert self.mir
        self.de = Language.find("de")
예제 #5
0
def add_ranked_words_to_db(lang_code):
    zeeguu.app.test_request_context().push()
    zeeguu.db.session.commit()
    from_lang = Language.find(lang_code)
    initial_line_number = 1
    for word in filter_word_list(word_list(lang_code)):
        r = RankedWord(word.lower(), from_lang,initial_line_number)
        zeeguu.db.session.add(r)
        initial_line_number+=1
    zeeguu.db.session.commit()
예제 #6
0
def add_ranked_words_to_db(lang_code):
    zeeguu.app.test_request_context().push()
    zeeguu.db.session.commit()
    from_lang = Language.find(lang_code)
    initial_line_number = 1
    for word in filter_word_list(word_list(lang_code)):
        r = RankedWord(word.lower(), from_lang, initial_line_number)
        zeeguu.db.session.add(r)
        initial_line_number += 1
    zeeguu.db.session.commit()
예제 #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')
예제 #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')
예제 #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)
예제 #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)
예제 #11
0
def get_known_words(lang_code):
    lang_id = Language.find(lang_code)
    bookmarks = flask.g.user.all_bookmarks()
    known_words = []
    filtered_known_words_from_user = []
    filtered_known_words_dict_list = []
    for bookmark in bookmarks:
        if bookmark.check_is_latest_outcome_too_easy():
            known_words.append(bookmark.origin.word)
    for word_known in known_words:
        if RankedWord.exists(word_known, lang_id):
            filtered_known_words_from_user.append(word_known)
            zeeguu.db.session.commit()
    filtered_known_words_from_user = list(set(filtered_known_words_from_user))
    for word in filtered_known_words_from_user:
        filtered_known_words_dict_list.append({'word': word})
    js = json.dumps(filtered_known_words_dict_list)
    resp = flask.Response(js, status=200, mimetype='application/json')
    return resp
예제 #12
0
def get_learned_bookmarks(lang):
    lang = Language.find(lang)
    bookmarks = flask.g.user.all_bookmarks()
    too_easy_bookmarks=[]
    learned_bookmarks_dict_list =[]
    for bookmark in bookmarks:
        if bookmark.check_is_latest_outcome_too_easy() and bookmark.origin.language == lang:
                too_easy_bookmarks.append(bookmark)
    learned_bookmarks= [bookmark for bookmark in bookmarks if bookmark not in too_easy_bookmarks]
    for bookmark in learned_bookmarks:
        learned_bookmarks_dict = {}
        learned_bookmarks_dict ['id'] = bookmark.id
        learned_bookmarks_dict ['origin'] = bookmark.origin.word
        learned_bookmarks_dict['text'] = bookmark.text.content
        learned_bookmarks_dict_list.append(learned_bookmarks_dict)

    js = json.dumps(learned_bookmarks_dict_list)
    resp = flask.Response(js, status=200, mimetype='application/json')
    return resp
예제 #13
0
def get_known_words(lang_code):
    lang_id = Language.find(lang_code)
    bookmarks = flask.g.user.all_bookmarks()
    known_words=[]
    filtered_known_words_from_user = []
    filtered_known_words_dict_list =[]
    for bookmark in bookmarks:
        if bookmark.check_is_latest_outcome_too_easy():
                known_words.append(bookmark.origin.word)
    for word_known in known_words:
        if RankedWord.exists(word_known, lang_id):
            filtered_known_words_from_user.append(word_known)
            zeeguu.db.session.commit()
    filtered_known_words_from_user = list(set(filtered_known_words_from_user))
    for word in filtered_known_words_from_user:
        filtered_known_words_dict_list.append( {'word': word} )
    js = json.dumps(filtered_known_words_dict_list)
    resp = flask.Response(js, status=200, mimetype='application/json')
    return resp
예제 #14
0
def get_learned_bookmarks(lang):
    lang = Language.find(lang)
    bookmarks = flask.g.user.all_bookmarks()
    too_easy_bookmarks = []
    learned_bookmarks_dict_list = []
    for bookmark in bookmarks:
        if bookmark.check_is_latest_outcome_too_easy(
        ) and bookmark.origin.language == lang:
            too_easy_bookmarks.append(bookmark)
    learned_bookmarks = [
        bookmark for bookmark in bookmarks
        if bookmark not in too_easy_bookmarks
    ]
    for bookmark in learned_bookmarks:
        learned_bookmarks_dict = {}
        learned_bookmarks_dict['id'] = bookmark.id
        learned_bookmarks_dict['origin'] = bookmark.origin.word
        learned_bookmarks_dict['text'] = bookmark.text.content
        learned_bookmarks_dict_list.append(learned_bookmarks_dict)

    js = json.dumps(learned_bookmarks_dict_list)
    resp = flask.Response(js, status=200, mimetype='application/json')
    return resp
예제 #15
0
def get_difficulty_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 difficulty for. Each text consists of an array
        with the text itself as 'content' and an additional 'id' which gets roundtripped unchanged
    :param personalized (optional): calculate difficulty score for a specific user? (Enabled by default)
    :param rank_boundary (optional): upper boundary for word frequency rank (between 1 and 10'000)

    :return difficulties: json array, contains the difficulties as arrays with the key 'score_median' for the median
        and 'score_average' for the average difficulty the value (between 0 (easy) and 1 (hard)) 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'

    personalized = True
    if 'personalized' in data:
        personalized = data['personalized'].lower()
        if personalized == 'false' or personalized == '0':
            personalized = False

    rank_boundary = 10000.0
    if 'rank_boundary' in data:
        rank_boundary = float(data['rank_boundary'])
        if rank_boundary > 10000.0:
            rank_boundary = 10000.0

    user = flask.g.user
    known_probabilities = KnownWordProbability.find_all_by_user_cached(user)

    difficulties = []
    for text in texts:
        # Calculate difficulty for each word
        words = util.split_words_from_text(text['content'])
        words_difficulty = []
        for word in words:
            ranked_word = RankedWord.find_cache(word, language)

            word_difficulty = 1.0  # Value between 0 (easy) and 1 (hard)
            if ranked_word is not None:
                # Check if the user knows the word
                try:
                    known_propability = known_probabilities[
                        word]  # Value between 0 (unknown) and 1 (known)
                except KeyError:
                    known_propability = None

                if personalized and known_propability is not None:
                    word_difficulty -= float(known_propability)
                elif ranked_word.rank <= rank_boundary:
                    word_frequency = (
                        rank_boundary - (ranked_word.rank - 1)
                    ) / rank_boundary  # Value between 0 (rare) and 1 (frequent)
                    word_difficulty -= word_frequency

            words_difficulty.append(word_difficulty)

        # Uncomment to print data for histogram generation
        #text.generate_histogram(words_difficulty)

        # Median difficulty for text
        words_difficulty.sort()
        center = int(round(len(words_difficulty) / 2, 0))
        difficulty_median = words_difficulty[center]

        # Average difficulty for text
        difficulty_average = sum(words_difficulty) / float(
            len(words_difficulty))

        difficulties.append(
            dict(score_median=difficulty_median,
                 score_average=difficulty_average,
                 id=text['id']))

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

    return flask.Response(response, status=200, mimetype='application/json')
예제 #16
0
def get_not_looked_up_words(lang_code):
    js = json.dumps(
        flask.g.user.get_not_looked_up_words(Language.find(lang_code)))
    resp = flask.Response(js, status=200, mimetype='application/json')
    return resp
예제 #17
0
def get_known_bookmarks(lang_code):
    js = json.dumps(flask.g.user.get_known_bookmarks(Language.find(lang_code)))
    resp = flask.Response(js, status=200, mimetype='application/json')
    return resp
예제 #18
0
def get_difficulty_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 difficulty for. Each text consists of an array
        with the text itself as 'content' and an additional 'id' which gets roundtripped unchanged
    :param personalized (optional): calculate difficulty score for a specific user? (Enabled by default)
    :param rank_boundary (optional): upper boundary for word frequency rank (between 1 and 10'000)

    :return difficulties: json array, contains the difficulties as arrays with the key 'score_median' for the median
        and 'score_average' for the average difficulty the value (between 0 (easy) and 1 (hard)) 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'

    personalized = True
    if 'personalized' in data:
        personalized = data['personalized'].lower()
        if personalized == 'false' or personalized == '0':
            personalized = False

    rank_boundary = 10000.0
    if 'rank_boundary' in data:
        rank_boundary = float(data['rank_boundary'])
        if rank_boundary > 10000.0:
            rank_boundary = 10000.0

    user = flask.g.user
    known_probabilities = KnownWordProbability.find_all_by_user_cached(user)

    difficulties = []
    for text in texts:
        # Calculate difficulty for each word
        words = util.split_words_from_text(text['content'])
        words_difficulty = []
        for word in words:
            ranked_word = RankedWord.find_cache(word, language)

            word_difficulty = 1.0 # Value between 0 (easy) and 1 (hard)
            if ranked_word is not None:
                # Check if the user knows the word
                try:
                    known_propability = known_probabilities[word] # Value between 0 (unknown) and 1 (known)
                except KeyError:
                    known_propability = None

                if personalized and known_propability is not None:
                    word_difficulty -= float(known_propability)
                elif ranked_word.rank <= rank_boundary:
                    word_frequency = (rank_boundary-(ranked_word.rank-1))/rank_boundary # Value between 0 (rare) and 1 (frequent)
                    word_difficulty -= word_frequency

            words_difficulty.append(word_difficulty)

        # Uncomment to print data for histogram generation
        #text.generate_histogram(words_difficulty)

        # Median difficulty for text
        words_difficulty.sort()
        center = int(round(len(words_difficulty)/2, 0))
        difficulty_median = words_difficulty[center]

        # Average difficulty for text
        difficulty_average = sum(words_difficulty) / float(len(words_difficulty))

        difficulties.append(dict(score_median=difficulty_median, score_average=difficulty_average, id=text['id']))

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

    return flask.Response(response, status=200, mimetype='application/json')
예제 #19
0
def get_not_looked_up_words(lang_code):
    js = json.dumps(flask.g.user.get_not_looked_up_words(Language.find(lang_code)))
    resp = flask.Response(js, status=200, mimetype='application/json')
    return resp
예제 #20
0
def get_known_bookmarks(lang_code):
    js = json.dumps(flask.g.user.get_known_bookmarks(Language.find(lang_code)))
    resp = flask.Response(js, status=200, mimetype='application/json')
    return resp