ok
Browse files- train_valid_split.py +38 -19
train_valid_split.py
CHANGED
@@ -27,21 +27,39 @@ def filter_valid(questions):
|
|
27 |
return new_questions
|
28 |
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def format_to_valid(questions):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
answers_txt = [e["answer"] for e in questions]
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
answer_vectors = vectorizer.transform(answers_txt)
|
36 |
-
for i, question in enumerate(questions):
|
37 |
-
similarities = linear_kernel(answer_vectors[[i]], answer_vectors).flatten()
|
38 |
-
answer_scores = [(j, sim) for j, sim in enumerate(similarities) if sim != 1]
|
39 |
-
answer_scores = sorted(answer_scores, key=lambda x: x[1], reverse=True)
|
40 |
-
sorted_answers = [questions[j]["answer"] for j, _ in answer_scores if questions[j]["answer"] != question["answer"]]
|
41 |
-
negative_answer = sorted_answers[0]
|
42 |
-
assert question["answer"] not in sorted_answers
|
43 |
-
question["candidates"] = [question["answer"]] + sorted_answers
|
44 |
-
question["negative_example"] = negative_answer
|
45 |
return questions
|
46 |
|
47 |
|
@@ -57,18 +75,19 @@ def valid_train_split(filename, mapping=None):
|
|
57 |
line = json.loads(line_txt.strip())
|
58 |
domain = line["domain"]
|
59 |
if domain != previous_domain and previous_domain != "":
|
|
|
60 |
if len(mapping[previous_domain]) > 1:
|
61 |
-
train.extend(
|
62 |
elif len(valid) > 2000:
|
63 |
-
train.extend(
|
64 |
elif len(domain_data["pages"]) > 1:
|
65 |
-
train.extend(
|
66 |
elif len(domain_data["questions"]) < 15:
|
67 |
-
train.extend(
|
68 |
else:
|
69 |
questions = filter_valid(domain_data["questions"])
|
70 |
if len(questions) < 15:
|
71 |
-
train.extend(
|
72 |
else:
|
73 |
questions = format_to_valid(questions)
|
74 |
valid.extend(questions)
|
@@ -76,7 +95,7 @@ def valid_train_split(filename, mapping=None):
|
|
76 |
domain_data["questions"].append(line)
|
77 |
domain_data["pages"].add(line["domain_index"])
|
78 |
previous_domain = domain
|
79 |
-
train.extend(
|
80 |
return train, valid, filename
|
81 |
|
82 |
|
|
|
27 |
return new_questions
|
28 |
|
29 |
|
30 |
+
# def format_to_valid(questions):
|
31 |
+
# answers_txt = [e["answer"] for e in questions]
|
32 |
+
# questions_txt = [e["question"] for e in questions]
|
33 |
+
# vectorizer = TfidfVectorizer()
|
34 |
+
# vectorizer.fit(answers_txt + questions_txt)
|
35 |
+
# answer_vectors = vectorizer.transform(answers_txt)
|
36 |
+
# for i, question in enumerate(questions):
|
37 |
+
# similarities = linear_kernel(answer_vectors[[i]], answer_vectors).flatten()
|
38 |
+
# answer_scores = [(j, sim) for j, sim in enumerate(similarities) if sim != 1]
|
39 |
+
# answer_scores = sorted(answer_scores, key=lambda x: x[1], reverse=True)
|
40 |
+
# sorted_answers = [questions[j]["answer"] for j, _ in answer_scores if questions[j]["answer"] != question["answer"]]
|
41 |
+
# negative_answer = sorted_answers[len(sorted_answers) // 2]
|
42 |
+
# assert question["answer"] not in sorted_answers
|
43 |
+
# question["candidates"] = [question["answer"]] + sorted_answers
|
44 |
+
# question["negative_example"] = negative_answer
|
45 |
+
# return questions
|
46 |
+
|
47 |
+
|
48 |
def format_to_valid(questions):
|
49 |
+
answers = [e["answer"] for e in questions]
|
50 |
+
for question in questions:
|
51 |
+
answer = question["answer"]
|
52 |
+
candidates = [e for e in answers if e != answer]
|
53 |
+
candidates = [answer] + candidates
|
54 |
+
question["candidates"] = candidates
|
55 |
+
return questions
|
56 |
+
|
57 |
+
|
58 |
+
def format_to_train(questions):
|
59 |
answers_txt = [e["answer"] for e in questions]
|
60 |
+
answers_shifted = answers_txt[1:] + [answers_txt[0]]
|
61 |
+
for question, answer in zip(questions, answers_shifted):
|
62 |
+
question["negative"] = answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
return questions
|
64 |
|
65 |
|
|
|
75 |
line = json.loads(line_txt.strip())
|
76 |
domain = line["domain"]
|
77 |
if domain != previous_domain and previous_domain != "":
|
78 |
+
form_questions = format_to_train(domain_data["questions"])
|
79 |
if len(mapping[previous_domain]) > 1:
|
80 |
+
train.extend(form_questions)
|
81 |
elif len(valid) > 2000:
|
82 |
+
train.extend(form_questions)
|
83 |
elif len(domain_data["pages"]) > 1:
|
84 |
+
train.extend(form_questions)
|
85 |
elif len(domain_data["questions"]) < 15:
|
86 |
+
train.extend(form_questions)
|
87 |
else:
|
88 |
questions = filter_valid(domain_data["questions"])
|
89 |
if len(questions) < 15:
|
90 |
+
train.extend(form_questions)
|
91 |
else:
|
92 |
questions = format_to_valid(questions)
|
93 |
valid.extend(questions)
|
|
|
95 |
domain_data["questions"].append(line)
|
96 |
domain_data["pages"].add(line["domain_index"])
|
97 |
previous_domain = domain
|
98 |
+
# train.extend(form_questions)
|
99 |
return train, valid, filename
|
100 |
|
101 |
|