-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
116 lines (101 loc) · 3.89 KB
/
Copy pathmain.py
File metadata and controls
116 lines (101 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Automated Essay Scorer - Using data from Hewlett Foundation (Kaggle)
# import regular libraries
import pandas as pd
import numpy as np
from statistics import mean
import matplotlib.pyplot as plt
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import stopwords
import pickle
import nltk
from flask import request
from flask import jsonify
from flask import Flask, render_template
# Import other functions
from essaygrader.process.averageWordLength import averageWordLength
from essaygrader.process.misspellings import nmisspelled
from essaygrader.process.wordcount import length
from essaygrader.process.averageSentenceLength import averageSentenceLength
from essaygrader.process.grammarchecker import grammarCheck
from essaygrader.process.keywords import keyWords
from essaygrader.process.sentcount import sentcount
from essaygrader.process.vocabulary import VocabCounter
from essaygrader.process.stopwords import stopWords
from eg import essaygrader
app = Flask(__name__)
# For imagery score
def evaluate(essay):
emote = pd.read_excel('essaygrader/data/pastData/emote.xlsx')
points = 0
for word in essay:
word = word.lower()
if word.lower() in emote['word'].unique():
bnoun = emote[emote['word'] == word]['noun'].values[0]
if (bnoun):
points += mean(emote[emote['word'] == word][['ndim' + str(i) for i in range(1, 8)]].values[0])
else:
points += mean(emote[emote['word'] == word][['adim' + str(i) for i in range(1, 11)]].values[0])
return points
# For vocabulary
def vocabulary(li):
vocab = VocabCounter()
return round(vocab.CountVocab(li)*100, 2)
# Clean essay
def cleanEssay(essay):
essays = [essay]
tokenizer = RegexpTokenizer(r'\w+')
cleanedessay = tokenizer.tokenize(essay)
essays.append(cleanedessay)
stop_words = set(stopwords.words('english'))
cleanedessay_nosw = [w for w in cleanedessay if not w.lower() in stop_words]
essays.append(cleanedessay_nosw)
return essays
# Make features for essay
def makeFeatures(essays, prompt):
essay = essays[0]
cleanedessay = essays[1]
cleanedessay_nosw = essays[2]
features = []
features.append(averageWordLength(cleanedessay))
features.append(nmisspelled(cleanedessay))
features.append(length(cleanedessay))
features.append(keyWords(prompt, cleanedessay_nosw))
features.append(sentcount(essay))
features.append(evaluate(cleanedessay))
features.append(stopWords(cleanedessay))
features.append(vocabulary(cleanedessay))
row = pd.DataFrame([features], columns=['Average Word Length', 'percent_misspelled', 'word_count', 'percent_key_words', 'sentcount', 'score', 'percent_stop_words', 'vocabulary'])
return row
# Render templates
@app.route('/')
def my_form():
return render_template('index.html')
@app.route('/input')
def input():
return render_template('input.html')
@app.route('/score')
def score():
return render_template('score.html')
@app.route('/input', methods=['POST'])
def my_form_post():
# Take from user input
grade = request.form.get('grade', None)
topic = request.form.get('type', None)
essay = request.form['text']
prompt = request.form['text2']
if (grade is not None) and (topic is not None) and (essay != "") and (prompt != ""):
# Clean essay and make features
essays = cleanEssay(essay)
row = makeFeatures(essays, prompt)
# Grade essay
grader = essaygrader(grade, topic, row)
answer = grader.gradeEssay()
prediction = answer[0:25]
feedback = answer[25:]
# Output prediction in score.html page
return render_template('score.html', grade=prediction, explain=feedback)
else:
# If any input field is empty, show message
return render_template('input.html', error="Please fill in all required fields.")
if __name__ == "__main__":
app.run(port='8088',threaded=False)