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

parley

  • sources
  • kde-4.12
  • 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  QString stripAccents(const QString& original){
25  QString noAccents;
26  QString decomposed = original.normalized(QString::NormalizationForm_D);
27  for (int i = 0; i < decomposed.length(); ++i) {
28  if ( decomposed[i].category() != 1 ) {
29  noAccents.append(decomposed[i]);
30  }
31  }
32  kDebug() << original << " without accents: " << noAccents;
33  return noAccents;
34  }
35 }
36 
37 using namespace Practice;
38 
39 WrittenPracticeValidator::WrittenPracticeValidator(int translation, KEduVocDocument* doc)
40 :m_entry(0),
41 m_doc(doc),
42 m_error(0),
43 m_speller(0),
44 m_spellerAvailable(false)
45 {
46  setLanguage(translation);
47 }
48 
49 WrittenPracticeValidator::~WrittenPracticeValidator()
50 {
51  delete m_speller;
52 }
53 
54 void WrittenPracticeValidator::setEntry(TestEntry* entry)
55 {
56  m_entry = entry;
57 }
58 
59 void WrittenPracticeValidator::setLanguage(int translation)
60 {
61  m_translation = translation;
62 
63  // default: try locale
64  if ( !m_speller ) {
65  m_speller = new Sonnet::Speller(m_doc->identifier(translation).locale());
66  } else {
67  m_speller->setLanguage(m_doc->identifier(translation).locale());
68  }
69 
70  // we might succeed with language name instead.
71  if ( !m_speller->isValid() ) {
72  m_speller->setLanguage(m_doc->identifier(translation).name());
73  }
74 
75  if ( !m_speller->isValid() ) {
76  kDebug() << "No spellchecker for current language found: " << m_doc->identifier(m_translation).locale();
77  kDebug() << "Available dictionaries: " << m_speller->availableLanguages()
78  << "\n names: " << m_speller->availableLanguageNames()
79  << "\n backends: " << m_speller->availableBackends();
80  m_spellerAvailable = false;
81  } else {
82  m_spellerAvailable = true;
83  }
84 
85 }
86 
87 bool WrittenPracticeValidator::spellcheckerAvailable()
88 {
89  return m_spellerAvailable;
90 }
91 
92 void WrittenPracticeValidator::validateAnswer(const QString& answer)
93 {
94  if ( m_entry == 0 ) {
95  kError() << "No entry set, cannot verify answer.";
96  return;
97  }
98 
99  QString correct = m_entry->entry()->translation(m_translation)->text();
100 
101  kDebug() << "Correct answer should be: " << correct;
102  m_error = 0;
103 
104  //Check for empty answers and valid answers first
105  if (answer.isEmpty()) {
106  m_error |= TestEntry::Wrong;
107  kDebug() << "Empty answer ";
108  } else if(isCorrect(answer)){
109  m_error |= TestEntry::Correct;
110  } else {
111  //Check for all valid errors to build a list of
112  //possible mistakes. This provides us with usefull informations
113  //that we can use to give feedback to the user.
114  if(isCapitalizationMistake(correct, answer)) {
115  m_error |= TestEntry::Correct;
116  } else if(isAccentMistake(correct, answer)) {
117  m_error |= TestEntry::Correct;
118  } else if(isSynonymMistake(answer)) {
119  m_error |= TestEntry::Correct;
120  } else {
121  m_error |= TestEntry::Wrong;
122  kDebug() << "Wrong answer: " << answer;
123  }
124  }
125  kDebug() << "Error code " << m_error;
126 
127  m_entry->setLastErrors(m_error);
128 }
129 
130 QString WrittenPracticeValidator::getCorrectedAnswer()
131 {
132  return m_correctedAnswer;
133 }
134 
135 bool WrittenPracticeValidator::isCorrect(const QString& answer)
136 {
137  if(answer == m_entry->entry()->translation(m_translation)->text()) {
138  kDebug() << "Correct answer was given";
139  return true;
140  }
141  return false;
142 }
143 
144 bool WrittenPracticeValidator::isSynonymMistake(const QString& answer)
145 {
146  foreach(KEduVocTranslation *synonym, m_entry->entry()->translation(m_translation)->synonyms()) {
147  if (synonym->text() == answer ||
148  (Prefs::ignoreCapitalizationMistakes() && isCapitalizationMistake(synonym->text(),answer)) ||
149  (Prefs::ignoreAccentMistakes() && isAccentMistake(synonym->text(),answer))) {
150  kDebug() << "Synonym entered: " << synonym->text() << " answer: " << answer;
151  m_correctedAnswer = synonym->text();
152  m_error |= TestEntry::Synonym;
153  //only return true if accept these kinds of mistakes
154  //otherwise just set the error flag
155  if(Prefs::countSynonymsAsCorrect()) {
156  return true;
157  }
158  }
159  }
160  return false;
161 }
162 
163 bool WrittenPracticeValidator::isCapitalizationMistake(const QString& original, const QString& answer)
164 {
165  if (answer.toLower() == original.toLower()) {
166  kDebug() << "CapitalizationMistake: " << original << " answer: " << answer;
167  m_error |= TestEntry::CapitalizationMistake;
168  m_correctedAnswer = answer;
169  //only return true if accept these kinds of mistakes
170  //otherwise just set the error flag
171  if(Prefs::ignoreCapitalizationMistakes())
172  return true;
173  }
174  return false;
175 }
176 
177 bool WrittenPracticeValidator::isAccentMistake(const QString& original, const QString& answer)
178 {
179  QString stripedOriginal = ParleyStringHandlerOld::stripAccents(original);
180  QString stripedAnswer = ParleyStringHandlerOld::stripAccents(answer);
181  if ( stripedOriginal == stripedAnswer ||
182  (Prefs::ignoreCapitalizationMistakes() && isCapitalizationMistake(stripedOriginal,stripedAnswer))) {
183  kDebug() << "AccentMistake: " << original << " answer: " << answer;
184  m_error |= TestEntry::AccentMistake;
185  m_correctedAnswer = answer;
186  //only return true if accept these kinds of mistakes
187  //otherwise just set the error flag
188  if(Prefs::ignoreAccentMistakes()) {
189  return true;
190  }
191  }
192  return false;
193 }
194 
Practice::WrittenPracticeValidator::spellcheckerAvailable
bool spellcheckerAvailable()
Definition: writtenpracticevalidator.cpp:87
Practice::WrittenPracticeValidator::~WrittenPracticeValidator
~WrittenPracticeValidator()
Definition: writtenpracticevalidator.cpp:49
TestEntry::Wrong
< no error, solution was right
Definition: testentry.h:37
TestEntry::Synonym
< a false friend
Definition: testentry.h:32
prefs.h
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
ParleyStringHandlerOld::stripAccents
QString stripAccents(const QString &original)
Definition: answervalidatorold.cpp:28
writtenpracticevalidator.h
TestEntry::setLastErrors
void setLastErrors(ErrorTypes errorTypes)
Definition: testentry.cpp:114
Practice::WrittenPracticeValidator::WrittenPracticeValidator
WrittenPracticeValidator(int translation, KEduVocDocument *doc)
Definition: writtenpracticevalidator.cpp:39
Prefs::countSynonymsAsCorrect
static bool countSynonymsAsCorrect()
Get When the synonym instead of the word was entered, does it count as correct?
Definition: prefs.h:673
Prefs::ignoreAccentMistakes
static bool ignoreAccentMistakes()
Get Count answers as right when only the accentuation is wrong.
Definition: prefs.h:502
Practice::WrittenPracticeValidator::validateAnswer
void validateAnswer(const QString &answer)
Definition: writtenpracticevalidator.cpp:92
TestEntry::CapitalizationMistake
< misspelled
Definition: testentry.h:27
Practice::WrittenPracticeValidator::getCorrectedAnswer
QString getCorrectedAnswer()
Definition: writtenpracticevalidator.cpp:130
Prefs::ignoreCapitalizationMistakes
static bool ignoreCapitalizationMistakes()
Get Count answers as right when only the capitalization is wrong.
Definition: prefs.h:521
TestEntry::entry
KEduVocExpression * entry()
Definition: testentry.cpp:134
Practice::WrittenPracticeValidator::setEntry
void setEntry(TestEntry *entry)
Definition: writtenpracticevalidator.cpp:54
TestEntry
Definition: testentry.h:22
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:06 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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