Exemplo n.º 1
0
class UserWord(db.Model, util.JSONSerializable):
    __tablename__ = 'user_words'
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    word = db.Column(db.String(255), nullable=False, unique=True)
    language_id = db.Column(db.String(2), db.ForeignKey("language.id"))
    language = db.relationship("Language")
    rank_id = db.Column(db.Integer,
                        db.ForeignKey("word_ranks.id"),
                        nullable=True)
    rank = db.relationship("WordRank")
    db.UniqueConstraint(word, language_id)

    IMPORTANCE_LEVEL_STEP = 1000
    IMPOSSIBLE_RANK = 1000000
    IMPOSSIBLE_IMPORTANCE_LEVEL = IMPOSSIBLE_RANK / IMPORTANCE_LEVEL_STEP

    def __init__(self, word, language, rank=None):
        self.word = word
        self.language = language
        self.rank = rank

    def __repr__(self):
        return '<UserWord %r>' % (self.word)

    def serialize(self):
        return self.word

    # returns a number between
    def importance_level(self):
        if self.rank is not None:
            return max((10 - self.rank.rank / UserWord.IMPORTANCE_LEVEL_STEP),
                       0)
        else:
            return 0

    # we use this in the bookmarks.html to show the rank.
    # for words in which there is no rank info, we don't display anything
    def importance_level_string(self):
        if self.rank == None:
            return ""
        b = "|"
        return b * self.importance_level()

    @classmethod
    def find(cls, word, language, rank=None):
        try:
            return (cls.query.filter(cls.word == word).filter(
                cls.language == language).one())
        except sqlalchemy.orm.exc.NoResultFound:
            return cls(word, language, rank)

    @classmethod
    def find_rank(cls, word, language):
        return WordRank.find(word, language)

    @classmethod
    def find_all(cls):
        return cls.query.all()
Exemplo n.º 2
0
class Url(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(2083))

    path = db.Column(db.String(2083))

    url = db.Column(db.String(2083))

    domain_name_id = db.Column(db.Integer, db.ForeignKey("domain_name.id"))
    domain = db.relationship("DomainName")

    def __init__(self, url, title):
        self.path = Url.get_path(url)
        self.domain = DomainName.find(Url.get_domain(url))
        self.title = title

    def title_if_available(self):
        if self.title != "":
            return self.title
        return self.url

    def as_string(self):
        return self.domain.domain_name + self.path

    def domain_name(self):
        return self.domain.domain_name

    @classmethod
    def get_domain(self, url):
        protocol_re = '(.*://)?'
        domain_re = '([^/?]*)'
        path_re = '(.*)'

        domain = re.findall(protocol_re + domain_re, url)[0]
        return domain[0] + domain[1]

    @classmethod
    def get_path(self, url):
        protocol_re = '(.*://)?'
        domain_re = '([^/?]*)'
        path_re = '(.*)'

        domain = re.findall(protocol_re + domain_re + path_re, url)[0]
        return domain[2]

    @classmethod
    def find(cls, url, title=""):
        try:
            d = DomainName.find(Url.get_domain(url))
            return (cls.query.filter(cls.path == Url.get_path(url)).filter(
                cls.domain == d).one())
        except sqlalchemy.orm.exc.NoResultFound:
            return cls(url, title)

    def render_link(self, link_text):
        if self.url != "":
            return '<a href="' + self.url + '">' + link_text + '</a>'
        else:
            return ""
Exemplo n.º 3
0
class Card(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    position = db.Column(db.Integer)
    bookmark_id = db.Column(db.Integer, db.ForeignKey('bookmark.id'))
    bookmark = db.relationship("Bookmark", backref="card")
    last_seen = db.Column(db.DateTime)

    def __init__(self, bookmark):
        self.bookmark = bookmark
        self.position = 0
        self.reason = ""
        self.seen()

    def seen(self):
        self.last_seen = datetime.datetime.now()

    def set_reason(self, reason):
        self.reason = reason

    def reason(self):
        return self.reason

    def is_starred(self):
        return self.bookmark.user.has_starred(self.bookmark.origin)

    def star(self):
        word = self.bookmark.origin
        self.bookmark.user.starred_words.append(word)

    def unstar(self):
        word = self.bookmark.origin
        self.bookmark.user.starred_words.remove(word)
Exemplo n.º 4
0
class Url(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}
    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.String(2083))
    title = db.Column(db.String(2083))

    def __init__(self, url, title):
        self.url = url
        self.title = title

    def title_if_available(self):
        if self.title != "":
            return self.title
        return self.url

    @classmethod
    def find(cls, url, title):
        try:
            return (cls.query.filter(cls.url == url).one())
        except sqlalchemy.orm.exc.NoResultFound:
            return cls(url, title)

    def render_link(self, link_text):
        if self.url != "":
            return '<a href="' + self.url + '">' + link_text + '</a>'
        else:
            return ""
Exemplo n.º 5
0
class Language(db.Model):
    id = db.Column(db.String(2), primary_key=True)
    name = db.Column(db.String(255), unique=True)

    def __init__(self, id, name):
        self.name = name
        self.id = id

    def __repr__(self):
        return '<Language %r>' % (self.id)

    @classmethod
    def default_learned(cls):
        return cls.find("de")

    @classmethod
    def default_native_language(cls):
        return cls.find("en")

    @classmethod
    def native_languages(cls):
        return [cls.find("en"), cls.find("ro")]

    @classmethod
    def available_languages(cls):
        return cls.all()

    @classmethod
    def find(cls, id_):
        return cls.query.filter(Language.id == id_).one()

    @classmethod
    def all(cls):
        return cls.query.filter().all()
Exemplo n.º 6
0
class Card(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    position = db.Column(db.Integer)
    contribution_id = db.Column(db.Integer, db.ForeignKey('contribution.id'))
    contribution = db.relationship("Contribution", backref="card")
    last_seen = db.Column(db.DateTime)

    def __init__(self, contribution):
        self.contribution = contribution
        self.position = 0
        self.reason = ""
        self.seen()

    def seen(self):
        self.last_seen = datetime.datetime.now()

    def set_reason(self, reason):
        self.reason = reason

    def reason(self):
        return self.reason

    def is_starred(self):
        return self.contribution.user.has_starred(self.contribution.origin)

    def star(self):
        word = self.contribution.origin
        self.contribution.user.starred_words.append(word)
        print "starred the hell out of... " + self.contribution.origin.word

    def unstar(self):
        word = self.contribution.origin
        self.contribution.user.starred_words.remove(word)
        print "just unstarred ..." + self.contribution.origin.word
Exemplo n.º 7
0
class ExerciseSource(db.Model):
    __tablename__ = 'exercise_source'
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    source = db.Column(db.String(255), nullable=False)

    def __init__(self, source):
        self.source = source
class WatchInteractionEvent(db.Model):
    __tablename__ = 'watch_interaction_event'
    __table_args__ = dict(mysql_collate='utf8_bin')

    id = db.Column(db.Integer, primary_key=True)

    time = db.Column(db.DateTime)

    bookmark_id = db.Column(db.Integer,
                            db.ForeignKey('bookmark.id'),
                            nullable=False)
    bookmark = db.relationship("Bookmark")

    event_type_id = db.Column(db.Integer,
                              db.ForeignKey('watch_event_type.id'),
                              nullable=False)
    event_type = db.relationship("WatchEventType")

    def __init__(self, event_type, bookmark_id, time):

        self.time = time
        self.bookmark_id = bookmark_id
        self.event_type = event_type

    def data_as_dictionary(self):
        return dict(user_id=self.bookmark.user_id,
                    bookmark_id=self.bookmark_id,
                    time=self.time.strftime("%Y-%m-%dT%H:%M:%S"),
                    event=self.event_type.name)

    def is_learned_event(self):
        return self.event_type.name == "learnedIt"

    def is_wrong_translation_event(self):
        return self.event_type.name == "wrongTranslation"

    def prevents_further_study(self):
        """
        Some events prevent a bookmark for being good for study
        in the future
        :return:
        """
        return self.is_learned_event() or self.is_wrong_translation_event()

    @classmethod
    def events_for_bookmark(cls, bookmark):
        return cls.query.filter_by(bookmark_id=bookmark.id).all()

    @classmethod
    def events_for_bookmark_id(cls, bookmark_id):
        return cls.query.filter_by(bookmark_id=bookmark_id).all()

    @classmethod
    def events_for_user(cls, user):
        return cls.query.join(Bookmark).filter(
            Bookmark.user_id == user.id).all()
Exemplo n.º 9
0
class ExerciseSource(db.Model):
    __tablename__ = 'exercise_source'
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    source = db.Column(db.String(255), nullable=False)

    def __init__(self, source):
        self.source = source

    @classmethod
    def find_by_source(cls, source):
        new_source = ExerciseSource.query.filter_by(source=source).first()
        return new_source
Exemplo n.º 10
0
class RankedWord(db.Model, util.JSONSerializable):
    __tablename__ = 'ranked_word'
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    word = db.Column(db.String(255), nullable=False, unique=True, index=True)

    language_id = db.Column(db.String(2), db.ForeignKey("language.id"))
    language = db.relationship("Language")
    rank = db.Column(db.Integer)
    db.UniqueConstraint(word, language_id)

    def __init__(self, word, language, rank):
        self.word = word
        self.language = language
        self.rank = rank

    @classmethod
    def find(cls, word, language):
        word = word.lower()
        try:
            return (cls.query.filter(cls.word == word).filter(
                cls.language == language).one())
        except sqlalchemy.orm.exc.NoResultFound:
            return None

    @classmethod
    def find_all(cls, language):
        return cls.query.filter(cls.language == language).all()

    @classmethod
    def exists(cls, word, language):
        word = word.lower()
        try:
            (cls.query.filter(cls.word == word).filter(
                cls.language == language).one())
            return True
        except sqlalchemy.orm.exc.NoResultFound:
            return False

    @classmethod
    def words_list(cls):
        words_list = []
        for word in cls.find_all():
            words_list.append(word.word)
        return words_list
Exemplo n.º 11
0
class ExerciseOutcome(db.Model):
    __tablename__ = 'exercise_outcome'
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    outcome = db.Column(db.String(255), nullable=False)

    IKNOW = 'I know'
    NOT_KNOW = 'Do not know'

    def __init__(self, outcome):
        self.outcome = outcome

    @classmethod
    def find(cls, outcome):
        try:
            return cls.query.filter_by(outcome=outcome).first()
        except sqlalchemy.orm.exc.NoResultFound:
            return cls(outcome)
Exemplo n.º 12
0
class Session(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User")
    last_use = db.Column(db.DateTime)

    def __init__(self, user, id_):
        self.id = id_
        self.user = user
        self.update_use_date()

    def update_use_date(self):
        self.last_use = datetime.datetime.now()

    @classmethod
    def for_user(cls, user):
        while True:
            id_ = random.randint(0, zeeguu.app.config.get("MAX_SESSION"))
            if cls.query.get(id_) is None:
                break
        return cls(user, id_)

    @classmethod
    def find_for_id(cls, session_id):
        try:
            return cls.query.filter(cls.id == session_id).one()
        except:
            return None

    # @classmethod
    # def find_for_user(cls, user):
    #     return cls.query.filter(cls.user == user).order_by(desc(cls.last_use)).first()

    @classmethod
    def find_for_user(cls, user):
        s = cls.query.filter(cls.user == user).\
            filter(cls.id < zeeguu.app.config.get("MAX_SESSION")).\
            order_by(desc(cls.last_use)).first()
        if not s:
            s = cls.for_user(user)
        return s
Exemplo n.º 13
0
class RSSFeedRegistration(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}
    __tablename__ = 'rss_feed_registration'

    id = db.Column(db.Integer, primary_key=True)

    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User")

    rss_feed_id = db.Column(db.Integer, db.ForeignKey("rss_feed.id"))
    rss_feed = relationship("RSSFeed")

    def __init__(self, user, feed):
        self.user = user
        self.rss_feed = feed

    @classmethod
    def find_or_create(cls, user, feed):
        try:
            return (cls.query.filter(cls.user == user).filter(
                cls.rss_feed == feed).one())
        except sqlalchemy.orm.exc.NoResultFound:
            return cls(user, feed)

    @classmethod
    def feeds_for_user(cls, user):
        """
        would have been nicer to define a method on the User class get feeds,
        but that would pollute the user model, and it's not nice.
        :param user:
        :return:
        """
        return (cls.query.filter(cls.user == user))

    @classmethod
    def with_id(cls, id):
        return (cls.query.filter(cls.id == id)).one()

    @classmethod
    def with_feed_id(cls, id, user):
        return (cls.query.filter(cls.rss_feed_id == id))\
                        .filter(cls.user_id == user.id).one()
Exemplo n.º 14
0
class WatchEventType(db.Model):
    __table_args__ = dict(mysql_collate='utf8_bin')
    __tablename__ = 'watch_event_type'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)

    def __init__(self, name):
        self.name = name

    @classmethod
    def find_by_name(cls, name):
        """
        :param name:
        :return: the desired object, or None if it does not exist
        """
        try:
            return WatchEventType.query.filter_by(name=name).first()
        except:
            return None
Exemplo n.º 15
0
class Exercise(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}
    __tablename__ = 'exercise'

    id = db.Column(db.Integer, primary_key=True)
    outcome_id = db.Column(db.Integer,
                           db.ForeignKey('exercise_outcome.id'),
                           nullable=False)
    outcome = db.relationship("ExerciseOutcome", backref="exercise")
    source_id = db.Column(db.Integer,
                          db.ForeignKey('exercise_source.id'),
                          nullable=False)
    source = db.relationship("ExerciseSource", backref="exercise")
    solving_speed = db.Column(db.Integer)
    time = db.Column(db.DateTime, nullable=False)

    def __init__(self, outcome, source, solving_speed, time):
        self.outcome = outcome
        self.source = source
        self.solving_speed = solving_speed
        self.time = time
Exemplo n.º 16
0
class Session(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User")
    last_use = db.Column(db.DateTime)

    def __init__(self, user, id_):
        self.id = id_
        self.user = user
        self.update_use_date()

    def update_use_date(self):
        self.last_use = datetime.datetime.now()

    @classmethod
    def for_user(cls, user):
        while True:
            id_ = random.randint(0, 1 << 31)
            if cls.query.get(id_) is None:
                break
        return cls(user, id_)
Exemplo n.º 17
0
class ExerciseOutcome(db.Model):
    __tablename__ = 'exercise_outcome'
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    outcome = db.Column(db.String(255), nullable=False)

    TOO_EASY = 'Too easy'
    SHOW_SOLUTION = 'Show solution'
    CORRECT = 'Correct'
    WRONG = 'Wrong'

    def __init__(self, outcome):
        self.outcome = outcome

    @classmethod
    def find(cls, outcome):
        try:
            return cls.query.filter_by(outcome=outcome).one()
        except sqlalchemy.orm.exc.NoResultFound:
            return cls(outcome)
class UserActivityData(db.Model):
    __table_args__ = dict(mysql_collate="utf8_bin")
    __tablename__ = 'user_activity_data'

    id = db.Column(db.Integer, primary_key=True)

    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User")

    time = db.Column(db.DateTime)

    event = db.Column(db.String(255))
    value = db.Column(db.String(255))
    extra_data = db.Column(db.String(4096))

    def __init__(self, user, time, event, value, extra_data):
        self.user = user
        self.time = time
        self.event = event
        self.value = value
        self.extra_data = extra_data

    def data_as_dictionary(self):
        return dict(user_id=self.user_id,
                    time=self.time.strftime("%Y-%m-%dT%H:%M:%S"),
                    event=self.event,
                    value=self.value,
                    extra_data=self.extra_data)
Exemplo n.º 19
0
class UniqueCode(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    code = db.Column(db.String(4))
    email = db.Column(db.String(255))
    time = db.Column(db.DateTime)

    def __init__(self, email):
        self.code = randint(100,999)
        self.email = email
        self.time = datetime.now()

    def __str__(self):
        return str(self.code)

    @classmethod
    def last_code(cls, email):
        return (cls.query.filter(cls.email == email).order_by(cls.time.desc()).first()).code

    @classmethod
    def all_codes_for(cls, email):
        return (cls.query.filter(cls.email == email)).all()
Exemplo n.º 20
0
class Language(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.String(2), primary_key=True)
    name = db.Column(db.String(255), unique=True)

    def __init__(self, id, name):
        self.name = name
        self.id = id

    def __repr__(self):
        return '<Language %r>' % (self.id)

    @classmethod
    def default_learned(cls):
        return cls.find("de")

    @classmethod
    def default_native_language(cls):
        return cls.find("en")

    @classmethod
    def native_languages(cls):
        return [cls.find("en")]

    @classmethod
    def available_languages(cls):
        return list(set(cls.all()) - set([Language.find("en")]))

    @classmethod
    def find(cls, id_):
        return cls.query.filter(Language.id == id_).one()

    @classmethod
    def all(cls):
        return cls.query.filter().all()
Exemplo n.º 21
0
class DomainName(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}
    __tablename__ = 'domain_name'

    id = db.Column(db.Integer, primary_key=True)
    domain_name = db.Column(db.String(2083))

    def __init__(self, url):
        self.domain_name = self.extract_domain_name(url)

    def extract_domain_name(self, url):
        protocol_re = '(.*://)?'
        domain_re = '([^/?]*)'

        domain = re.findall(protocol_re + domain_re, url)[0]
        return domain[0] + domain[1]

    @classmethod
    def find(cls, domain_url):
        try:
            return (cls.query.filter(cls.domain_name == domain_url).one())
        except sqlalchemy.orm.exc.NoResultFound:
            # print "tried, but didn't find " + domain_url
            return cls(domain_url)
Exemplo n.º 22
0
class Impression(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User", backref="impressions")
    word_id = db.Column(db.Integer, db.ForeignKey("word.id"))
    word = db.relationship("Word")
    text_id = db.Column(db.Integer, db.ForeignKey("text.id"))
    text = db.relationship("Text")
    count = db.Column(db.Integer)
    last_search_id = db.Column(db.Integer, db.ForeignKey("search.id"))
    last_search = db.relationship("Search")

    def __init__(self, user, word, text=None):
        self.user = user
        self.word = word
        self.text = text

    def __repr__(self):
        return '<Impression %r>' % (self.word.word)
Exemplo n.º 23
0
class Search(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User", backref="searches")
    word_id = db.Column(db.Integer, db.ForeignKey("word.id"))
    word = db.relationship("Word")
    language_id = db.Column(db.String(2), db.ForeignKey("language.id"))
    language = db.relationship("Language")
    text_id = db.Column(db.Integer, db.ForeignKey("text.id"))
    text = db.relationship("Text")
    contribution_id = db.Column(db.Integer, db.ForeignKey("contribution.id"))
    contribution = db.relationship("Contribution", backref="search")

    def __init__(self, user, word, language, text=None):
        self.user = user
        self.word = word
        self.language = language
        self.text = text

    def __repr__(self):
        return '<Search %r>' % (self.word.word)
Exemplo n.º 24
0
class Contribution(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    origin_id = db.Column(db.Integer, db.ForeignKey('word.id'))
    origin = db.relationship("Word", primaryjoin=origin_id == Word.id,
                             backref="translations")
    translation_id = db.Column(db.Integer, db.ForeignKey('word.id'))
    translation = db.relationship("Word",
                                  primaryjoin=translation_id == Word.id)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship("User", backref="contributions")

    text_id = db.Column(db.Integer, db.ForeignKey('text.id'))
    text = db.relationship("Text", backref="contributions")

    time = db.Column(db.DateTime)

    def __init__(self, origin, translation, user, text, time):
        self.origin = origin
        self.translation = translation
        self.user = user
        self.time = time
        self.text = text
Exemplo n.º 25
0
class Search(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User", backref="searches")
    word_id = db.Column(db.Integer, db.ForeignKey("user_words.id"))
    word = db.relationship("UserWord")
    language_id = db.Column(db.String(2), db.ForeignKey("language.id"))
    language = db.relationship("Language")
    text_id = db.Column(db.Integer, db.ForeignKey("text.id"))
    text = db.relationship("Text")
    bookmark_id = db.Column(db.Integer, db.ForeignKey("bookmark.id"))
    bookmark = db.relationship("Bookmark", backref="search")

    def __init__(self, user, word, language, text=None):
        self.user = user
        self.user_word = word
        self.language = language
        self.text = text

    def __repr__(self):
        return '<Search %r>' % (self.user_word.word)
Exemplo n.º 26
0
class EncounterBasedProbability(db.Model):
    __tablename__ = 'encounter_based_probability'
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    DEFAULT_PROBABILITY = 0.5

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
    user = db.relationship("User")
    ranked_word_id = db.Column(db.Integer,
                               db.ForeignKey("ranked_word.id"),
                               nullable=False)
    ranked_word = db.relationship("RankedWord")
    not_looked_up_counter = db.Column(db.Integer, nullable=False)
    probability = db.Column(db.DECIMAL(10, 9), nullable=False)
    db.UniqueConstraint(user_id, ranked_word_id)
    db.CheckConstraint('probability>=0', 'probability<=1')

    def __init__(self, user, ranked_word, not_looked_up_counter, probability):
        self.user = user
        self.ranked_word = ranked_word
        self.not_looked_up_counter = not_looked_up_counter
        self.probability = probability

    @classmethod
    def find(cls, user, ranked_word, default_probability=None):
        try:
            return cls.query.filter_by(user=user,
                                       ranked_word=ranked_word).one()
        except sqlalchemy.orm.exc.NoResultFound:
            return cls(user, ranked_word, 1, default_probability)

    @classmethod
    def find_all(cls):
        return cls.query.all()

    @classmethod
    def find_all_by_user(cls, user):
        return cls.query.filter_by(user=user).all()

    @classmethod
    def exists(cls, user, ranked_word):
        try:
            cls.query.filter_by(user=user, ranked_word=ranked_word).one()
            return True
        except sqlalchemy.orm.exc.NoResultFound:
            return False

    @classmethod
    def find_or_create(cls, word, user, language):
        ranked_word = RankedWord.find(word.lower(), language)
        if EncounterBasedProbability.exists(user, ranked_word):
            enc_prob = EncounterBasedProbability.find(user, ranked_word)
            enc_prob.not_looked_up_counter += 1
            enc_prob.boost_prob()
        else:
            enc_prob = EncounterBasedProbability.find(
                user, ranked_word,
                EncounterBasedProbability.DEFAULT_PROBABILITY)
        return enc_prob

    def reset_prob(self):
        # Why is this 0.5? Should be lower! I just looked up the word...
        # ugh...
        self.probability = 0.5

    def word_has_just_beek_bookmarked(self):
        """
            the user can't know this word very well if he's
            bookmarking it again
        :return:
        """

        self.probability /= 2

#         This function controls if prob is already 1.0, else it adds 0.1. It maximum adds 0.1, therefore cannot exceed 1

    def boost_prob(self):
        if float(self.probability) <> 1.0:
            self.probability = float(self.probability) + 0.1
Exemplo n.º 27
0
class Text(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(10000))

    content_hash = db.Column(db.LargeBinary(32))
    language_id = db.Column(db.String(2), db.ForeignKey("language.id"))
    language = db.relationship("Language")

    url_id = db.Column(db.Integer, db.ForeignKey('url.id'))
    url = db.relationship("Url", backref="texts")

    def __init__(self, content, language, url):
        self.content = content
        self.language = language
        self.url = url
        self.content_hash = util.text_hash(content)

    def __repr__(self):
        return '<Text %r>' % (self.language.short)

    def words(self):
        for word in re.split(re.compile(u"[^\\w]+", re.U), self.content):
            yield UserWord.find(word, self.language)

    def shorten_word_context(self, given_word, max_word_count):
        # shorter_text = ""
        limited_words = []

        words = self.content.split(
        )  # ==> gives me a list of the words ["these", "types", ",", "the"]
        word_count = len(words)

        if word_count <= max_word_count:
            return self.content

        for i in range(0, max_word_count):
            limited_words.append(
                words[i])  # lista cu primele max_length cuvinte
        shorter_text = ' '.join(
            limited_words)  # string cu primele 'max_word_count' cuv

        # sometimes the given_word does not exist in the text.
        # in that case return a text containing max_length words
        if given_word not in words:
            return shorter_text

        if words.index(given_word) <= max_word_count:
            return shorter_text

        for i in range(max_word_count + 1, words.index(given_word) + 1):
            limited_words.append(words[i])
        shorter_text = ' '.join(limited_words)

        return shorter_text

    @classmethod
    def find(cls, text, language):
        try:
            query = (cls.query.filter(cls.language == language).filter(
                cls.content_hash == util.text_hash(text)))
            if query.count() > 0:
                query = query.filter(cls.content == text)
                try:
                    return query.one()
                except sqlalchemy.orm.exc.NoResultFound:
                    pass
            return cls(text, language)
        except:
            import traceback
            traceback.print_exc()
Exemplo n.º 28
0
class Bookmark(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    origin_id = db.Column(db.Integer, db.ForeignKey('user_words.id'))
    origin = db.relationship("UserWord",
                             primaryjoin=origin_id == UserWord.id,
                             backref="translations")
    translations_list = relationship("UserWord",
                                     secondary="bookmark_translation_mapping")

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship("User", backref="bookmarks")

    text_id = db.Column(db.Integer, db.ForeignKey('text.id'))
    text = db.relationship("Text", backref="bookmarks")

    time = db.Column(db.DateTime)

    exercise_log = relationship("Exercise",
                                secondary="bookmark_exercise_mapping")

    def __init__(self, origin, translation, user, text, time):
        self.origin = origin
        self.translations_list.append(translation)
        self.user = user
        self.time = time
        self.text = text

    def add_new_exercise(self, exercise):
        self.exercise_log.append(exercise)

    def translation(self):
        return self.translations_list[0]

    def translations_rendered_as_text(self):
        return ", ".join(self.translation_words_list())

    def translation_words_list(self):
        translation_words = []
        for translation in self.translations_list:
            translation_words.append(translation.word)
        return translation_words

    def add_new_translation(self, translation):
        self.translations_list.append(translation)

    def remove_translation(self, translation):
        if translation in self.translations_list:
            self.translations_list.remove(translation)

    def add_exercise_outcome(self, exercise_source, exercise_outcome,
                             exercise_solving_speed):
        new_source = ExerciseSource.query.filter_by(
            source=exercise_source).first()
        new_outcome = ExerciseOutcome.query.filter_by(
            outcome=exercise_outcome).first()
        exercise = Exercise(new_outcome, new_source, exercise_solving_speed,
                            datetime.datetime.now())
        self.add_new_exercise(exercise)
        db.session.add(exercise)

    @classmethod
    def find_all_filtered_by_user(cls):
        return cls.query.filter_by(user=flask.g.user).all()

    @classmethod
    def find(cls, b_id):
        return cls.query.filter_by(id=b_id).first()

    @classmethod
    def is_sorted_exercise_log_after_date_outcome(cls, outcome, bookmark):
        sorted_exercise_log_after_date = sorted(bookmark.exercise_log,
                                                key=lambda x: x.time,
                                                reverse=True)
        if sorted_exercise_log_after_date:
            if sorted_exercise_log_after_date[0].outcome.outcome == outcome:
                return True
        return False
Exemplo n.º 29
0
class User(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    name = db.Column(db.String(255))
    password = db.Column(db.LargeBinary(255))
    password_salt = db.Column(db.LargeBinary(255))
    learned_language_id = db.Column(db.String(2), db.ForeignKey("language.id"))
    learned_language = sqlalchemy.orm.relationship(
        "Language", foreign_keys=[learned_language_id])
    starred_words = relationship("UserWord",
                                 secondary="starred_words_association")

    native_language_id = db.Column(db.String(2), db.ForeignKey("language.id"))
    native_language = sqlalchemy.orm.relationship(
        "Language", foreign_keys=[native_language_id])

    def __init__(self,
                 email,
                 username,
                 password,
                 learned_language=None,
                 native_language=None):
        self.email = email
        self.name = username
        self.update_password(password)
        self.learned_language = learned_language or Language.default_learned()
        self.native_language = native_language or Language.default_native_language(
        )

    def __repr__(self):
        return '<User %r>' % (self.email)

    def has_starred(self, word):
        return word in self.starred_words

    def star(self, word):
        self.starred_words.append(word)
        print word.word + " is now starred for user " + self.name
        # TODO: Does this work without a commit here? To double check.

    def set_learned_language(self, code):
        self.learned_language = Language.find(code)

    def set_native_language(self, code):
        self.native_language = Language.find(code)

    @classmethod
    def find(cls, email):
        return User.query.filter(User.email == email).one()

    @classmethod
    def find_by_id(cls, id):
        return User.query.filter(User.id == id).one()

    @sqlalchemy.orm.validates("email")
    def validate_email(self, col, email):
        if "@" not in email:
            raise ValueError("Invalid email address")
        return email

    @sqlalchemy.orm.validates("password")
    def validate_password(self, col, password):
        if password is None or len(password) == 0:
            raise ValueError("Invalid password")
        return password

    @sqlalchemy.orm.validates("name")
    def validate_name(self, col, name):
        if name is None or len(name) == 0:
            raise ValueError("Invalid username")
        return name

    def update_password(self, password):
        self.password_salt = "".join(
            chr(random.randint(0, 255)) for i in range(32))
        self.password = util.password_hash(password, self.password_salt)

    @classmethod
    def authorize(cls, email, password):
        try:
            user = cls.query.filter(cls.email == email).one()
            if user.password == util.password_hash(password,
                                                   user.password_salt):
                return user
        except sqlalchemy.orm.exc.NoResultFound:
            return None

    def bookmarks_chronologically(self):
        return Bookmark.query.filter_by(user_id=self.id).order_by(
            Bookmark.time.desc()).all()

    def user_words(self):
        return map((lambda x: x.origin.word), self.all_bookmarks())

    def all_bookmarks(self):
        return Bookmark.query.filter_by(user_id=self.id).order_by(
            Bookmark.time.desc()).all()

    def bookmark_count(self):
        return len(self.all_bookmarks())

    def word_count(self):
        return len(self.user_words())

    def bookmarks_by_date(self):
        def extract_day_from_date(bookmark):
            return (bookmark,
                    bookmark.time.replace(bookmark.time.year,
                                          bookmark.time.month,
                                          bookmark.time.day, 0, 0, 0, 0))

        bookmarks = self.all_bookmarks()
        bookmarks_by_date = dict()

        for elem in map(extract_day_from_date, bookmarks):
            bookmarks_by_date.setdefault(elem[1], []).append(elem[0])

        sorted_dates = bookmarks_by_date.keys()
        sorted_dates.sort(reverse=True)
        return bookmarks_by_date, sorted_dates

    def unique_urls(self):
        urls = set()
        for b in self.all_bookmarks():
            urls.add(b.text.url)
        return urls

    def recommended_urls(self):
        urls_to_words = {}
        for bookmark in self.all_bookmarks():
            if bookmark.text.url.url != "undefined":
                urls_to_words.setdefault(bookmark.text.url, 0)
                urls_to_words[
                    bookmark.text.url] += bookmark.origin.importance_level()
        return sorted(urls_to_words, key=urls_to_words.get, reverse=True)

    def get_known_bookmarks(self):
        bookmarks = Bookmark.find_all_filtered_by_user()
        i_know_bookmarks = []
        for bookmark in bookmarks:
            if Bookmark.is_sorted_exercise_log_after_date_outcome(
                    ExerciseOutcome.IKNOW, bookmark):
                i_know_bookmark_dict = {}
                i_know_bookmark_dict['id'] = bookmark.id
                i_know_bookmark_dict['origin'] = bookmark.origin.word
                i_know_bookmark_dict['text'] = bookmark.text.content
                i_know_bookmark_dict['time'] = bookmark.time.strftime(
                    '%m/%d/%Y')
                i_know_bookmarks.append(i_know_bookmark_dict.copy())
        return i_know_bookmarks

    def get_known_bookmarks_count(self):
        return len(self.get_known_bookmarks())

    def get_estimated_vocabulary(self, lang):
        bookmarks = Bookmark.find_all_filtered_by_user()
        filtered_words_known_from_user_dict_list = []
        marked_words_of_user_in_text = []
        words_of_all_bookmarks_content = []
        filtered_words_known_from_user = []
        for bookmark in bookmarks:
            bookmark_content_words = re.sub("[^\w]", " ",
                                            bookmark.text.content).split()
            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 WordRank.exists(word_known.lower(), lang):
                filtered_words_known_from_user.append(word_known)
            zeeguu.db.session.commit()

        filtered_words_known_from_user = list(
            set(filtered_words_known_from_user))
        for word in filtered_words_known_from_user:
            filtered_word_known_from_user_dict = {}
            filtered_word_known_from_user_dict['word'] = word
            filtered_words_known_from_user_dict_list.append(
                filtered_word_known_from_user_dict.copy())
        return filtered_words_known_from_user_dict_list

    def get_estimated_vocabulary_for_learned_language(self):
        return self.get_estimated_vocabulary(self.learned_language)

    def get_estimated_vocabulary_count(self):
        return len(self.get_estimated_vocabulary_for_learned_language())
Exemplo n.º 30
0
class KnownWordProbability(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_bin'}
    __tablename__ = 'known_word_probability'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
    user = db.relationship("User")
    user_word_id = db.Column(db.Integer,
                             db.ForeignKey('user_word.id'),
                             nullable=True)
    user_word = db.relationship("UserWord")
    ranked_word_id = db.Column(db.Integer,
                               db.ForeignKey("ranked_word.id"),
                               nullable=True)
    ranked_word = db.relationship("RankedWord")
    probability = db.Column(db.DECIMAL(10, 9), nullable=False)
    db.CheckConstraint('probability>=0', 'probability<=1')

    def __init__(self, user, user_word, ranked_word, probability):
        self.user = user
        self.user_word = user_word
        self.ranked_word = ranked_word
        self.probability = probability

    def word_has_just_beek_bookmarked(self):
        self.probability /= 2

    @classmethod
    def calculateKnownWordProb(cls, exerciseProb, encounterProb):
        return 0.8 * float(exerciseProb) + 0.2 * float(encounterProb)

    @classmethod
    def find(cls, user, user_word, ranked_word, probability=None):
        try:
            return cls.query.filter_by(user=user,
                                       user_word=user_word,
                                       ranked_word=ranked_word).one()
        except sqlalchemy.orm.exc.NoResultFound:
            return cls(user, user_word, ranked_word, probability)

    @classmethod
    def find_all_by_user(cls, user):
        return cls.query.filter_by(user=user).all()

    @classmethod
    def find_all_by_user_cached(cls, user):
        known_probabilities_cache = {}
        known_probabilities = cls.find_all_by_user(user)
        for known_probability in known_probabilities:
            user_word = known_probability.user_word
            # TODO: Why are there many KnownWordProbabilities with no user word in the database?
            if user_word is not None:
                known_probabilities_cache[
                    user_word.word] = known_probability.probability
        return known_probabilities_cache

    @classmethod
    def find_all_by_user_with_rank(cls, user):
        known_probs = cls.query.filter_by(user=user).all()
        for p in known_probs:
            if p.ranked_word is None:
                known_probs.remove(p)
        return known_probs

    @classmethod
    def exists(cls, user, user_word, ranked_word):
        try:
            cls.query.filter_by(user=user,
                                user_word=user_word,
                                ranked_word=ranked_word).one()
            return True
        except sqlalchemy.orm.exc.NoResultFound:
            return False

    @classmethod
    def get_probably_known_words(cls, user):
        return cls.query.filter(cls.user == user).filter(
            cls.probability >= 0.9).all()