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, nltk_words
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
- def generate_questions(self, list_words: List[str] = None, num_question: int = 1, num_ans_per_question: int = 4):
 
 
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
- stress = get_stress_pattern(word)
24
- ipa = convert_word_to_ipa(word)
25
- if ipa is None or stress is None:
26
- continue
27
- stress_groups[stress].append({"word": word, "ipa": ipa})
 
 
 
 
 
 
 
 
 
 
 
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
- @staticmethod
92
- def get_random_word_and_ipa_by_stress(stress: int):
93
- max_attempts = 10000
94
- attempts = 0
95
- while attempts < max_attempts:
96
- if not nltk_words:
97
- return None
98
- word = random.choice(nltk_words)
99
- word_ipa = convert_word_to_ipa(word)
100
- word_stress = get_stress_pattern(word)
101
- if word_ipa is None or word_stress is None:
102
- attempts += 1
103
- continue
104
- if word_stress == stress:
105
- return word, word_ipa
106
- attempts += 1
107
- return None # Return None if no word is found
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+