Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import random | |
| from transformers import pipeline | |
| class ToeicVocabularyApp: | |
| def __init__(self, data_path): | |
| self.data_path = data_path | |
| self.df = pd.read_csv(data_path) | |
| self.settings = { | |
| "num_questions": 10, | |
| "study_mode": ["Flashcard", "Trắc nghiệm", "Thử thách tốc độ", "Ôn tập theo chủ đề", "Kiểm tra"], | |
| "time_limit": 30, # in seconds | |
| "selected_topics": [] | |
| } | |
| self.classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") | |
| self.topics = ["Kinh tế", "Du lịch", "Giao tiếp", "Công nghệ", "Giáo dục"] | |
| def update_settings(self, setting_name, value): | |
| if setting_name in self.settings: | |
| self.settings[setting_name] = value | |
| def add_vocabulary(self, word, meaning, topic=None): | |
| new_entry = {"Từ vựng": word, "Nghĩa": meaning, "Chủ đề": topic if topic else ""} | |
| self.df = self.df.append(new_entry, ignore_index=True) | |
| self.df.to_csv(self.data_path, index=False) | |
| def modify_vocabulary(self, index, word=None, meaning=None, topic=None): | |
| if word: | |
| self.df.at[index, "Từ vựng"] = word | |
| if meaning: | |
| self.df.at[index, "Nghĩa"] = meaning | |
| if topic: | |
| self.df.at[index, "Chủ đề"] = topic | |
| self.df.to_csv(self.data_path, index=False) | |
| def generate_question(self, mode): | |
| if mode == "Trắc nghiệm": | |
| questions = [] | |
| for _ in range(self.settings["num_questions"]): | |
| correct_row = self.df.sample(1).iloc[0] | |
| wrong_answers = self.df[self.df["Từ vựng"] != correct_row["Từ vựng"]]["Nghĩa"].sample(3).tolist() | |
| options = [correct_row["Nghĩa"]] + wrong_answers | |
| random.shuffle(options) | |
| question = { | |
| "question": correct_row["Từ vựng"], | |
| "options": options, | |
| "correct_answer": correct_row["Nghĩa"] | |
| } | |
| questions.append(question) | |
| return questions | |
| elif mode == "Ôn tập theo chủ đề": | |
| # Placeholder for topic-based review | |
| pass | |
| # Add other modes as needed | |
| def use_guide(self): | |
| return "Guide text will be added here." | |
| def run_mode(self, mode): | |
| if mode == "Hướng dẫn sử dụng": | |
| return self.use_guide() | |
| elif mode in ["Flashcard", "Trắc nghiệm", "Thử thách tốc độ", "Ôn tập theo chủ đề", "Kiểm tra"]: | |
| return self.generate_question(mode) | |
| else: | |
| return "Invalid mode selected." | |
| # Example of usage | |
| if __name__ == "__main__": | |
| app = ToeicVocabularyApp('toeic_vocabulary.csv') | |
| app.update_settings('num_questions', 5) | |
| print(app.run_mode('Trắc nghiệm')) | |