-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocabulary_extractor.py
547 lines (459 loc) · 19.3 KB
/
vocabulary_extractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# vocabulary_extractor.py
import os
import requests
import spacy
from dotenv import load_dotenv
import sys
import openai
import textwrap
# Load environment variables from .env file
load_dotenv()
MW_LEARNER_KEY = os.getenv('MW_LEARNER_KEY') # Merriam-Webster Learner's Dictionary API Key
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') # OpenAI API Key
# Initialize OpenAI API
openai.api_key = OPENAI_API_KEY
# Initialize spaCy English model
try:
nlp = spacy.load('en_core_web_sm')
except OSError:
print("spaCy English model not found. Downloading...")
os.system("python -m spacy download en_core_web_sm")
nlp = spacy.load('en_core_web_sm')
def load_word_list(filepath):
"""
Load words from a .txt file into a set.
"""
try:
with open(filepath, 'r', encoding='utf-8') as file:
words = set(line.strip().lower() for line in file if line.strip())
return words
except FileNotFoundError:
print(f"Error: The file {filepath} was not found.")
return set()
def get_task_number():
"""
Prompt the user to input the task number (1-4) or exit.
"""
while True:
task_number = input("Select the task number to extract new words for (1, 2, 3, or 4), or type any other key to quit: ").strip()
if task_number in ['1', '2', '3', '4']:
return int(task_number)
else:
print("Exiting the program.")
return None
def read_task_files(task_number):
"""
Read the listening, question, and reading files for the given task number.
Handles different file structures based on the task.
"""
possible_files = {
1: [f"task{task_number}_question.txt"],
2: [f"task{task_number}_listening.txt", f"task{task_number}_question.txt", f"task{task_number}_reading.txt"],
3: [f"task{task_number}_listening.txt", f"task{task_number}_question.txt", f"task{task_number}_reading.txt"],
4: [f"task{task_number}_listening.txt", f"task{task_number}_question.txt"]
}
filenames = possible_files.get(task_number, [])
combined_text = ""
for filename in filenames:
try:
with open(filename, 'r', encoding='utf-8') as file:
content = file.read()
combined_text += " " + content
except FileNotFoundError:
print(f"Warning: The file {filename} was not found and will be skipped.")
if not combined_text.strip():
print("Error: No content found in the specified task files.")
return None
return combined_text
def extract_lemmatized_words(text):
"""
Use spaCy to tokenize and lemmatize the text, extracting relevant parts of speech.
"""
doc = nlp(text.lower())
words = set()
for token in doc:
if token.is_alpha and token.pos_ in {'NOUN', 'VERB', 'ADJ', 'ADV'}:
words.add(token.lemma_)
return words
def fetch_mw_audio(word):
"""
Fetch the audio pronunciation URL from Merriam-Webster's API for the given word.
"""
api_key = MW_LEARNER_KEY # Using Learner's Dictionary API for audio
url = f"https://www.dictionaryapi.com/api/v3/references/learners/json/{word}?key={api_key}"
response = requests.get(url)
if response.status_code != 200:
print(f"Error: Failed to fetch audio for '{word}'. Status Code: {response.status_code}")
return None
data = response.json()
if not data:
print(f"No data found for '{word}'.")
return None
# Handle suggestions (when word not found)
if isinstance(data[0], str):
print(f"'{word}' not found in dictionary. Suggestions: {data}")
return None
# Extract audio pronunciation
audio_url = None
for entry in data:
if 'hwi' in entry and 'prs' in entry['hwi']:
for prs in entry['hwi']['prs']:
if 'sound' in prs and 'audio' in prs['sound']:
audio_code = prs['sound']['audio']
# Construct the audio URL based on the audio code
if audio_code.startswith('bix'):
audio_url = f"https://media.merriam-webster.com/audio/prons/en/us/mp3/b/bix/{audio_code}.mp3"
elif audio_code.startswith('gg'):
audio_url = f"https://media.merriam-webster.com/audio/prons/en/us/mp3/g/gg/{audio_code}.mp3"
else:
audio_url = f"https://media.merriam-webster.com/audio/prons/en/us/mp3/{audio_code[0]}/{audio_code}.mp3"
break
if audio_url:
break
if not audio_url:
print(f"No audio pronunciation found for '{word}'.")
return None
return audio_url
def get_chatgpt_completion(prompt, model="gpt-4o-mini"):
"""
Get completion from ChatGPT.
"""
try:
response = openai.ChatCompletion.create(
model=model,
messages=[{"role": "user", "content": prompt}],
temperature=0.5,
)
return response.choices[0].message["content"].strip()
except Exception as e:
print(f"Error with OpenAI API: {e}")
return ""
def find_context_sentences(word, text):
"""
Extract all sentences containing the word.
"""
doc = nlp(text.lower())
sentences = [sent.text.strip() for sent in doc.sents if word in sent.text.lower()]
return sentences
def create_html_table(vocab_data):
"""
Create an HTML table from the vocabulary data.
Includes speaker icons that play audio pronunciations inline.
The generated HTML has line breaks but no leading indentation.
The play button inherits text color and adapts to dark and light themes.
"""
# Define the HTML template without leading indentation
html_template = textwrap.dedent("""\
<html lang="en">
<head>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
.speaker {
cursor: pointer;
font-size: 16px;
color: inherit; /* Inherit text color for dynamic theming */
}
/* Dark Mode Styles */
@media (prefers-color-scheme: dark) {
body {
background-color: #121212;
color: #e0e0e0;
}
table {
border: 1px solid #444444;
}
th {
background-color: #1e1e1e;
color: #e0e0e0;
}
td {
border: 1px solid #444444;
}
.speaker {
color: inherit; /* Inherit text color in dark mode */
}
}
/* Light Mode Styles */
@media (prefers-color-scheme: light) {
body {
background-color: #ffffff;
color: #000000;
}
th {
background-color: #f2f2f2;
color: #333333;
}
.speaker {
color: inherit; /* Inherit text color in light mode */
}
}
/* Smooth transition for theme changes */
@media (prefers-color-scheme: dark), (prefers-color-scheme: light) {
body, table, th, td, .speaker {
transition: background-color 0.3s, color 0.3s;
}
}
</style>
</head>
<body>
<table>
<tr>
<th>New Word</th>
<th>Pronunciation</th>
<th>Part of Speech</th>
<th>English Explanation</th>
<th>Chinese Explanation</th>
<th>Example Sentence</th>
</tr>
""")
# Initialize html_content with the template (line breaks preserved, no leading spaces)
html_content = html_template
# Iterate over each vocabulary entry and append table rows without leading indentation
for word in vocab_data:
# Prepare the speaker icon and audio elements
speaker_html = (
f'<span class="speaker" onclick="document.getElementById(\'audio_{word["New Word"]}\').play();">►</span>'
f'<audio id="audio_{word["New Word"]}"><source src="{word["Audio"]}" type="audio/mpeg"></audio>'
)
# Construct the table row with line breaks and internal indentation
row = textwrap.dedent(f"""\
<tr>
<td>{word['New Word']}</td>
<td>{speaker_html}</td>
<td>{word['Part of Speech']}</td>
<td>{word['English Explanation']}</td>
<td>{word['Chinese Explanation']}</td>
<td>{word['Example Sentence']}</td>
</tr>
""")
# Append the row to the html_content
html_content += row
# Append the closing tags with line breaks
closing_tags = textwrap.dedent("""\
</table>
</body>
</html>
""")
html_content += closing_tags
return html_content
def confirm_words(difficult_words):
"""
Print the difficult words and allow the user to exclude any.
"""
print("\nDifficult Words:")
for word in sorted(difficult_words):
print(f"- {word}")
exclude = input("\nDo you want to exclude any words? (y/n): ").strip().lower()
if exclude == 'y':
words_to_exclude = input("Enter the words you want to exclude, separated by commas: ")
words_to_exclude = set(word.strip().lower() for word in words_to_exclude.split(','))
excluded = difficult_words.intersection(words_to_exclude)
if excluded:
print(f"\nExcluding the following words: {', '.join(excluded)}")
return excluded
else:
print("No matching words found to exclude.")
return set()
else:
return set()
def add_words_to_basic(excluded_words, basic_words_file='basic_words.txt'):
"""
Add excluded words to the start of the basic_words.txt file.
"""
if excluded_words:
try:
# Read existing words
if os.path.exists(basic_words_file):
with open(basic_words_file, 'r', encoding='utf-8') as file:
existing_words = file.read()
else:
existing_words = ""
# Prepend excluded words
with open(basic_words_file, 'w', encoding='utf-8') as file:
for word in excluded_words:
file.write(f"{word}\n")
file.write(existing_words)
print(f"Excluded words have been added to the start of '{basic_words_file}'.")
except Exception as e:
print(f"Error writing to '{basic_words_file}': {e}")
def chunk_list(lst, chunk_size):
"""
Split a list into chunks of a specified size.
"""
for i in range(0, len(lst), chunk_size):
yield lst[i:i + chunk_size]
def main():
"""
Main function to run the vocabulary extractor in a loop until the user decides to exit.
"""
# Load word lists
basic_words = load_word_list('basic_words.txt')
toefl_words = load_word_list('toefl_words.txt')
if not basic_words:
print("Basic words list is empty or not loaded. Exiting.")
return
if not toefl_words:
print("TOEFL words list is empty or not loaded. Exiting.")
return
while True:
# Get user input for task number
task_number = get_task_number()
if task_number is None:
break
# Read and combine task files
combined_text = read_task_files(task_number)
if combined_text is None:
print("Error reading task files. Please check the files and try again.\n")
continue
# Extract lemmatized words
extracted_words = extract_lemmatized_words(combined_text)
# Identify difficult words
difficult_words = extracted_words - basic_words
difficult_words = difficult_words.intersection(toefl_words)
if not difficult_words:
print("No difficult words found based on the provided lists.\n")
continue
print(f"\nFound {len(difficult_words)} difficult words.")
# Confirm words to exclude
excluded_words = confirm_words(difficult_words)
# Remove excluded words from difficult_words
difficult_words = difficult_words - excluded_words
# Add excluded words to basic_words.txt at the start
add_words_to_basic(excluded_words)
if not difficult_words:
print("No difficult words left to process after exclusion.\n")
continue
print(f"\nProcessing {len(difficult_words)} words...")
# Initialize vocab_data
vocab_data = []
# Prepare word_data_list
word_data_list = []
for word in sorted(difficult_words):
# Fetch audio pronunciation from Merriam-Webster
audio_url = fetch_mw_audio(word)
if not audio_url:
print(f"Skipping '{word}' due to missing audio pronunciation.\n")
continue
# Find context sentences
context_sentences = find_context_sentences(word, combined_text)
context_sentence = context_sentences[0] if context_sentences else ""
word_entry = {
'word': word,
'audio_url': audio_url,
'context_sentence': context_sentence,
}
word_data_list.append(word_entry)
if not word_data_list:
print("No words to process.\n")
continue
# Process word_data_list in batches
batch_size = 10
for word_batch in chunk_list(word_data_list, batch_size):
# Build the prompt
prompt = """
You are a bilingual dictionary expert. For each of the following words and their context sentences, please provide:
1. The part of speech of the word.
2. An English explanation of the word, using definitions from Oxford or Longman dictionaries, **selecting the most relevant meaning based on the provided context**.
3. A **Chinese explanation** of the word that uses the most **direct, common, and natural Chinese word or term** to match the meaning in context. Avoid literal translations of the English definition and instead use the most familiar or concise equivalent term from Chinese dictionaries (e.g., 牛津高阶双解词典).
4. An example sentence that fits the context, written in **everyday conversational American English**.
**Important:**
- Focus strictly on the meaning of the word in the context of the given sentence.
- Ensure that the English explanation and Chinese definition are both accurately reflects the meaning of the word as used in the context sentence provided.
Here are the words and context sentences:
"""
for idx, word_entry in enumerate(word_batch):
word = word_entry['word']
context_sentence = word_entry['context_sentence']
prompt += f"\n{idx+1}. Word: {word}\n"
prompt += f" Context Sentence: \"{context_sentence}\"\n"
prompt += """
Please format your response as follows:
For each word:
Word: {word}
Part of Speech: {part of speech}
English Explanation: [Your English explanation here]
Chinese Explanation: [Your Chinese explanation here]
Example Sentence: [Your example sentence here]
**Example:**
Word: Outlet
Part of Speech: Noun
English Explanation: An electrical socket that provides power for devices.
Chinese Explanation: 电源插座。
Example Sentence: "I always sit by the outlet in class so I can keep my laptop plugged in."
"""
# Send the prompt to ChatGPT
chatgpt_response = get_chatgpt_completion(prompt)
if not chatgpt_response:
print("Skipping batch due to ChatGPT response failure.\n")
continue
# Parse the response
responses = chatgpt_response.split('Word:')[1:]
for resp in responses:
# resp starts with the word, followed by the explanations
lines = resp.strip().split('\n')
word_line = lines[0]
word = word_line.strip()
part_of_speech = "N/A"
english_explanation = "No definition available."
chinese_explanation = "翻译不可用"
example_sentence = "No example provided."
for line in lines[1:]:
if line.lower().startswith("part of speech:"):
part_of_speech = line.split("Part of Speech:")[1].strip()
elif line.lower().startswith("english explanation:"):
english_explanation = line.split("English Explanation:")[1].strip()
elif line.lower().startswith("chinese explanation:"):
chinese_explanation = line.split("Chinese Explanation:")[1].strip()
elif line.lower().startswith("example sentence:"):
example_sentence = line.split("Example Sentence:")[1].strip()
# Find the word_entry in word_batch corresponding to this word
word_entry = next((w for w in word_batch if w['word'].lower() == word.lower()), None)
if not word_entry:
print(f"Warning: Could not find word data for '{word}'.")
continue
# Fetch audio_url
audio_url = word_entry['audio_url']
# Prepare vocabulary entry
vocab_entry = {
'New Word': word.capitalize(),
'Pronunciation': "", # To be filled with speaker icon and audio
'Part of Speech': part_of_speech,
'English Explanation': english_explanation,
'Chinese Explanation': chinese_explanation,
'Example Sentence': example_sentence,
'Audio': audio_url
}
# Add vocab_entry to vocab_data
vocab_data.append(vocab_entry)
if not vocab_data:
print("No vocabulary data to generate.\n")
continue
# Create HTML table
html_table = create_html_table(vocab_data)
# Save to HTML file
output_filename = f"task{task_number}_vocabulary_list.html"
try:
with open(output_filename, 'w', encoding='utf-8') as html_file:
html_file.write(html_table)
html_file.write("\n")
print(f"Vocabulary list generated successfully and saved to '{output_filename}'.\n")
except Exception as e:
print(f"Error writing to '{output_filename}': {e}\n")
continue
if __name__ == "__main__":
main()