-
Notifications
You must be signed in to change notification settings - Fork 2
/
word.py
79 lines (68 loc) · 2.14 KB
/
word.py
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
import json
import nltk
from nltk import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()
stop = stopwords.words('english')
def generateWords(stu, final, users):
for i in range(0, len(stu)):
wordlist = {}
duplicated = []
memberid = stu[i]['memberId']
if memberid in users:
# duplicated users
continue
else:
users.append(memberid)
for v in stu[i]['vocabularyList']:
w = v['word']
postid = v['postId']
# remove video 3913
if postid == '3913':
continue
# remove white space of the word
w = w.strip()
if not w.isalpha():
continue
# stop words
if w in stop:
continue
# lemma
f1 = wordnet_lemmatizer.lemmatize(w)
w = wordnet_lemmatizer.lemmatize(f1, pos='v')
if w in duplicated:
# duplicated vocabulary
continue
else:
if wordlist.has_key(postid):
wordlist[postid].append(w)
else:
# wordlist['5797'] = ['book']
wordlist[postid] = [w]
duplicated.append(w)
user = {
'memberId' : memberid,
'wordList' : wordlist
}
final.append(user)
writefile = open('student_filteredWords.json', 'w')
final = []
# check duplicated users
users =[]
openfile = open('studentBehaviorInfo_1.json')
students = json.load(openfile)
generateWords(students, final, users)
openfile.close()
openfile = open('studentBehaviorInfo_2.json')
students = json.load(openfile)
generateWords(students, final, users)
openfile.close()
openfile = open('studentBehaviorInfoOver40Class_1213.json')
students = json.load(openfile)
generateWords(students, final, users)
openfile.close()
# print final
# print len(users)
json.dump(final, writefile)
writefile.close()