Spaces:
Running
Running
linhnguyen02
commited on
Commit
·
7a13735
1
Parent(s):
ee409a4
update tu dong nghia va trai nghia
Browse files
env.py
CHANGED
|
@@ -24,5 +24,9 @@ config = {
|
|
| 24 |
},
|
| 25 |
"google": {
|
| 26 |
"api_key": os.getenv("GOOGLE_API_KEY"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
}
|
|
|
|
| 24 |
},
|
| 25 |
"google": {
|
| 26 |
"api_key": os.getenv("GOOGLE_API_KEY"),
|
| 27 |
+
},
|
| 28 |
+
"elastic": {
|
| 29 |
+
"url": os.getenv("ELASTIC_URL"),
|
| 30 |
+
"api_key": os.getenv("ELASTIC_API_KEY")
|
| 31 |
}
|
| 32 |
}
|
src/factories/gen_question/types/antonym_question.py
CHANGED
|
@@ -4,110 +4,135 @@ import random
|
|
| 4 |
from src.factories.gen_question.types.base import Question, nltk_words
|
| 5 |
from src.enums import QuestionTypeEnum
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
class AntonymsQuestion(Question):
|
| 9 |
-
"""
|
| 10 |
-
This class generates multiple-choice questions that ask the user
|
| 11 |
-
to select an antonym for a given word.
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
it falls back to randomly chosen words from a built-in word list (nltk_words).
|
| 16 |
-
"""
|
| 17 |
|
| 18 |
-
def generate_questions(self, list_words: List[str] = None, num_question: int = 1,
|
| 19 |
-
|
| 20 |
-
list_words = []
|
| 21 |
|
| 22 |
-
|
| 23 |
list_unique_words = set(list_words)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
Randomly selects a word and finds one of its antonyms.
|
| 29 |
-
|
| 30 |
-
Returns:
|
| 31 |
-
tuple(str, str): question_word, antonym_answer
|
| 32 |
-
"""
|
| 33 |
-
# Try from provided list word
|
| 34 |
-
while list_unique_words:
|
| 35 |
-
source_word = random.sample(list(list_unique_words), 1)[0]
|
| 36 |
-
list_unique_words.remove(source_word)
|
| 37 |
-
antonym_word = self.get_antonym(source_word)
|
| 38 |
-
if antonym_word in list_unique_words:
|
| 39 |
-
list_unique_words.remove(antonym_word)
|
| 40 |
-
if antonym_word:
|
| 41 |
-
return source_word, antonym_word
|
| 42 |
-
|
| 43 |
-
# Fallback: use nltk_words
|
| 44 |
-
while True:
|
| 45 |
-
source_word = random.choice(nltk_words)
|
| 46 |
-
antonym_word = self.get_antonym(source_word)
|
| 47 |
-
if antonym_word:
|
| 48 |
-
return source_word, antonym_word
|
| 49 |
|
| 50 |
for _ in range(num_question):
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
choices = [correct_answer]
|
| 54 |
-
distractor_set = set()
|
| 55 |
|
| 56 |
-
while len(choices) < num_ans_per_question:
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
random.shuffle(choices)
|
| 66 |
|
| 67 |
result.append({
|
| 68 |
"question": question_word,
|
| 69 |
-
"type": QuestionTypeEnum.
|
| 70 |
"choices": choices,
|
| 71 |
"answer": choices.index(correct_answer),
|
| 72 |
-
"explain": []
|
| 73 |
})
|
| 74 |
|
| 75 |
return result
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
"""
|
| 79 |
-
Retrieves a random antonym for the given word using dictionary API data.
|
| 80 |
-
|
| 81 |
-
It checks both the 'meanings.antonyms' and 'meanings.definitions.antonyms' fields.
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
"""
|
| 89 |
-
data = self.fetch_word_data(word)
|
| 90 |
-
if not data:
|
| 91 |
-
return None
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
meaning = random.sample(meanings, 1)[0]
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
if not antonyms:
|
| 104 |
-
definitions = meaning.get("definitions", [])
|
| 105 |
-
for definition in definitions:
|
| 106 |
-
antonyms.extend(definition.get("antonyms", []))
|
| 107 |
|
| 108 |
-
if
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
|
|
|
| 4 |
from src.factories.gen_question.types.base import Question, nltk_words
|
| 5 |
from src.enums import QuestionTypeEnum
|
| 6 |
|
| 7 |
+
from src.loaders.elastic import Elastic
|
| 8 |
+
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
class AntonymsQuestion(Question):
|
| 12 |
+
INDEX = "vocabulary"
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def generate_questions(self, list_words: List[str] = None, num_question: int = 1,
|
| 15 |
+
num_ans_per_question: int = 4, cefr: int = 3):
|
|
|
|
| 16 |
|
| 17 |
+
list_words = list_words or []
|
| 18 |
list_unique_words = set(list_words)
|
| 19 |
|
| 20 |
+
result = []
|
| 21 |
+
used_words = set()
|
| 22 |
+
used_choices = set()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
for _ in range(num_question):
|
| 25 |
+
max_loop = 100
|
| 26 |
+
question_word, correct_answer, antonym_set = \
|
| 27 |
+
self._pick_question_word(list_unique_words, used_words, cefr)
|
| 28 |
+
|
| 29 |
+
pos = self.get_pos({
|
| 30 |
+
"bool": {
|
| 31 |
+
"must": [
|
| 32 |
+
{"term": {"word.keyword": question_word.lower()}},
|
| 33 |
+
{"term": {"antonyms.keyword": correct_answer.lower()}}
|
| 34 |
+
]
|
| 35 |
+
}
|
| 36 |
+
})
|
| 37 |
+
used_words.update([question_word, correct_answer])
|
| 38 |
|
| 39 |
choices = [correct_answer]
|
|
|
|
| 40 |
|
| 41 |
+
while len(choices) < num_ans_per_question and max_loop > 0:
|
| 42 |
+
doc = self.get_random(self.INDEX, None, cefr=cefr, pos=pos)
|
| 43 |
+
if not doc:
|
| 44 |
+
continue
|
| 45 |
+
|
| 46 |
+
candidate = doc["word"]
|
| 47 |
|
| 48 |
+
# Loại trừ điều kiện chung
|
| 49 |
+
if (
|
| 50 |
+
candidate in used_choices or
|
| 51 |
+
candidate in used_words or
|
| 52 |
+
candidate in antonym_set or
|
| 53 |
+
candidate == question_word or
|
| 54 |
+
candidate == correct_answer
|
| 55 |
+
):
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
+
# Loại distractor có nghĩa trùng với đáp án
|
| 59 |
+
syns = set(self.get_list_antonym(candidate))
|
| 60 |
+
if correct_answer in syns:
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
choices.append(candidate)
|
| 64 |
+
used_choices.add(candidate)
|
| 65 |
+
max_loop -= 1
|
| 66 |
|
| 67 |
random.shuffle(choices)
|
| 68 |
|
| 69 |
result.append({
|
| 70 |
"question": question_word,
|
| 71 |
+
"type": QuestionTypeEnum.SYNONYM,
|
| 72 |
"choices": choices,
|
| 73 |
"answer": choices.index(correct_answer),
|
| 74 |
+
"explain": []
|
| 75 |
})
|
| 76 |
|
| 77 |
return result
|
| 78 |
+
|
| 79 |
+
# -----------------------------------------------------
|
| 80 |
+
# Lấy tất cả antonym của 1 từ từ ES (nhiều nghĩa)
|
| 81 |
+
# -----------------------------------------------------
|
| 82 |
+
def get_list_antonym(self, word: str):
|
| 83 |
+
es = Elastic()
|
| 84 |
+
query = {"term": {"word.keyword": word.lower()}}
|
| 85 |
+
resp = es.search(index=self.INDEX, query=query, size=1000)
|
| 86 |
+
|
| 87 |
+
hits = resp["hits"]["hits"]
|
| 88 |
+
if not hits:
|
| 89 |
+
return []
|
| 90 |
+
|
| 91 |
+
antonyms = set()
|
| 92 |
+
for h in hits:
|
| 93 |
+
s = h["_source"].get("antonyms", [])
|
| 94 |
+
antonyms.update(s)
|
| 95 |
+
|
| 96 |
+
return list(antonyms)
|
| 97 |
+
|
| 98 |
+
# -----------------------------------------------------
|
| 99 |
+
# Lấy 1 từ làm câu hỏi và 1 antonym làm đáp án
|
| 100 |
+
# -----------------------------------------------------
|
| 101 |
+
def _pick_question_word(self, list_unique_words, used_words, cefr):
|
| 102 |
+
"""
|
| 103 |
+
- Ưu tiên lấy từ danh sách đầu vào
|
| 104 |
+
- Nếu hết → lấy từ ES random theo CEFR
|
| 105 |
"""
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
# ƯU TIÊN INPUT LIST
|
| 108 |
+
while list_unique_words:
|
| 109 |
+
source = list_unique_words.pop()
|
| 110 |
|
| 111 |
+
if source in used_words:
|
| 112 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
syns = self.get_list_antonym(source)
|
| 115 |
+
valid_syns = [s for s in syns if s not in used_words]
|
| 116 |
+
if not valid_syns:
|
| 117 |
+
continue
|
| 118 |
|
| 119 |
+
correct = random.choice(valid_syns)
|
| 120 |
+
return source, correct, set(syns)
|
|
|
|
| 121 |
|
| 122 |
+
# FALLBACK ES
|
| 123 |
+
while True:
|
| 124 |
+
doc = self.get_random(self.INDEX, None, cefr=cefr)
|
| 125 |
+
if not doc:
|
| 126 |
+
continue
|
| 127 |
|
| 128 |
+
source = doc["word"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
if source in used_words:
|
| 131 |
+
continue
|
| 132 |
|
| 133 |
+
syns = self.get_list_antonym(source)
|
| 134 |
+
valid_syns = [s for s in syns if s not in used_words]
|
| 135 |
+
if not valid_syns:
|
| 136 |
+
continue
|
| 137 |
|
| 138 |
+
return source, random.choice(valid_syns), set(syns)
|
src/factories/gen_question/types/base.py
CHANGED
|
@@ -1,13 +1,10 @@
|
|
| 1 |
from abc import ABC, abstractmethod
|
| 2 |
from typing import Set, Optional
|
| 3 |
|
| 4 |
-
import
|
| 5 |
-
import nltk
|
| 6 |
|
| 7 |
-
|
| 8 |
-
from nltk.corpus import words
|
| 9 |
|
| 10 |
-
nltk_words = words.words()
|
| 11 |
|
| 12 |
|
| 13 |
class Question(ABC):
|
|
@@ -15,6 +12,86 @@ class Question(ABC):
|
|
| 15 |
def generate_questions(self, list_words: Set[str], num_questions: int = 1, num_ans_per_question: int = 4):
|
| 16 |
pass
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@staticmethod
|
| 19 |
def cal_num_word_in_list_available_per_question(
|
| 20 |
len_list_words: int,
|
|
@@ -22,17 +99,4 @@ class Question(ABC):
|
|
| 22 |
num_ans_per_question: int = 4
|
| 23 |
) -> int:
|
| 24 |
return min(len_list_words//num_questions, num_ans_per_question)
|
| 25 |
-
|
| 26 |
-
@staticmethod
|
| 27 |
-
def fetch_word_data(word: str) -> Optional[dict]:
|
| 28 |
-
"""API get data of word"""
|
| 29 |
-
try:
|
| 30 |
-
base_url = "https://api.dictionaryapi.dev/api/v2/entries/en/"
|
| 31 |
-
resp = requests.get(base_url + word)
|
| 32 |
-
if resp.status_code == 200:
|
| 33 |
-
data = resp.json()
|
| 34 |
-
return data[0]
|
| 35 |
-
else:
|
| 36 |
-
return None
|
| 37 |
-
except Exception as e:
|
| 38 |
-
return None
|
|
|
|
| 1 |
from abc import ABC, abstractmethod
|
| 2 |
from typing import Set, Optional
|
| 3 |
|
| 4 |
+
import random
|
|
|
|
| 5 |
|
| 6 |
+
from src.loaders.elastic import Elastic
|
|
|
|
| 7 |
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
class Question(ABC):
|
|
|
|
| 12 |
def generate_questions(self, list_words: Set[str], num_questions: int = 1, num_ans_per_question: int = 4):
|
| 13 |
pass
|
| 14 |
|
| 15 |
+
def _build_query(self, query: dict = None, cefr: Optional[int] = None, pos: str = None):
|
| 16 |
+
must = []
|
| 17 |
+
if query:
|
| 18 |
+
must.append(query)
|
| 19 |
+
if cefr is not None:
|
| 20 |
+
must.append({
|
| 21 |
+
"range": {
|
| 22 |
+
"cefr": {"gte": cefr - 1, "lte": cefr + 1}
|
| 23 |
+
}
|
| 24 |
+
})
|
| 25 |
+
if pos is not None:
|
| 26 |
+
must.append({
|
| 27 |
+
"term": {"pos.keyword": pos}
|
| 28 |
+
})
|
| 29 |
+
return {"bool": {"must": must}} if must else {"match_all": {}}
|
| 30 |
+
|
| 31 |
+
# ---------------------------------------
|
| 32 |
+
# Get 1 random doc from ES using CEFR + filter
|
| 33 |
+
# ---------------------------------------
|
| 34 |
+
def get_random(self, index: str, query: dict = None, cefr: int = None, pos: str = None):
|
| 35 |
+
es = Elastic()
|
| 36 |
+
q = self._build_query(query, cefr, pos)
|
| 37 |
+
|
| 38 |
+
count = es.count(index=index, query=q)["count"]
|
| 39 |
+
if count == 0:
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
+
offset = random.randint(0, count - 1)
|
| 43 |
+
|
| 44 |
+
resp = es.search(index=index, query=q, size=1, from_=offset)
|
| 45 |
+
hits = resp["hits"]["hits"]
|
| 46 |
+
return hits[0]["_source"] if hits else None
|
| 47 |
+
|
| 48 |
+
# ---------------------------------------
|
| 49 |
+
# Get first matched doc
|
| 50 |
+
# ---------------------------------------
|
| 51 |
+
def get_detail_word(self, index: str, query: dict = None):
|
| 52 |
+
es = Elastic()
|
| 53 |
+
q = self._build_query(query)
|
| 54 |
+
resp = es.search(index=index, query=q, size=1)
|
| 55 |
+
hits = resp["hits"]["hits"]
|
| 56 |
+
return hits[0]["_source"] if hits else None
|
| 57 |
+
|
| 58 |
+
def get_cefr_word(self, index: str, word: str):
|
| 59 |
+
doc = self.get_detail_word(index, {"term": {"word.keyword": word}})
|
| 60 |
+
return doc.get("cefr") if doc else None
|
| 61 |
+
|
| 62 |
+
# ---------------------------------------
|
| 63 |
+
def check_valid_cefr(self, target_cefr: int, candidate_cefr: Optional[int]):
|
| 64 |
+
if candidate_cefr is None:
|
| 65 |
+
return False
|
| 66 |
+
return abs(target_cefr - candidate_cefr) <= 1
|
| 67 |
+
|
| 68 |
+
# ---------------------------------------
|
| 69 |
+
# Get pos of word
|
| 70 |
+
# ---------------------------------------
|
| 71 |
+
def get_pos(self, index: str, query: dict = None):
|
| 72 |
+
list_docs = self.get_list_word(index, query)
|
| 73 |
+
|
| 74 |
+
if len(list_docs) > 0:
|
| 75 |
+
doc = random.choice(list_docs)
|
| 76 |
+
if doc and "pos" in doc:
|
| 77 |
+
return doc["pos"]
|
| 78 |
+
|
| 79 |
+
return "noun"
|
| 80 |
+
|
| 81 |
+
def get_list_word(self, index: str, query: dict = None):
|
| 82 |
+
es = Elastic()
|
| 83 |
+
|
| 84 |
+
resp = es.search(
|
| 85 |
+
index=index,
|
| 86 |
+
query=query,
|
| 87 |
+
size=1000
|
| 88 |
+
)
|
| 89 |
+
hits = resp["hits"]["hits"]
|
| 90 |
+
|
| 91 |
+
return [hit["_source"] for hit in hits] if hits else []
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
@staticmethod
|
| 96 |
def cal_num_word_in_list_available_per_question(
|
| 97 |
len_list_words: int,
|
|
|
|
| 99 |
num_ans_per_question: int = 4
|
| 100 |
) -> int:
|
| 101 |
return min(len_list_words//num_questions, num_ans_per_question)
|
| 102 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/factories/gen_question/types/synonym_question.py
CHANGED
|
@@ -3,64 +3,64 @@ import random
|
|
| 3 |
|
| 4 |
from src.factories.gen_question.types.base import Question, nltk_words
|
| 5 |
from src.enums import QuestionTypeEnum
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
class SynonymsQuestion(Question):
|
| 9 |
-
""
|
| 10 |
-
This class generates multiple-choice questions that ask the user
|
| 11 |
-
to select a synonym for a given word.
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
it falls back to randomly chosen words from a built-in word list (nltk_words).
|
| 16 |
-
"""
|
| 17 |
|
| 18 |
-
|
| 19 |
-
if list_words is None:
|
| 20 |
-
list_words = []
|
| 21 |
-
|
| 22 |
-
result = []
|
| 23 |
list_unique_words = set(list_words)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
Randomly selects a word and finds one of its synonyms.
|
| 29 |
-
|
| 30 |
-
Returns:
|
| 31 |
-
tuple(str, str): question_word, synonym_answer
|
| 32 |
-
"""
|
| 33 |
-
# Try from provided list word
|
| 34 |
-
while list_unique_words:
|
| 35 |
-
source_word = random.sample(list(list_unique_words), 1)[0]
|
| 36 |
-
list_unique_words.remove(source_word)
|
| 37 |
-
synonym_word = self.get_synonym(source_word)
|
| 38 |
-
if synonym_word in list_unique_words:
|
| 39 |
-
list_unique_words.remove(source_word)
|
| 40 |
-
if synonym_word:
|
| 41 |
-
return source_word, synonym_word
|
| 42 |
-
|
| 43 |
-
# Fallback: use nltk_words
|
| 44 |
-
while True:
|
| 45 |
-
source_word = random.choice(nltk_words)
|
| 46 |
-
synonym_word = self.get_synonym(source_word)
|
| 47 |
-
if synonym_word:
|
| 48 |
-
return source_word, synonym_word
|
| 49 |
|
| 50 |
for _ in range(num_question):
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
choices = [correct_answer]
|
| 54 |
-
distractor_set = set()
|
| 55 |
|
| 56 |
-
while len(choices) < num_ans_per_question:
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
random.shuffle(choices)
|
| 66 |
|
|
@@ -69,45 +69,68 @@ class SynonymsQuestion(Question):
|
|
| 69 |
"type": QuestionTypeEnum.SYNONYM,
|
| 70 |
"choices": choices,
|
| 71 |
"answer": choices.index(correct_answer),
|
| 72 |
-
"explain": []
|
| 73 |
})
|
| 74 |
|
| 75 |
return result
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
"""
|
| 79 |
-
Retrieves a random synonym for the given word using dictionary API data.
|
| 80 |
-
|
| 81 |
-
It checks both the 'meanings.synonyms' and 'meanings.definitions.synonyms' fields.
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
"""
|
| 89 |
-
data = self.fetch_word_data(word)
|
| 90 |
-
if not data:
|
| 91 |
-
return None
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
meaning = random.sample(meanings, 1)[0]
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
if not synonyms:
|
| 104 |
-
definitions = meaning.get("definitions", [])
|
| 105 |
-
for definition in definitions:
|
| 106 |
-
synonyms.extend(definition.get("synonyms", []))
|
| 107 |
|
| 108 |
-
if
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
|
|
|
| 3 |
|
| 4 |
from src.factories.gen_question.types.base import Question, nltk_words
|
| 5 |
from src.enums import QuestionTypeEnum
|
| 6 |
+
from src.loaders.elastic import Elastic
|
| 7 |
|
| 8 |
|
| 9 |
class SynonymsQuestion(Question):
|
| 10 |
+
INDEX = "vocabulary"
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def generate_questions(self, list_words: List[str] = None, num_question: int = 1,
|
| 13 |
+
num_ans_per_question: int = 4, cefr: int = 3):
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
list_words = list_words or []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
list_unique_words = set(list_words)
|
| 17 |
|
| 18 |
+
result = []
|
| 19 |
+
used_words = set()
|
| 20 |
+
used_choices = set()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
for _ in range(num_question):
|
| 23 |
+
max_loop = 100
|
| 24 |
+
question_word, correct_answer, synonym_set = \
|
| 25 |
+
self._pick_question_word(list_unique_words, used_words, cefr)
|
| 26 |
+
|
| 27 |
+
pos = self.get_pos({
|
| 28 |
+
"bool": {
|
| 29 |
+
"must": [
|
| 30 |
+
{"term": {"word.keyword": question_word.lower()}},
|
| 31 |
+
{"term": {"synonyms.keyword": correct_answer.lower()}}
|
| 32 |
+
]
|
| 33 |
+
}
|
| 34 |
+
})
|
| 35 |
+
used_words.update([question_word, correct_answer])
|
| 36 |
|
| 37 |
choices = [correct_answer]
|
|
|
|
| 38 |
|
| 39 |
+
while len(choices) < num_ans_per_question and max_loop > 0:
|
| 40 |
+
doc = self.get_random(self.INDEX, None, cefr=cefr, pos=pos)
|
| 41 |
+
if not doc:
|
| 42 |
+
continue
|
| 43 |
+
|
| 44 |
+
candidate = doc["word"]
|
| 45 |
+
|
| 46 |
+
# Loại trừ điều kiện chung
|
| 47 |
+
if (
|
| 48 |
+
candidate in used_choices or
|
| 49 |
+
candidate in used_words or
|
| 50 |
+
candidate in synonym_set or
|
| 51 |
+
candidate == question_word or
|
| 52 |
+
candidate == correct_answer
|
| 53 |
+
):
|
| 54 |
+
continue
|
| 55 |
|
| 56 |
+
# Loại distractor có nghĩa trùng với đáp án
|
| 57 |
+
syns = set(self.get_list_synonym(candidate))
|
| 58 |
+
if correct_answer in syns:
|
| 59 |
+
continue
|
| 60 |
+
|
| 61 |
+
choices.append(candidate)
|
| 62 |
+
used_choices.add(candidate)
|
| 63 |
+
max_loop -= 1
|
| 64 |
|
| 65 |
random.shuffle(choices)
|
| 66 |
|
|
|
|
| 69 |
"type": QuestionTypeEnum.SYNONYM,
|
| 70 |
"choices": choices,
|
| 71 |
"answer": choices.index(correct_answer),
|
| 72 |
+
"explain": []
|
| 73 |
})
|
| 74 |
|
| 75 |
return result
|
| 76 |
+
|
| 77 |
+
# -----------------------------------------------------
|
| 78 |
+
# Lấy tất cả synonym của 1 từ từ ES (nhiều nghĩa)
|
| 79 |
+
# -----------------------------------------------------
|
| 80 |
+
def get_list_synonym(self, word: str):
|
| 81 |
+
es = Elastic()
|
| 82 |
+
query = {"term": {"word.keyword": word.lower()}}
|
| 83 |
+
resp = es.search(index=self.INDEX, query=query, size=1000)
|
| 84 |
+
|
| 85 |
+
hits = resp["hits"]["hits"]
|
| 86 |
+
if not hits:
|
| 87 |
+
return []
|
| 88 |
+
|
| 89 |
+
synonyms = set()
|
| 90 |
+
for h in hits:
|
| 91 |
+
s = h["_source"].get("synonyms", [])
|
| 92 |
+
synonyms.update(s)
|
| 93 |
+
|
| 94 |
+
return list(synonyms)
|
| 95 |
+
|
| 96 |
+
# -----------------------------------------------------
|
| 97 |
+
# Lấy 1 từ làm câu hỏi và 1 synonym làm đáp án
|
| 98 |
+
# -----------------------------------------------------
|
| 99 |
+
def _pick_question_word(self, list_unique_words, used_words, cefr):
|
| 100 |
+
"""
|
| 101 |
+
- Ưu tiên lấy từ danh sách đầu vào
|
| 102 |
+
- Nếu hết → lấy từ ES random theo CEFR
|
| 103 |
"""
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
# ƯU TIÊN INPUT LIST
|
| 106 |
+
while list_unique_words:
|
| 107 |
+
source = list_unique_words.pop()
|
| 108 |
|
| 109 |
+
if source in used_words:
|
| 110 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
syns = self.get_list_synonym(source)
|
| 113 |
+
valid_syns = [s for s in syns if s not in used_words]
|
| 114 |
+
if not valid_syns:
|
| 115 |
+
continue
|
| 116 |
|
| 117 |
+
correct = random.choice(valid_syns)
|
| 118 |
+
return source, correct, set(syns)
|
|
|
|
| 119 |
|
| 120 |
+
# FALLBACK ES
|
| 121 |
+
while True:
|
| 122 |
+
doc = self.get_random(self.INDEX, None, cefr=cefr)
|
| 123 |
+
if not doc:
|
| 124 |
+
continue
|
| 125 |
|
| 126 |
+
source = doc["word"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
if source in used_words:
|
| 129 |
+
continue
|
| 130 |
|
| 131 |
+
syns = self.get_list_synonym(source)
|
| 132 |
+
valid_syns = [s for s in syns if s not in used_words]
|
| 133 |
+
if not valid_syns:
|
| 134 |
+
continue
|
| 135 |
|
| 136 |
+
return source, random.choice(valid_syns), set(syns)
|
src/loaders/elastic.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from elasticsearch import Elasticsearch
|
| 2 |
+
from env import config
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class Elastic:
|
| 7 |
+
_instance = None
|
| 8 |
+
|
| 9 |
+
def __new__(cls):
|
| 10 |
+
if cls._instance is None:
|
| 11 |
+
cls._instance = Elasticsearch(
|
| 12 |
+
config["elastic"]["url"],
|
| 13 |
+
api_key=config["elastic"]["api_key"]
|
| 14 |
+
)
|
| 15 |
+
return cls._instance
|