not-lain commited on
Commit
87d6c49
·
1 Parent(s): a93e419

update fronttxt for cloze match

Browse files
Files changed (1) hide show
  1. app.py +33 -17
app.py CHANGED
@@ -111,40 +111,56 @@ def extract_text_from_doc_or_docx(file):
111
 
112
  # function that generates a random string
113
  def generate_random_string(length=23):
114
- characters = string.ascii_letters + string.digits # Includes letters and digits
115
- random_string = ''.join(random.choice(characters) for _ in range(length))
116
- return random_string
 
117
 
118
  # function that adds the necessary json fields
119
- def handle_json_output(json_list : list) :
120
  n = len(json_list)
121
- for i in range(n) :
122
  # not last element
123
  random_string1 = generate_random_string()
124
  random_string2 = generate_random_string()
125
  element = json_list[i]
126
  front = element["frontText"]
127
  back = element["backText"]
128
- element["frontHTML"] = (f'<div id="element-richtextarea-{random_string1}" style="position:absolute;left:100px;top:50px;width:800px;height:300px;text-align:center;display:flex;align-items:center;font-size:40px;\">'
129
- f'<p>{front}</p></div>')
130
- element["backHTML"] = (f'<div id="element-richtextarea-{random_string2}" style="position:absolute;left:100px;top:50px;width:800px;height:300px;text-align:center;display:flex;align-items:center;font-size:40px;\">'
131
- f'<p>{back}</p></div>')
 
 
 
 
132
  element["termType"] = "basic"
133
- cloze_matches = re.findall(r'_{2,}', front)
134
  # match only the first one, if there is multiple don't do anything
135
- if (cloze_matches != []) & (len(cloze_matches)<= 2):
136
  # It's a cloze type card
137
  element["termType"] = "cloze"
 
138
  # inject the back in a span format into the front
139
  def replace_cloze(match):
140
  return f'</p><p><span class="closure">{back}</span></p><p>'
141
- front = re.sub(r'_{2,}', replace_cloze, front)
142
- # update
143
- element["frontHTML"] = (f'<div id="element-richtextarea-{random_string1}" style="position:absolute;left:100px;top:50px;width:800px;height:300px;text-align:center;display:flex;align-items:center;font-size:40px;\">'
144
- f'<p>{front}</p></div>')
 
 
 
 
 
 
 
145
  element["backText"] = ""
146
- element["backHTML"] = (f'<div id="element-richtextarea-{random_string2}" style="position:absolute;left:100px;top:50px;width:800px;height:300px;text-align:center;display:flex;align-items:center;font-size:40px;\">'
147
- f'<p><br></p></div>')
 
 
 
148
 
149
  return json_list
150
 
 
111
 
112
  # function that generates a random string
113
  def generate_random_string(length=23):
114
+ characters = string.ascii_letters + string.digits # Includes letters and digits
115
+ random_string = "".join(random.choice(characters) for _ in range(length))
116
+ return random_string
117
+
118
 
119
  # function that adds the necessary json fields
120
+ def handle_json_output(json_list: list):
121
  n = len(json_list)
122
+ for i in range(n):
123
  # not last element
124
  random_string1 = generate_random_string()
125
  random_string2 = generate_random_string()
126
  element = json_list[i]
127
  front = element["frontText"]
128
  back = element["backText"]
129
+ element["frontHTML"] = (
130
+ f'<div id="element-richtextarea-{random_string1}" style="position:absolute;left:100px;top:50px;width:800px;height:300px;text-align:center;display:flex;align-items:center;font-size:40px;">'
131
+ f"<p>{front}</p></div>"
132
+ )
133
+ element["backHTML"] = (
134
+ f'<div id="element-richtextarea-{random_string2}" style="position:absolute;left:100px;top:50px;width:800px;height:300px;text-align:center;display:flex;align-items:center;font-size:40px;">'
135
+ f"<p>{back}</p></div>"
136
+ )
137
  element["termType"] = "basic"
138
+ cloze_matches = re.findall(r"_{2,}", front)
139
  # match only the first one, if there is multiple don't do anything
140
+ if (cloze_matches != []) & (len(cloze_matches) <= 2):
141
  # It's a cloze type card
142
  element["termType"] = "cloze"
143
+
144
  # inject the back in a span format into the front
145
  def replace_cloze(match):
146
  return f'</p><p><span class="closure">{back}</span></p><p>'
147
+
148
+ front_html = re.sub(r"_{2,}", replace_cloze, front)
149
+ element["frontHTML"] = (
150
+ f'<div id="element-richtextarea-{random_string1}" style="position:absolute;left:100px;top:50px;width:800px;height:300px;text-align:center;display:flex;align-items:center;font-size:40px;">'
151
+ f"<p>{front_html}</p></div>"
152
+ )
153
+
154
+ def replace_underscores(match):
155
+ return f" {back} "
156
+
157
+ element["frontText"] = re.sub(r"_{2,}", replace_underscores, front)
158
  element["backText"] = ""
159
+
160
+ element["backHTML"] = (
161
+ f'<div id="element-richtextarea-{random_string2}" style="position:absolute;left:100px;top:50px;width:800px;height:300px;text-align:center;display:flex;align-items:center;font-size:40px;">'
162
+ f"<p><br></p></div>"
163
+ )
164
 
165
  return json_list
166