from os import stat from otree.api import * from sklearn.feature_extraction.text import TfidfVectorizer doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'Verbal' PLAYERS_PER_GROUP = None NUM_ROUNDS = 10 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): dRT = models.FloatField() sAns = models.LongStringField() dSim = models.FloatField() dBaseSim = models.FloatField() iDSpace = models.IntegerField() sTask = models.StringField(initial='Verbal') # PAGES class Between(Page): template_name = 'global/Between.html' class Verbal(Page): template_name = 'global/Verbal.html' form_model = 'player' form_fields = ['sAns','dRT'] @staticmethod def vars_for_template(player: Player): return dict( text = """I'd like an apple, An apple a may keeps the doctor away, Never compare an apple to an oringe, I prefer scikit-learn to Orange, The scikit-learn docs are Orange and Blue""" ) @staticmethod def js_vars(player: Player): return dict( sResetText = 'This is the reset text' ) @staticmethod def before_next_page(player: Player, timeout_happened): trueText = """I'd like an apple, An apple a day keeps the doctor away, Never compare an apple to an orange, I prefer scikit-learn to Orange, The scikit-learn docs are Orange and Blue""" originText = """I'd like an apple, An apple a may keeps the doctor away, Never compare an apple to an oringe, I prefer scikit-learn to Orange, The scikit-learn docs are Orange and Blue""" ansText = player.sAns # Check similarity of answer with corrected text corpus = [trueText,ansText,originText] vect = TfidfVectorizer(min_df=1, stop_words="english") tfidf = vect.fit_transform(corpus) pairwise_similarity = (tfidf * tfidf.T).toarray() player.dSim = pairwise_similarity[1,0] player.dBaseSim = pairwise_similarity[2,0] # Check double spacing. iAnsSpace = len(ansText.split(' ')) iSpace0 = len(originText.split(' ')) player.iDSpace = iSpace0 - iAnsSpace #print(iAnsSpace, iSpace0) @staticmethod def is_displayed(player: Player): return player.sTask=='Verbal' page_sequence = [Between, Verbal]