Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2021-235
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2021-235
2021-235
Commits
346dd83e
Commit
346dd83e
authored
Nov 26, 2021
by
HashaniJayasinghe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update document categorisation
parent
b1d2692b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
229 additions
and
0 deletions
+229
-0
app.py
app.py
+187
-0
categorize.py
categorize.py
+42
-0
No files found.
app.py
0 → 100644
View file @
346dd83e
from
flask
import
Flask
from
flask
import
request
from
flask_cors
import
CORS
import
os
import
question
as
qu
import
db
import
json
import
ast
from
operator
import
attrgetter
import
pickle
from
sklearn.feature_extraction.text
import
CountVectorizer
from
sklearn.feature_extraction.text
import
TfidfTransformer
app
=
Flask
(
"__main__"
)
CORS
(
app
)
current_path
=
os
.
path
.
abspath
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
)))
model
=
current_path
+
'
\\
finalized_model.pickle'
vec_count
=
current_path
+
'
\\
vec_count.pkl'
tfidf
=
current_path
+
'
\\
tfidf.pkl'
input_path
=
current_path
+
'
\
input.txt'
#function to generate questions
@
app
.
route
(
'/generateQuestions'
,
methods
=
[
'GET'
])
def
generateQuestions
():
text
=
request
.
args
.
get
(
'text'
)
print
(
text
)
qList
,
aList
,
shortAList
=
qu
.
createQuestions
(
text
)
values
=
{
"questions"
:
qList
,
"answers"
:
aList
,
"short"
:
shortAList
}
return
values
#function to insert questions
@
app
.
route
(
'/insertQuestions'
,
methods
=
[
'GET'
])
def
insertQuestions
():
questions
=
json
.
loads
(
request
.
args
.
get
(
'question'
))
answers
=
json
.
loads
(
request
.
args
.
get
(
'answers'
))
shortAnswers
=
json
.
loads
(
request
.
args
.
get
(
'short'
))
selectedQuestions
=
json
.
loads
(
request
.
args
.
get
(
'selected'
))
name
=
request
.
args
.
get
(
'name'
)
new_qs
=
[]
new_as
=
[]
new_shrt
=
[]
for
index
in
selectedQuestions
:
index
=
int
(
index
)
new_qs
.
append
(
questions
[
index
])
new_as
.
append
(
answers
[
index
])
new_shrt
.
append
(
shortAnswers
[
index
])
con
=
db
.
connection
()
cursor
=
con
.
cursor
()
sql
=
"INSERT INTO questions (qs_name, qs_list,as_list,as_shrt_list) VALUES (
%
s,
%
s,
%
s,
%
s)"
val
=
[
name
,
str
(
new_qs
),
str
(
new_as
),
str
(
new_shrt
)]
cursor
.
execute
(
sql
,
val
)
val
=
con
.
commit
()
return
str
(
1
)
#function to log
@
app
.
route
(
'/upload/login'
,
methods
=
[
'GET'
])
def
login
():
uname
=
request
.
args
.
get
(
'uname'
)
pwd
=
request
.
args
.
get
(
'pwd'
)
con
=
db
.
connection
()
cursor
=
con
.
cursor
()
selectData
=
"select * from users where uname='
%
s'"
%
uname
#retrieve data from mysql db
cursor
.
execute
(
selectData
)
result
=
cursor
.
fetchall
()
print
(
result
[
0
])
user
=
False
if
(
result
[
0
][
1
]
==
pwd
):
user
=
True
values
=
{
"user"
:
user
,
"type"
:
result
[
0
][
2
]
}
return
values
#function to load questions name list
@
app
.
route
(
'/loadQuestions'
,
methods
=
[
'GET'
])
def
loadQuestions
():
con
=
db
.
connection
()
cursor
=
con
.
cursor
()
selectData
=
"select qs_name from questions"
#retrieve data from mysql db
cursor
.
execute
(
selectData
)
result
=
cursor
.
fetchall
()
print
(
result
)
values
=
{
"name"
:
result
,
}
return
values
#function to load questions
@
app
.
route
(
'/getQuestions'
,
methods
=
[
'GET'
])
def
getQuestions
():
qname
=
request
.
args
.
get
(
'name'
)
con
=
db
.
connection
()
cursor
=
con
.
cursor
()
selectData
=
"select * from questions where qs_name='
%
s'"
%
qname
#retrieve data from mysql db
cursor
.
execute
(
selectData
)
result
=
cursor
.
fetchall
()
values
=
{
"questions"
:
ast
.
literal_eval
(
result
[
0
][
1
]),
"answers"
:
ast
.
literal_eval
(
result
[
0
][
2
]),
"short"
:
ast
.
literal_eval
(
result
[
0
][
3
]),
}
return
values
#function to load questions
@
app
.
route
(
'/insertResult'
,
methods
=
[
'GET'
])
def
insertResult
():
uname
=
request
.
args
.
get
(
'uname'
)
correct
=
request
.
args
.
get
(
'correct_an'
)
wrong
=
request
.
args
.
get
(
'wrong_an'
)
qname
=
request
.
args
.
get
(
'qname'
)
con
=
db
.
connection
()
cursor
=
con
.
cursor
()
sql
=
"INSERT INTO result (uname,correct,wrong,qname) VALUES (
%
s,
%
s,
%
s,
%
s)"
val
=
[
uname
,
correct
,
wrong
,
qname
]
cursor
.
execute
(
sql
,
val
)
val
=
con
.
commit
()
return
"1"
#function to load questions
@
app
.
route
(
'/loadResult'
,
methods
=
[
'GET'
])
def
loadResult
():
qname
=
request
.
args
.
get
(
'qname'
)
con
=
db
.
connection
()
cursor
=
con
.
cursor
()
selectData
=
"select * from result where qname='
%
s'"
%
qname
#retrieve data from mysql db
cursor
.
execute
(
selectData
)
result
=
cursor
.
fetchall
()
final
=
[]
for
r
in
result
:
marks
=
(
r
[
1
]
/
(
r
[
1
]
+
r
[
2
]))
*
100
name
=
(
r
[
0
]
.
split
(
'@'
))[
0
]
.
split
(
'.'
)
st_name
=
name
[
0
]
+
" "
+
name
[
1
]
+
" : "
final
.
append
((
st_name
,
marks
))
final
.
sort
(
key
=
lambda
y
:
y
[
1
],
reverse
=
True
)
values
=
{
"result"
:
final
[:
5
],
}
return
values
#function to predict cetogory
@
app
.
route
(
'/cetogoryPrediction'
,
methods
=
[
'GET'
])
def
cetogoryPrediction
():
content
=
request
.
args
.
get
(
'text'
)
new_doc
=
[
content
]
loaded_model
=
pickle
.
load
(
open
(
model
,
'rb'
))
loaded_vec
=
CountVectorizer
(
vocabulary
=
pickle
.
load
(
open
(
vec_count
,
"rb"
)))
loaded_tfidf
=
pickle
.
load
(
open
(
tfidf
,
"rb"
))
x_new_counts
=
loaded_vec
.
transform
(
new_doc
)
x_new_tfidf
=
loaded_tfidf
.
transform
(
x_new_counts
)
predict
=
loaded_model
.
predict
(
x_new_tfidf
)
values
=
{
"result"
:
predict
,
}
return
values
if
__name__
==
'__main__'
:
loadResult
()
\ No newline at end of file
categorize.py
0 → 100644
View file @
346dd83e
# from flask import Flask
# from flask import request
# from flask_cors import CORS
import
os
,
sys
from
operator
import
attrgetter
import
pickle
from
sklearn.feature_extraction.text
import
CountVectorizer
#from sklearn.feature_extraction.text import TfidfTransformer
import
shutil
current_path
=
os
.
path
.
abspath
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
)))
model
=
current_path
+
'
\\
finalized_model.pickle'
vec_count
=
current_path
+
'
\\
vec_count.pkl'
tfidf
=
current_path
+
'
\\
tfidf.pkl'
input_path
=
current_path
+
'
\
input.txt'
doc_path
=
current_path
+
'
\
Docs'
notice_path
=
current_path
+
'
\
Docs
\\
notices'
grade10_path
=
current_path
+
'
\
Docs
\\
grade10'
grade11_path
=
current_path
+
'
\
Docs
\\
grade11'
path
=
str
(
sys
.
argv
[
1
])
content
=
str
(
sys
.
argv
[
2
])
new_doc
=
[
content
]
loaded_model
=
pickle
.
load
(
open
(
model
,
'rb'
))
loaded_vec
=
CountVectorizer
(
vocabulary
=
pickle
.
load
(
open
(
vec_count
,
"rb"
)))
loaded_tfidf
=
pickle
.
load
(
open
(
tfidf
,
"rb"
))
x_new_counts
=
loaded_vec
.
transform
(
new_doc
)
x_new_tfidf
=
loaded_tfidf
.
transform
(
x_new_counts
)
predict
=
loaded_model
.
predict
(
x_new_tfidf
)
print
(
predict
[
0
])
if
(
predict
[
0
]
==
"Notice"
):
shutil
.
move
(
doc_path
+
"
\\
"
+
path
,
notice_path
+
"
\\
"
+
path
)
elif
(
predict
[
0
]
==
"grade10"
):
shutil
.
move
(
doc_path
+
"
\\
"
+
path
,
grade10_path
+
"
\\
"
+
path
)
elif
(
predict
[
0
]
==
"grade11"
):
shutil
.
move
(
doc_path
+
"
\\
"
+
path
,
grade11_path
+
"
\\
"
+
path
)
print
(
"success"
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment