Example #1
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')
Example #2
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')
Example #3
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')
Example #4
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')