linhnguyen02 commited on
Commit ·
53ccc9b
1
Parent(s): 7a13735
update tim trong am
Browse files
src/factories/gen_question/types/stress_question.py
CHANGED
|
@@ -2,14 +2,17 @@ from typing import List
|
|
| 2 |
from collections import defaultdict
|
| 3 |
import random
|
| 4 |
|
| 5 |
-
from src.factories.gen_question.types.base import Question
|
| 6 |
from src.enums import QuestionTypeEnum
|
| 7 |
from src.utils.number import rand_exclude
|
| 8 |
from src.utils.word import get_stress_pattern, convert_word_to_ipa
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
class StressQuestion(Question):
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
if list_words is None:
|
| 14 |
list_words = []
|
| 15 |
|
|
@@ -20,11 +23,22 @@ class StressQuestion(Question):
|
|
| 20 |
|
| 21 |
stress_groups = defaultdict(list)
|
| 22 |
for word in list_words:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# create
|
| 30 |
def choice_random_words_in_stress_group(stress_group_key: int):
|
|
@@ -88,20 +102,41 @@ class StressQuestion(Question):
|
|
| 88 |
|
| 89 |
return result
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from collections import defaultdict
|
| 3 |
import random
|
| 4 |
|
| 5 |
+
from src.factories.gen_question.types.base import Question
|
| 6 |
from src.enums import QuestionTypeEnum
|
| 7 |
from src.utils.number import rand_exclude
|
| 8 |
from src.utils.word import get_stress_pattern, convert_word_to_ipa
|
| 9 |
+
from src.loaders.elastic import Elastic
|
| 10 |
|
| 11 |
|
| 12 |
class StressQuestion(Question):
|
| 13 |
+
INDEX = 'phonetic'
|
| 14 |
+
|
| 15 |
+
def generate_questions(self, list_words: List[str] = None, num_question: int = 1, num_ans_per_question: int = 4, cefr: int = 3):
|
| 16 |
if list_words is None:
|
| 17 |
list_words = []
|
| 18 |
|
|
|
|
| 23 |
|
| 24 |
stress_groups = defaultdict(list)
|
| 25 |
for word in list_words:
|
| 26 |
+
list_doc = self.get_list_word(index=self.INDEX,
|
| 27 |
+
query = {
|
| 28 |
+
"bool": {
|
| 29 |
+
"must": {
|
| 30 |
+
"term": {"word.keyword": word}
|
| 31 |
+
}
|
| 32 |
+
}})
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
for doc in list_doc:
|
| 36 |
+
if "stress" not in doc or "ipa" not in doc:
|
| 37 |
+
continue
|
| 38 |
+
stress_groups[doc["stress"]].append({
|
| 39 |
+
"word": word,
|
| 40 |
+
"ipa": doc["ipa"]
|
| 41 |
+
})
|
| 42 |
|
| 43 |
# create
|
| 44 |
def choice_random_words_in_stress_group(stress_group_key: int):
|
|
|
|
| 102 |
|
| 103 |
return result
|
| 104 |
|
| 105 |
+
def get_random_word_and_ipa_by_stress(self, stress: int, cefr: int = None):
|
| 106 |
+
es = Elastic()
|
| 107 |
+
|
| 108 |
+
must = [
|
| 109 |
+
{"term": {"stress": stress}},
|
| 110 |
+
{"exists": {"field": "ipa"}},
|
| 111 |
+
{"exists": {"field": "word"}}
|
| 112 |
+
]
|
| 113 |
+
if cefr is not None:
|
| 114 |
+
must.append({
|
| 115 |
+
"range": {
|
| 116 |
+
"cefr": {"gte": cefr - 1, "lte": cefr + 1}
|
| 117 |
+
}
|
| 118 |
+
})
|
| 119 |
+
query = {
|
| 120 |
+
"bool": {"must": must}
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
total_doc = es.count(index=self.INDEX, query=query)["count"]
|
| 124 |
+
if total_doc == 0:
|
| 125 |
+
return None
|
| 126 |
+
|
| 127 |
+
offset = random.randint(0, total_doc - 1)
|
| 128 |
+
resp = es.search(
|
| 129 |
+
index=self.INDEX,
|
| 130 |
+
query=query,
|
| 131 |
+
size=1,
|
| 132 |
+
from_=offset
|
| 133 |
+
)
|
| 134 |
+
hits = resp["hits"]["hits"]
|
| 135 |
+
|
| 136 |
+
if hits:
|
| 137 |
+
doc = hits[0]["_source"]
|
| 138 |
+
if "word" in doc and "ipa" in doc:
|
| 139 |
+
return doc["word"], doc["ipa"]
|
| 140 |
+
|
| 141 |
+
return None
|
| 142 |
+
|