mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
text content cleaning happens in parsing & adjusted the approach
This commit is contained in:
2
src/cpp/include/stringUtils.h
Normal file
2
src/cpp/include/stringUtils.h
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#pragma once
|
||||||
|
std::string cleanContent(std::string answer);
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
#include <qtoolbutton.h>
|
#include <qtoolbutton.h>
|
||||||
#include <qwindow.h>
|
#include <qwindow.h>
|
||||||
#include <qwindowdefs.h>
|
#include <qwindowdefs.h>
|
||||||
#include <regex>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
@@ -37,6 +36,7 @@
|
|||||||
#include "api.h"
|
#include "api.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "trainWindow.h"
|
#include "trainWindow.h"
|
||||||
|
#include "stringUtils.h"
|
||||||
|
|
||||||
struct Page {
|
struct Page {
|
||||||
int start;
|
int start;
|
||||||
@@ -158,21 +158,6 @@ QToolButton *load;
|
|||||||
QToolButton *practice;
|
QToolButton *practice;
|
||||||
|
|
||||||
|
|
||||||
const std::regex doubleSpaceExp(
|
|
||||||
" ",
|
|
||||||
std::regex_constants::ECMAScript | std::regex_constants::icase
|
|
||||||
);
|
|
||||||
|
|
||||||
const std::regex tabExp(
|
|
||||||
"\t",
|
|
||||||
std::regex_constants::ECMAScript | std::regex_constants::icase
|
|
||||||
);
|
|
||||||
|
|
||||||
const std::regex newLineExp(
|
|
||||||
"\n",
|
|
||||||
std::regex_constants::ECMAScript | std::regex_constants::icase
|
|
||||||
);
|
|
||||||
|
|
||||||
void CreateMdems(std::vector<Question*>& questions) {
|
void CreateMdems(std::vector<Question*>& questions) {
|
||||||
hMdemScroll->removeItem(mdemSpacer);
|
hMdemScroll->removeItem(mdemSpacer);
|
||||||
|
|
||||||
@@ -191,12 +176,6 @@ void CreateMdems(std::vector<Question*>& questions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto transformAnswer = [](std::string answer) -> std::string {
|
|
||||||
answer = std::regex_replace(answer, doubleSpaceExp, " ");
|
|
||||||
answer = std::regex_replace(answer, tabExp, "");
|
|
||||||
answer = std::regex_replace(answer, newLineExp, "");
|
|
||||||
return answer;
|
|
||||||
};
|
|
||||||
|
|
||||||
for (size_t i = 0; i < questions.size(); ++i) {
|
for (size_t i = 0; i < questions.size(); ++i) {
|
||||||
if (MultiElementQuestion* mw = dynamic_cast<MultiElementQuestion*>(questions[i])) {
|
if (MultiElementQuestion* mw = dynamic_cast<MultiElementQuestion*>(questions[i])) {
|
||||||
@@ -206,7 +185,6 @@ void CreateMdems(std::vector<Question*>& questions) {
|
|||||||
auto choices = mw->Choices;
|
auto choices = mw->Choices;
|
||||||
for (size_t k = 0; k < choices.size(); ++k) {
|
for (size_t k = 0; k < choices.size(); ++k) {
|
||||||
auto answer = choices[k].Answer;
|
auto answer = choices[k].Answer;
|
||||||
answer = transformAnswer(answer);
|
|
||||||
answer = std::format("- {}", answer);
|
answer = std::format("- {}", answer);
|
||||||
if (k < mdems[i]->backLabels.size()) {
|
if (k < mdems[i]->backLabels.size()) {
|
||||||
mdems[i]->backLabels[k]->setText(QString::fromStdString(answer));
|
mdems[i]->backLabels[k]->setText(QString::fromStdString(answer));
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ add_library(
|
|||||||
parser.cpp
|
parser.cpp
|
||||||
time.cpp
|
time.cpp
|
||||||
api.cpp
|
api.cpp
|
||||||
|
stringUtils.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(transpiler ${SOURCES})
|
add_executable(transpiler ${SOURCES})
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "result.h"
|
#include "result.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include "stringUtils.h"
|
||||||
|
|
||||||
struct QuestionElement {
|
struct QuestionElement {
|
||||||
bool isDash;
|
bool isDash;
|
||||||
@@ -317,19 +318,21 @@ Result<std::vector<Question*>> ParseQuestions(const std::vector<Token>& tokens)
|
|||||||
auto *question = new GroupQuestion();
|
auto *question = new GroupQuestion();
|
||||||
question->ID = id;
|
question->ID = id;
|
||||||
question->QuestionText = questionText;
|
question->QuestionText = questionText;
|
||||||
question->Section = section;
|
question->Section = section;
|
||||||
int32_t k = -1;
|
int32_t k = -1;
|
||||||
for (size_t i = 0; i < questionElements.size(); ++i) {
|
for (size_t i = 0; i < questionElements.size(); ++i) {
|
||||||
auto questionElement = questionElements[i];
|
auto questionElement = questionElements[i];
|
||||||
if (questionElement.isGroup) {
|
if (questionElement.isGroup) {
|
||||||
++k;
|
++k;
|
||||||
auto group = Group();
|
auto group = Group();
|
||||||
group.name = questionElement.content;
|
group.name = cleanContent(questionElement.content);
|
||||||
question->Groups.push_back(group);
|
question->Groups.push_back(group);
|
||||||
} else {
|
} else {
|
||||||
if (k >= 0) {
|
if (k >= 0) {
|
||||||
question->Groups[k].elements.push_back(
|
question->Groups[k].elements.push_back(
|
||||||
questionElement.content
|
cleanContent(
|
||||||
|
questionElement.content
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -341,11 +344,11 @@ Result<std::vector<Question*>> ParseQuestions(const std::vector<Token>& tokens)
|
|||||||
} else {
|
} else {
|
||||||
auto *question = new MultiElementQuestion();
|
auto *question = new MultiElementQuestion();
|
||||||
question->ID = id;
|
question->ID = id;
|
||||||
question->QuestionText = questionText;
|
question->QuestionText = cleanContent(questionText);
|
||||||
question->Section = section;
|
question->Section = section;
|
||||||
for (const auto& elem : questionElements) {
|
for (const auto& elem : questionElements) {
|
||||||
Choice choice;
|
Choice choice;
|
||||||
choice.Answer = elem.content;
|
choice.Answer = cleanContent(elem.content);
|
||||||
choice.IsCorrect = !elem.isDash;
|
choice.IsCorrect = !elem.isDash;
|
||||||
question->Choices.push_back(choice);
|
question->Choices.push_back(choice);
|
||||||
}
|
}
|
||||||
|
|||||||
25
src/cpp/transpiler/stringUtils.cpp
Normal file
25
src/cpp/transpiler/stringUtils.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include <regex>
|
||||||
|
|
||||||
|
#include "stringUtils.h"
|
||||||
|
|
||||||
|
const std::regex doubleOrMoreSpaceExp(
|
||||||
|
"\\s\\s+",
|
||||||
|
std::regex_constants::ECMAScript | std::regex_constants::icase
|
||||||
|
);
|
||||||
|
|
||||||
|
const std::regex tabExp(
|
||||||
|
"\\t+",
|
||||||
|
std::regex_constants::ECMAScript | std::regex_constants::icase
|
||||||
|
);
|
||||||
|
|
||||||
|
const std::regex newLineExp(
|
||||||
|
"\\n+",
|
||||||
|
std::regex_constants::ECMAScript | std::regex_constants::icase
|
||||||
|
);
|
||||||
|
|
||||||
|
std::string cleanContent(std::string answer) {
|
||||||
|
answer = std::regex_replace(answer, newLineExp, "");
|
||||||
|
answer = std::regex_replace(answer, tabExp, " ");
|
||||||
|
answer = std::regex_replace(answer, doubleOrMoreSpaceExp, " ");
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user