forked from adwait-thattey/stackoverflow_api_recommender
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
202 lines (159 loc) · 5.05 KB
/
models.py
File metadata and controls
202 lines (159 loc) · 5.05 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import json
class Index:
def __init__(self):
self._vector = dict()
self._magnitude = 0
self._is_magnitude_fresh = True
def calc_magnitude(self):
sum = 0
for x in self._vector.values():
sum += x * x
sum = sum ** 0.5
self._magnitude = sum
self._is_magnitude_fresh = True
@property
def vector(self):
return self._vector
@vector.setter
def vector(self, values_dict):
if not isinstance(values_dict, dict):
raise AttributeError("values dict must be a dictionary")
self._vector = values_dict
self._is_magnitude_fresh = False
@vector.deleter
def vector(self):
self._vector.clear()
self._is_magnitude_fresh = False
def __len__(self):
return self._magnitude
@property
def magnitude(self):
if self._is_magnitude_fresh:
return self._magnitude
else:
raise ValueError("Magnitude is not fresh. Recalculate before calling")
@magnitude.setter
def magnitude(self, value):
raise TypeError("Magnitude can not be manually set. Call calc_magnitude method instead ")
def normalize(self):
if not self._is_magnitude_fresh:
self.calc_magnitude()
for key in self.vector:
self.vector[key] /= self.magnitude
self._magnitude = 1.0
self._is_magnitude_fresh = True
class CodeSnippet:
def __init__(self, lang, snippet):
self.language = lang
self.snippet = snippet
self.index = None
@property
def export_object(self):
obj = {
"language": self.language,
"snippet": self.snippet
}
return obj
def to_json(self):
json_string = json.dumps(self.export_object)
return json_string
@classmethod
def from_json(cls, json_string):
# parse json and return object of class
# return cls(...)
return None
class APIDoc:
def __init__(self, url, name, doc_text, code_snippets, out_links, lang):
self.url = url
self.name = name
self.text = doc_text
self.codes = [CodeSnippet(lang, cs) for cs in code_snippets]
self.out_links = out_links
self.index = None
@property
def export_object(self):
obj = {
"url": self.url,
"name": self.name,
"text": self.text,
"codes": [snip.export_object for snip in self.codes],
"out_links": json.dumps(self.out_links)
}
return obj
def to_json(self):
json_string = json.dumps(self.export_object)
return json_string
@classmethod
def from_json(cls, json_string):
# parse json and return object of class
# return cls(...)
return None
class Answer:
def __init__(self, url, text, code_snippets, out_links, upvotes, lang):
self.url = url
self.text = text
self.codes = [CodeSnippet(lang, cs) for cs in code_snippets]
self.out_links = out_links
self.upvotes = upvotes
self.index = None
@property
def export_object(self):
obj = {
"url": self.url,
"text": self.text,
"codes": [snip.export_object for snip in self.codes],
"out_links": self.out_links,
"upvotes": self.upvotes
}
return obj
def to_json(self):
json_string = json.dumps(self.export_object)
return json_string
@classmethod
def from_json(cls, json_string):
# parse json and return object of class
# return cls(...)
return None
class Question:
def __init__(self, url, question_id, title, text, code_snippets, out_links, answers, lang):
self.url = url
self.id = question_id
self.title = title
self.text = text
self.codes = [CodeSnippet(lang, cs) for cs in code_snippets]
self.out_links = out_links
self.answers = answers
self.index = None
@property
def export_object(self):
obj = {
"id": self.id,
"url": self.url,
"title": self.title,
"text": self.text,
"codes": [snip.export_object for snip in self.codes],
"out_links": self.out_links,
"answers": [ans.export_object for ans in self.answers]
}
return obj
def to_json(self):
json_string = json.dumps(self.export_object)
return json_string
@classmethod
def from_json(cls, json_string):
# parse json and return object of class
# return cls(...)
return None
class AnswerIndex:
def __init__(self, qid, index):
self.ques_id = qid
self.text = Index()
self.code = Index()
self.others = Index()
class QuestionIndex:
def __init__(self, ques):
self.id = ques.id
self.text_index = Index()
self.code_index = Index()
self.others_index = Index()
self.answers_index = [AnswerIndex(ques.id, ix) for ix in range(len(ques.answers))]