forked from pprakhar30/MOQA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
documents.py
95 lines (64 loc) · 2.34 KB
/
documents.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import gzip
import json
import nltk
import numpy as np
from collections import defaultdict
from nltk.stem import SnowballStemmer
from utils import check_sent, normalize
from nltk.tokenize import RegexpTokenizer
tokenizer = RegexpTokenizer(r'\w+')
stemmer = SnowballStemmer("english")
class QAdoc:
def __init__(self, itemId, questionType, answerType, question, answer, V, WordIDMap):
self.itemId = itemId
self.questionType = questionType
self.answerType = answerType
self.question = question
self.answer = answer
self.Question = [WordIDMap[stemmer.stem(word)] for word in tokenizer.tokenize(question) if stemmer.stem(word) in WordIDMap]
self.Answer = [WordIDMap[stemmer.stem(word)] for word in tokenizer.tokenize(answer) if stemmer.stem(word) in WordIDMap]
self.qFeature = {}
self.aFeature = {}
self.create_QAFeature()
def create_QAFeature(self):
for wordId in self.Question:
if wordId in self.qFeature:
self.qFeature[wordId] += 1
else:
self.qFeature[wordId] = np.float64(1)
for wordId in self.Answer:
if wordId in self.aFeature:
self.aFeature[wordId] += 1
else:
self.aFeature[wordId] = np.float64(1)
normalize(self.qFeature)
normalize(self.aFeature)
class ReviewDoc:
def __init__(self, itemId, reviewText, sentences, SPerItem, V, WordIDMap):
self.itemId = itemId
self.reviewText = reviewText
self.Review = []
self.create_ReviewFromSentence(sentences, SPerItem, V, WordIDMap)
def create_ReviewFromSentence(self, sentences, SPerItem, V, WordIDMap):
review_sentence = self.reviewText.split('.')
for sent in review_sentence:
if check_sent(sent, WordIDMap):
obj = Sentence(self.itemId, sent, V, WordIDMap, self)
sentences.append(obj)
self.Review.append(len(sentences)-1)
SPerItem[self.itemId].append(len(sentences)-1)
obj.create_SFeature()
class Sentence:
def __init__(self, itemId, Review, V, WordIDMap, ReviewObj):
self.itemId = itemId
self.sent = Review
self.rObj = ReviewObj
self.Sent = [WordIDMap[stemmer.stem(word)] for word in tokenizer.tokenize(Review) if stemmer.stem(word) in WordIDMap]
self.sFeature = {}
def create_SFeature(self):
for wordId in self.Sent:
if wordId in self.sFeature:
self.sFeature[wordId] += 1
else:
self.sFeature[wordId] = np.float64(1)
normalize(self.sFeature)