106 lines
2.8 KiB
TypeScript
106 lines
2.8 KiB
TypeScript
//import { QuizData } from "./database.ts";
|
|
|
|
import { QuizConfig } from "./types.ts";
|
|
|
|
// Class to parse text and add translations
|
|
export class TextToQuizTranslations {
|
|
quizData: QuizConfig;
|
|
constructor(quiz: QuizConfig) {
|
|
this.quizData = {
|
|
quiz_config_id: quiz.quiz_config_id,
|
|
owner_id: quiz.owner_id,
|
|
quiz_name: quiz.quiz_name,
|
|
active_date: quiz.active_date,
|
|
quiz_mode: quiz.quiz_mode,
|
|
questions: quiz.questions,
|
|
results: quiz.results,
|
|
};
|
|
}
|
|
|
|
parseText(text: string) {
|
|
const lines = text.split("\n");
|
|
let question = "";
|
|
let meaning = "";
|
|
const languageLines: string[] = [];
|
|
|
|
lines.forEach((value: string) => {
|
|
const thisLine = value.trim();
|
|
if (thisLine !== "") {
|
|
languageLines.push(thisLine);
|
|
}
|
|
});
|
|
|
|
const newQuestions: any = [];
|
|
let nextQuestionIndex = this.quizData.questions.length;
|
|
languageLines.forEach((value: string, index: number) => {
|
|
const sequence = index % 2;
|
|
|
|
if (sequence == 0) {
|
|
question = value;
|
|
} else {
|
|
meaning = value;
|
|
const thisQuestion: {
|
|
id: string;
|
|
question_index?: number;
|
|
quiz_id: string;
|
|
owner_id: string;
|
|
language_1: string;
|
|
language_2: string;
|
|
text_1: string;
|
|
audio_file_1: string;
|
|
text_2: string;
|
|
audio_file_2: string;
|
|
order_num: number;
|
|
background_file: string;
|
|
background_image_data: string;
|
|
video_file: string;
|
|
video_file_data: string;
|
|
} = {
|
|
quiz_id: this.quizData.quiz_config_id,
|
|
owner_id: this.quizData.owner_id,
|
|
question_index: nextQuestionIndex,
|
|
language_1: "bis-ph",
|
|
language_2: "en-us",
|
|
text_1: question,
|
|
text_2: meaning,
|
|
audio_file_1: "",
|
|
audio_file_2: "",
|
|
background_file: "",
|
|
background_image_data: "",
|
|
id: crypto.randomUUID(),
|
|
order_num: 1,
|
|
video_file: "",
|
|
video_file_data: "",
|
|
};
|
|
|
|
nextQuestionIndex++;
|
|
|
|
newQuestions.push(thisQuestion);
|
|
}
|
|
});
|
|
|
|
this.quizData.questions = [...newQuestions, ...this.quizData.questions];
|
|
|
|
return this.quizData;
|
|
}
|
|
|
|
async parseFile(fileName: string) {
|
|
try {
|
|
const text = await Deno.readTextFile(fileName);
|
|
return this.parseText(text);
|
|
} catch (error) {
|
|
console.error(`Error reading file: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Example usage:
|
|
// const quizData = { /* ... */ };
|
|
// const quizToText = new QuizToTextFile(quizData);
|
|
// await quizToText.saveToFile('quiz_translations.txt');
|
|
|
|
// const textToQuiz = new TextToQuizTranslations();
|
|
// const parsedQuizData = await textToQuiz.parseFile('quiz_translations.txt');
|
|
// console.log(parsedQuizData);
|