• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • practice
writtenpracticevalidator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2010 Benjamin Schleinzer <ben-kde@schleinzer.eu>
3  Copyright 2007-2010 Frederik Gladhorn <gladhorn@kde.org>
4 ***************************************************************************/
5 
6 /***************************************************************************
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 ***************************************************************************/
14 #include "writtenpracticevalidator.h"
15 #include "testentry.h"
16 #include "prefs.h"
17 
18 #include <keduvoctranslation.h>
19 #include <kdebug.h>
20 
23 namespace ParleyStringHandlerOld
24 {
25 QString stripAccents(const QString& original)
26 {
27  QString noAccents;
28  QString decomposed = original.normalized(QString::NormalizationForm_D);
29  for (int i = 0; i < decomposed.length(); ++i) {
30  if (decomposed[i].category() != 1) {
31  noAccents.append(decomposed[i]);
32  }
33  }
34  kDebug() << original << " without accents: " << noAccents;
35  return noAccents;
36 }
37 }
38 
39 using namespace Practice;
40 
41 WrittenPracticeValidator::WrittenPracticeValidator(int translation, KEduVocDocument* doc)
42  : m_entry(0),
43  m_doc(doc),
44  m_error(0),
45  m_speller(0),
46  m_spellerAvailable(false)
47 {
48  setLanguage(translation);
49 }
50 
51 WrittenPracticeValidator::~WrittenPracticeValidator()
52 {
53  delete m_speller;
54 }
55 
56 void WrittenPracticeValidator::setEntry(TestEntry* entry)
57 {
58  m_entry = entry;
59 }
60 
61 void WrittenPracticeValidator::setLanguage(int translation)
62 {
63  m_translation = translation;
64 
65  // default: try locale
66  if (!m_speller) {
67  m_speller = new Sonnet::Speller(m_doc->identifier(translation).locale());
68  } else {
69  m_speller->setLanguage(m_doc->identifier(translation).locale());
70  }
71 
72  // we might succeed with language name instead.
73  if (!m_speller->isValid()) {
74  m_speller->setLanguage(m_doc->identifier(translation).name());
75  }
76 
77  if (!m_speller->isValid()) {
78  kDebug() << "No spellchecker for current language found: " << m_doc->identifier(m_translation).locale();
79  kDebug() << "Available dictionaries: " << m_speller->availableLanguages()
80  << "\n names: " << m_speller->availableLanguageNames()
81  << "\n backends: " << m_speller->availableBackends();
82  m_spellerAvailable = false;
83  } else {
84  m_spellerAvailable = true;
85  }
86 
87 }
88 
89 bool WrittenPracticeValidator::spellcheckerAvailable()
90 {
91  return m_spellerAvailable;
92 }
93 
94 void WrittenPracticeValidator::validateAnswer(const QString& answer)
95 {
96  if (m_entry == 0) {
97  kError() << "No entry set, cannot verify answer.";
98  return;
99  }
100 
101  QString correct = m_entry->entry()->translation(m_entry->languageTo())->text();
102 
103  kDebug() << "Correct answer should be: " << correct;
104  m_error = 0;
105 
106  //Check for empty answers and valid answers first
107  if (answer.isEmpty()) {
108  m_error |= TestEntry::Wrong;
109  kDebug() << "Empty answer ";
110  } else if (isCorrect(correct, answer)) {
111  m_error |= TestEntry::Correct;
112  } else {
113  //Check for all valid errors to build a list of
114  //possible mistakes. This provides us with useful information
115  //that we can use to give feedback to the user.
116  if (isCapitalizationMistake(correct, answer)) {
117  m_error |= TestEntry::Correct;
118  } else if (isAccentMistake(correct, answer)) {
119  m_error |= TestEntry::Correct;
120  } else if (isSynonymMistake(answer)) {
121  m_error |= TestEntry::Correct;
122  } else {
123  m_error |= TestEntry::Wrong;
124  kDebug() << "Wrong answer: " << answer;
125  }
126  }
127  kDebug() << "Error code " << m_error;
128 
129  m_entry->setLastErrors(m_error);
130 }
131 
132 QString WrittenPracticeValidator::getCorrectedAnswer()
133 {
134  return m_correctedAnswer;
135 }
136 
137 bool WrittenPracticeValidator::isCorrect(const QString& correct, const QString& answer)
138 {
139  if (answer == correct ) {
140  kDebug() << "Correct answer was given";
141  return true;
142  }
143  return false;
144 }
145 
146 bool WrittenPracticeValidator::isSynonymMistake(const QString& answer)
147 {
148  foreach(KEduVocTranslation * synonym, m_entry->entry()->translation(m_entry->languageTo())->synonyms()) {
149  if (synonym->text() == answer ||
150  (Prefs::ignoreCapitalizationMistakes() && isCapitalizationMistake(synonym->text(), answer)) ||
151  (Prefs::ignoreAccentMistakes() && isAccentMistake(synonym->text(), answer))) {
152  kDebug() << "Synonym entered: " << synonym->text() << " answer: " << answer;
153  m_correctedAnswer = synonym->text();
154  m_error |= TestEntry::Synonym;
155  //only return true if accept these kinds of mistakes
156  //otherwise just set the error flag
157  if (Prefs::countSynonymsAsCorrect()) {
158  return true;
159  }
160  }
161  }
162  return false;
163 }
164 
165 bool WrittenPracticeValidator::isCapitalizationMistake(const QString& original, const QString& answer)
166 {
167  if (answer.toLower() == original.toLower()) {
168  kDebug() << "CapitalizationMistake: " << original << " answer: " << answer;
169  m_error |= TestEntry::CapitalizationMistake;
170  m_correctedAnswer = answer;
171  //only return true if accept these kinds of mistakes
172  //otherwise just set the error flag
173  if (Prefs::ignoreCapitalizationMistakes())
174  return true;
175  }
176  return false;
177 }
178 
179 bool WrittenPracticeValidator::isAccentMistake(const QString& original, const QString& answer)
180 {
181  QString stripedOriginal = ParleyStringHandlerOld::stripAccents(original);
182  QString stripedAnswer = ParleyStringHandlerOld::stripAccents(answer);
183  if (stripedOriginal == stripedAnswer ||
184  (Prefs::ignoreCapitalizationMistakes() && isCapitalizationMistake(stripedOriginal, stripedAnswer))) {
185  kDebug() << "AccentMistake: " << original << " answer: " << answer;
186  m_error |= TestEntry::AccentMistake;
187  m_correctedAnswer = answer;
188  //only return true if accept these kinds of mistakes
189  //otherwise just set the error flag
190  if (Prefs::ignoreAccentMistakes()) {
191  return true;
192  }
193  }
194  return false;
195 }
TestEntry::entry
KEduVocExpression * entry() const
Definition: testentry.cpp:149
Practice::WrittenPracticeValidator::spellcheckerAvailable
bool spellcheckerAvailable()
Definition: writtenpracticevalidator.cpp:89
QString::append
QString & append(QChar ch)
Practice::WrittenPracticeValidator::~WrittenPracticeValidator
~WrittenPracticeValidator()
Definition: writtenpracticevalidator.cpp:51
TestEntry::Wrong
< no error, solution was right
Definition: testentry.h:37
TestEntry::Synonym
< a false friend
Definition: testentry.h:32
prefs.h
QString::normalized
QString normalized(NormalizationForm mode) const
testentry.h
TestEntry::AccentMistake
< capitalization error (whAt)
Definition: testentry.h:28
TestEntry::Correct
< the part that was entered is right, but not complete
Definition: testentry.h:36
QString::isEmpty
bool isEmpty() const
ParleyStringHandlerOld::stripAccents
QString stripAccents(const QString &original)
Definition: answervalidatorold.cpp:29
QString
writtenpracticevalidator.h
QString::toLower
QString toLower() const
TestEntry::setLastErrors
void setLastErrors(ErrorTypes errorTypes)
Definition: testentry.cpp:129
Practice::WrittenPracticeValidator::WrittenPracticeValidator
WrittenPracticeValidator(int translation, KEduVocDocument *doc)
Definition: writtenpracticevalidator.cpp:41
Prefs::countSynonymsAsCorrect
static bool countSynonymsAsCorrect()
Get When the synonym instead of the word was entered, does it count as correct?
Definition: prefs.h:678
Prefs::ignoreAccentMistakes
static bool ignoreAccentMistakes()
Get Count answers as right when only the accentuation is wrong.
Definition: prefs.h:507
Practice::WrittenPracticeValidator::validateAnswer
void validateAnswer(const QString &answer)
Definition: writtenpracticevalidator.cpp:94
QString::length
int length() const
TestEntry::CapitalizationMistake
< misspelled
Definition: testentry.h:27
Practice::WrittenPracticeValidator::getCorrectedAnswer
QString getCorrectedAnswer()
Definition: writtenpracticevalidator.cpp:132
Prefs::ignoreCapitalizationMistakes
static bool ignoreCapitalizationMistakes()
Get Count answers as right when only the capitalization is wrong.
Definition: prefs.h:526
TestEntry::languageTo
int languageTo() const
Definition: testentry.cpp:114
Practice::WrittenPracticeValidator::setEntry
void setEntry(TestEntry *entry)
Definition: writtenpracticevalidator.cpp:56
TestEntry
Definition: testentry.h:22
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal