예제 #1
0
 def cache_ranked_words(cls):
     cls.ranked_words_cache = {}
     for language in Language.all():
         ranked_words = cls.find_all(language)
         for ranked_word in ranked_words:
             ranked_word_key = language.id + '_' + ranked_word.word
             cls.ranked_words_cache[ranked_word_key] = ranked_word
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 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'
예제 #4
0
def create_account():

    # A cool way of passing the arguments to the flask template
    template_arguments = dict (
         languages= Language.all(),
         native_languages = Language.native_languages(),
         default_learned= Language.default_learned()
    )

    # GET
    if flask.request.method == "GET":
        return flask.render_template("create_account.html", **template_arguments)

    # POST
    form = flask.request.form
    password = form.get("password", None)
    email = form.get("email", None)
    name = form.get("name", None)
    code = form.get("code", None)
    language = Language.find(form.get("language", None))
    native_language = Language.find(form.get("native_language", None))

    if not (code == "Kairo" or code == "unibe" or code == "rug" or code =="42"):
        flash("Invitation code is not recognized. Please contact us.")

    elif password is None or email is None or name is None:
        flash("Please enter your name, email address, and password")

    else:
        try:

            zeeguu.db.session.add(User(email, name, password, language, native_language))
            zeeguu.db.session.commit()
            user = User.authorize(email, password)
            flask.session["user"] = user.id
            return flask.redirect(flask.url_for("account.my_account"))

        except ValueError:
            flash("Username could not be created. Please contact us.")
        except sqlalchemy.exc.IntegrityError:
            flash(email + " is already in use. Please select a different email.")
        except:
            flash("Something went wrong. Please contact us.")
        finally:
            zeeguu.db.session.rollback()

    return flask.render_template("create_account.html", **template_arguments)
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')