Example #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"
Example #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"
Example #3
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'
Example #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")
Example #5
0
def available_languages():
    """
    :return: jason with language codes for the
    supported languages.
    e.g. ["en", "fr", "de", "it", "no", "ro"]
    """
    available_language_codes = map((lambda x: x.id), (Language.available_languages()))
    return json.dumps(available_language_codes)
Example #6
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")
Example #7
0
def available_native_languages():
    """
    :return: jason with language codes for the
    supported native languages. curently only english...
    e.g. ["en", "fr", "de", "it", "no", "ro"]unquote_plus(flask.r
    """
    available_language_codes = list(
        map((lambda x: x.code), Language.native_languages()))
    return json.dumps(available_language_codes)
Example #8
0
def available_languages():
    """
    :return: jason with language codes for the
    supported languages.
    e.g. ["en", "fr", "de", "it", "no", "ro"]
    """
    available_language_codes = list(
        map((lambda x: x.code), Language.available_languages()))
    return json.dumps(available_language_codes)
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()
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()
Example #11
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 #12
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 #13
0
    def __init__(self):
        super().__init__()

        self.rss_feed = self._create_model_object()
        self.feed = self.rss_feed
        self.save(self.rss_feed)

        lang1 = Language.find_or_create(LANG_OF_FEED_ONE)
        url = Url.find_or_create(self.db.session, URL_OF_FEED_ONE)
        image_url = Url.find_or_create(self.db.session, IMG_URL_OF_FEED_ONE)
        self.feed1 = RSSFeed.find_or_create(self.db.session, url, "", "", image_url=image_url,
                                            language=lang1)
        self.save(self.feed1)

        lang2 = Language.find_or_create(LANG_OF_FEED_TWO)
        url2 = Url.find_or_create(self.db.session, URL_OF_FEED_TWO)
        image_url2 = Url.find_or_create(self.db.session, IMG_URL_OF_FEED_TWO)
        self.feed2 = RSSFeed.find_or_create(self.db.session,
                                            url2, "", "", image_url=image_url2, language=lang2)
        self.save(self.feed2)
Example #14
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)
Example #15
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)
Example #16
0
def create_own_cohort():
    '''
        Creates a cohort in the database.
        Requires form input (inv_code, name, language_id, max_students, teacher_id)

    '''
    if not is_teacher(flask.g.user.id):
        flask.abort(401)
    inv_code = request.form.get("inv_code")
    name = request.form.get("name")
    language_id = request.form.get("language_id")
    if name is None or inv_code is None or language_id is None:
        flask.abort(400)
    available_languages = Language.available_languages()
    code_allowed = False
    for code in available_languages:
        if language_id in str(code):
            code_allowed = True

    if not code_allowed:
        flask.abort(400)
    language = Language.find_or_create(language_id)
    teacher_id = flask.g.user.id
    max_students = request.form.get("max_students")
    if int(max_students) < 1:
        flask.abort(400)

    try:
        c = Cohort(inv_code, name, language, max_students)
        db.session.add(c)
        db.session.commit()
        _link_teacher_cohort(teacher_id, c.id)
        return "OK"
    except ValueError:
        flask.abort(400)
        return "ValueError"
    except sqlalchemy.exc.IntegrityError:
        flask.abort(400)
        return "IntegrityError"
Example #17
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
Example #18
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
Example #19
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
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'
Example #22
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
Example #23
0
def create_test_db():
    zeeguu.app.test_request_context().push()

    zeeguu.db.session.commit()
    zeeguu.db.drop_all()
    zeeguu.db.create_all()

    fr = Language("fr", "French")
    de = Language("de", "German")
    dk = Language("dk", "Danish")
    en = Language("en", "English")
    it = Language("it", "Italian")
    no = Language("no", "Norwegian")
    ro = Language("ro", "Romanian")
    es = Language("es", "Spanish")

    zeeguu.db.session.add(en)
    zeeguu.db.session.add(fr)
    zeeguu.db.session.add(de)
    zeeguu.db.session.add(dk)
    zeeguu.db.session.add(no)
    zeeguu.db.session.add(it)
    zeeguu.db.session.add(ro)
    zeeguu.db.session.add(es)
    zeeguu.db.session.commit()

    show_solution = ExerciseOutcome("Show solution")
    retry = ExerciseOutcome("Retry")
    correct = ExerciseOutcome("Correct")
    wrong = ExerciseOutcome("Wrong")
    typo = ExerciseOutcome("Typo")
    too_easy = ExerciseOutcome("Too easy")

    recognize = ExerciseSource("Recognize")
    translate = ExerciseSource("Translate")

    zeeguu.db.session.add(show_solution)
    zeeguu.db.session.add(retry)
    zeeguu.db.session.add(correct)
    zeeguu.db.session.add(wrong)
    zeeguu.db.session.add(typo)
    zeeguu.db.session.add(too_easy)

    zeeguu.db.session.add(recognize)
    zeeguu.db.session.add(translate)

    user = User("*****@*****.**", "Mircea", "pass", de, ro)
    user2 = User("*****@*****.**", "Ada", "pass", fr)

    zeeguu.db.session.add(user)
    zeeguu.db.session.add(user2)

    jan111 = datetime.date(2011, 01, 01)
    ian101 = datetime.date(2001, 01, 01)
    jan14 = datetime.date(2014, 1, 14)

    today_dict = {
        'sogar': 'actually',
        'sperren': 'to lock, to close',
        'Gitter': 'grates',
        'erfahren': 'to experience',
        'treffen': 'hit',
        'jeweils': 'always',
        'Darstellung': 'presentation',
        'Vertreter': 'representative',
        'Knecht': 'servant',
        'besteht': 'smtg. exists'
    }

    dict = {
        u'Spaß': 'fun',
        'solche': 'suchlike',
        'ehemaliger': 'ex',
        'betroffen': 'affected',
        'Ufer': 'shore',
        u'höchstens': 'at most'
    }

    french_dict = {'jambes': 'legs', 'de': 'of', 'et': 'and'}

    story_url = 'http://www.gutenberg.org/files/23393/23393-h/23393-h.htm'
    japanese_story = [
        # ['recht', 'right', 'Du hast recht', story_url],
        [
            'hauen', 'to chop', 'Da waren einmal zwei Holzhauer können',
            story_url
        ],
        [
            u'Wald', 'to arrive',
            u'Um in den Walden zu gelangen, mußten sie einen großen Fluß passieren. Um in den Walden zu gelangen, mußten sie einen großen Fluß passieren. Um in den Walden zu gelangen, mußten sie einen großen Fluß passieren. Um in den Walden zu gelangen, mußten sie einen großen Fluß passieren',
            story_url
        ],
        [
            'eingerichtet', 'established',
            u'Um in den Wald zu gelangen, mußten sie einen großen Fluß passieren, über den eine Fähre eingerichtet war',
            story_url
        ],
        [
            u'vorläufig', 'temporary',
            u'von der er des rasenden Sturmes wegen vorläufig nicht zurück konnte',
            story_url
        ],
        [
            u'werfen', 'to throw',
            u'Im Hause angekommen, warfen sie sich zur Erde,', story_url
        ],
        [
            'Tosen', 'roar',
            u'sie Tür und Fenster wohl verwahrt hatten und lauschten dem Tosen des Sturmes.sie Tür und Fenster wohl verwahrt hatten und lauschten dem Tosen des Sturmes.sie Tür und Fenster wohl verwahrt hatten und lauschten dem Tosen des Sturmes',
            story_url
        ],
        [
            'Entsetzen', 'horror', 'Entsetzt starrte Teramichi auf die Wolke',
            story_url
        ]
    ]

    add_ranked_word_to_db('de')

    for key in today_dict:
        add_bookmark(
            user, de, key, en, today_dict[key], jan111,
            "Keine bank durfe auf immunitat pochen, nur weil sie eine besonders herausgehobene bedeutung fur das finanzsystem habe, sagte holder, ohne namen von banken zu nennen"
            + key, "http://url2", "title of url2")

    for key in dict:
        add_bookmark(
            user, de, key, en, dict[key], ian101,
            "Deutlich uber dem medianlohn liegen beispielsweise forschung und entwicklung, tabakverarbeitung, pharma oder bankenwesen, am unteren ende der skala liegen die tieflohnbranchen detailhandel, gastronomie oder personliche dienstleistungen. "
            + key, "http://url1", "title of url1")

    for key in french_dict:
        add_bookmark(
            user, de, key, en, french_dict[key], ian101,
            "Deutlich uber dem medianlohn liegen beispielsweise forschung und entwicklung, tabakverarbeitung, pharma oder bankenwesen, am unteren ende der skala liegen die tieflohnbranchen detailhandel, gastronomie oder personliche dienstleistungen. "
            + key, "http://url1", "title of url1")
    for w in japanese_story:
        add_bookmark(user, de, w[0], en, w[1], jan14, w[2], w[3],
                     "japanese story")

    zeeguu.db.session.commit()
Example #24
0
test_feed = test_feed(_feed_url)

feed_name = input(
    f"Feed name (Enter for: {test_feed.title}):  ") or test_feed.title
print(f'= {feed_name}')

icon_name = input(
    "Icon name to be found in resources folder (e.g. 20min.png):  ")
print(f'= {icon_name}')

description = input(f'Description (Enter for: {test_feed.description}): '
                    ) or test_feed.description
print(f'= {description}')

_language = input("Language code (e.g. en): ")
print(f'= {_language}')

icon_url = Url.find_or_create(zeeguu.db.session, RESOURCES_FOLDER + icon_name)
feed_url = Url.find_or_create(zeeguu.db.session, _feed_url)
language = Language.find_or_create(_language)

rss_feed = RSSFeed.find_or_create(zeeguu.db.session, feed_url, feed_name,
                                  description, icon_url, language)

print("Done: ")
print(rss_feed.title)
print(rss_feed.description)
print(rss_feed.language_id)
print(rss_feed.url.as_string())
print(rss_feed.image_url.as_string())
Example #25
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 #26
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 #27
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
Example #28
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
Example #29
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
Example #30
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