donfu
commited on
Commit
·
aae6ba6
1
Parent(s):
370ce50
Cleanup processing code
Browse files- process.py +0 -157
process.py
CHANGED
@@ -51,11 +51,6 @@ def process_dataset(dataset):
|
|
51 |
source = SOURCE.format(dataset)
|
52 |
if os.path.exists(xml_file):
|
53 |
df = xml_to_df(xml_file, source)
|
54 |
-
# df = filter_only_questions_with_accepted_answers(df)
|
55 |
-
# df = filter_scores_above(df, QUESTION_SCORE_TRESHOLD, ANSWER_SCORE_TRESHOLD)
|
56 |
-
# df = clean_tags(df)
|
57 |
-
# df = convert_html_to_markdown(df)
|
58 |
-
# df = group_qa(df)
|
59 |
oa = convert_to_oa(df)
|
60 |
save_parquet(oa, dataset)
|
61 |
# upload_hf(dataset)
|
@@ -148,43 +143,6 @@ def xml_to_df(path: str, source: str):
|
|
148 |
return df
|
149 |
|
150 |
|
151 |
-
def filter_only_questions_with_accepted_answers(df):
|
152 |
-
"""
|
153 |
-
Filter only to Questions with Accepted Answers
|
154 |
-
|
155 |
-
Filter dataframe by questions that have accepted answers, should also include
|
156 |
-
all rows of answers for those questions, even if not accepted.
|
157 |
-
|
158 |
-
Parameters:
|
159 |
-
df (DataFrame): containing a "AcceptedAnswerId", "Id", and "ParentId" columns
|
160 |
-
|
161 |
-
Returns:
|
162 |
-
df (DataFrame): current dataframe with filtered results
|
163 |
-
"""
|
164 |
-
accepted_ids = df[df["AcceptedAnswerId"] != 0]["Id"].tolist()
|
165 |
-
return df[(df["AcceptedAnswerId"] != 0) | (df["ParentId"].isin(accepted_ids))]
|
166 |
-
|
167 |
-
|
168 |
-
def filter_scores_above(
|
169 |
-
df, question_score_threshold: int = 20, answer_score_threshold: int = 20
|
170 |
-
):
|
171 |
-
"""
|
172 |
-
Filter Dataframe by minimum scores
|
173 |
-
|
174 |
-
Filter Question and Answer columns by score thresholds to trim lower scoring results
|
175 |
-
|
176 |
-
Parameters:
|
177 |
-
df (DataFrame): containing a "Score" column
|
178 |
-
|
179 |
-
Returns:
|
180 |
-
df (DataFrame): current dataframe with filtered results
|
181 |
-
"""
|
182 |
-
return df[
|
183 |
-
((df["Score"] >= question_score_threshold) & (df.PostTypeId == 1))
|
184 |
-
| ((df["Score"] >= answer_score_threshold) & (df.PostTypeId == 2))
|
185 |
-
]
|
186 |
-
|
187 |
-
|
188 |
remove_markdown_links_pattern = r"\[([^\]]+)\]\(([^\)]+)\)"
|
189 |
remove_remaining_links = r"https?:\/\/[^\s]+"
|
190 |
|
@@ -200,120 +158,5 @@ def to_markdown(text):
|
|
200 |
return text
|
201 |
|
202 |
|
203 |
-
def convert_html_to_markdown(df, column: str = "Body"):
|
204 |
-
"""
|
205 |
-
Convert HTML tags to markdown
|
206 |
-
|
207 |
-
Feeds HTML text body into markdown. Remove final newline from <p> tags
|
208 |
-
|
209 |
-
Parameters:
|
210 |
-
df (DataFrame): containing a "Body" column with HTML
|
211 |
-
|
212 |
-
Returns:
|
213 |
-
df (DataFrame): current dataframe with parsed column
|
214 |
-
"""
|
215 |
-
df.dropna(subset=[column], inplace=True)
|
216 |
-
df[f"{column}Clean"] = df[column].apply(to_markdown)
|
217 |
-
return df
|
218 |
-
|
219 |
-
|
220 |
-
def clean_tags(df):
|
221 |
-
"""
|
222 |
-
Convert Tags into Comma separated
|
223 |
-
|
224 |
-
Converts Tag slugs into commas separated tags
|
225 |
-
|
226 |
-
Parameters:
|
227 |
-
df (DataFrame): containing a "Tags" column with slugs
|
228 |
-
|
229 |
-
Returns:
|
230 |
-
df (DataFrame): current dataframe with parsed column
|
231 |
-
"""
|
232 |
-
df["TagsClean"] = (
|
233 |
-
df["Tags"]
|
234 |
-
.str.replace("-", " ")
|
235 |
-
.str.replace("><", ", ")
|
236 |
-
.str.replace("<", "")
|
237 |
-
.str.replace(">", "")
|
238 |
-
)
|
239 |
-
return df
|
240 |
-
|
241 |
-
|
242 |
-
def group_qa(df):
|
243 |
-
"""
|
244 |
-
Group Questions and Answers
|
245 |
-
"""
|
246 |
-
questions = df[df.PostTypeId == 1]
|
247 |
-
answers = df[df.PostTypeId == 2]
|
248 |
-
|
249 |
-
df = pd.merge(
|
250 |
-
questions,
|
251 |
-
answers[
|
252 |
-
[
|
253 |
-
"Id",
|
254 |
-
"CreationDate",
|
255 |
-
"Score",
|
256 |
-
"ViewCount",
|
257 |
-
"CommentCount",
|
258 |
-
"ContentLicense",
|
259 |
-
"TagsClean",
|
260 |
-
"BodyClean",
|
261 |
-
"ParentId",
|
262 |
-
]
|
263 |
-
],
|
264 |
-
left_on="Id",
|
265 |
-
right_on="ParentId",
|
266 |
-
suffixes=("_q", "_a"),
|
267 |
-
how="left",
|
268 |
-
)
|
269 |
-
|
270 |
-
df["AcceptedAnswerFlag"] = df.apply(
|
271 |
-
lambda row: row["Id_a"] == row["AcceptedAnswerId"], axis=1
|
272 |
-
)
|
273 |
-
|
274 |
-
df = df.rename(
|
275 |
-
columns={
|
276 |
-
"BodyClean_q": "Question",
|
277 |
-
"Score_q": "QuestionScore",
|
278 |
-
"TagsClean_q": "QuestionTags",
|
279 |
-
"BodyClean_a": "Answer",
|
280 |
-
"Score_a": "AnswerScore",
|
281 |
-
"ContentLicense_q": "QuestionContentLicense",
|
282 |
-
"ContentLicense_a": "AnswerContentLicense",
|
283 |
-
"CreationDate_q": "CreationDate",
|
284 |
-
}
|
285 |
-
)
|
286 |
-
|
287 |
-
df = (
|
288 |
-
df.sort_values(
|
289 |
-
by=["AcceptedAnswerFlag", "AnswerScore"], ascending=[False, False]
|
290 |
-
)
|
291 |
-
.groupby("Question")
|
292 |
-
.head(MAX_ANSWERS)
|
293 |
-
.reset_index(drop=True)
|
294 |
-
)
|
295 |
-
df = (
|
296 |
-
df.groupby(
|
297 |
-
[
|
298 |
-
"Title",
|
299 |
-
"Question",
|
300 |
-
"QuestionScore",
|
301 |
-
"QuestionTags",
|
302 |
-
"QuestionContentLicense",
|
303 |
-
"DataSource",
|
304 |
-
"CreationDate",
|
305 |
-
]
|
306 |
-
)
|
307 |
-
.apply(
|
308 |
-
lambda x: x[["Answer", "AnswerScore", "AcceptedAnswerFlag"]].to_dict(
|
309 |
-
"records"
|
310 |
-
)
|
311 |
-
)
|
312 |
-
.reset_index()
|
313 |
-
.rename(columns={0: "Answers"})
|
314 |
-
)
|
315 |
-
return df
|
316 |
-
|
317 |
-
|
318 |
if __name__ == "__main__":
|
319 |
main()
|
|
|
51 |
source = SOURCE.format(dataset)
|
52 |
if os.path.exists(xml_file):
|
53 |
df = xml_to_df(xml_file, source)
|
|
|
|
|
|
|
|
|
|
|
54 |
oa = convert_to_oa(df)
|
55 |
save_parquet(oa, dataset)
|
56 |
# upload_hf(dataset)
|
|
|
143 |
return df
|
144 |
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
remove_markdown_links_pattern = r"\[([^\]]+)\]\(([^\)]+)\)"
|
147 |
remove_remaining_links = r"https?:\/\/[^\s]+"
|
148 |
|
|
|
158 |
return text
|
159 |
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
if __name__ == "__main__":
|
162 |
main()
|