klavaro  3.13
About: Klavaro is a touch typing tutor program.
  Fossies Dox: klavaro-3.13.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

adaptability.c
Go to the documentation of this file.
1 /**************************************************************************/
2 /* Klavaro - a flexible touch typing tutor */
3 /* Copyright (C) 2005-2021 Felipe Emmanuel Ferreira de Castro */
4 /* */
5 /* This file is part of Klavaro, which is a free software: you can */
6 /* redistribute it and/or modify it under the terms of the GNU General */
7 /* Public License as published by the Free Software Foundation, either */
8 /* version 3 of the License, or (at your option) any later version. */
9 /* */
10 /* Klavaro is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU General Public License for more details (in the file COPYING). */
14 /* You should have received a copy of the GNU General Public License */
15 /* along with Klavaro. If not, see <https://www.gnu.org/licenses/> */
16 /**************************************************************************/
17 
18 /*
19  * Adaptability exercise
20  */
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <glib.h>
25 #include <gtk/gtk.h>
26 
27 #include "auxiliar.h"
28 #include "main.h"
29 #include "keyboard.h"
30 #include "tutor.h"
31 #include "translation.h"
32 #include "accuracy.h"
33 #include "adaptability.h"
34 
35 /**********************************************************************
36  * Writes a random pattern of weird words in the exercise window
37  */
38 void
40 {
41  gint i, j, k;
42  gint tidx;
43  gchar *hlp;
44  gchar *utf8_text;
45  gunichar text[WORDS * (MAX_WORD_LEN + 1) + 3];
46  gunichar word[MAX_WORD_LEN + 1];
47  gboolean special;
48  gboolean word_ok = FALSE;
49  gdouble profi;
50 
51  g_message ("Accuracy errors: %lu Limit: %i", accur_error_total (), ERROR_LIMIT);
52  if (accur_ttime_n_get () >= 40)
53  g_message ("Proficiency ratio p(10)/p(40): %f Limit: %.1f",
55 
56  hlp = main_preferences_get_string ("interface", "language");
57 
58  special = accur_error_total () >= ERROR_LIMIT;
59  if (accur_ttime_n_get () >= 40)
60  {
61  profi = accur_profi_aver(9)/accur_profi_aver(39);
62  special = (special || (profi > PROFI_LIMIT));
63  }
64  if (special)
65  {
66  gtk_widget_show (get_wg ("togglebutton_toomuch_errors"));
67  special = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (get_wg ("togglebutton_toomuch_errors")));
68  }
69  else
70  gtk_widget_hide (get_wg ("togglebutton_toomuch_errors"));
71 
72  for (i = 0; i < LINES; i++)
73  { /* paragraphs per exercise */
74  tidx = 0;
75  for (j = 0; j < WORDS; j++)
76  { /* words per paragraph */
77  if (special)
78  word_ok = accur_create_word (word);
79 
80  if (!special || !word_ok)
81  {
82  if (rand () % 15)
83  adapt_create_word (word);
84  else
85  adapt_create_number (word);
86  }
87 
88  if (j == 0)
89  word[0] = keyb_unichar_toupper (word[0]);
90  else if (tutor_is_tibetan ())
91  text[tidx++] = TIBETAN_WORD_DELIMITER;
92  else
93  text[tidx++] = L' ';
94 
95  for (k = 0; word[k] != L'\0'; k++)
96  text[tidx++] = word[k];
97  }
98  if (g_str_has_prefix (hlp, "ur"))
99  text[tidx++] = URDU_STOP;
100  if (g_str_has_prefix (hlp, "pa"))
101  text[tidx++] = DEVANAGARI_STOP;
102  else if (tutor_is_tibetan ())
103  text[tidx++] = TIBETAN_STOP;
104  else if (trans_lang_has_stopmark ())
105  text[tidx++] = L'.';
106  text[tidx++] = L'\n';
107  text[tidx++] = L'\0';
108  utf8_text = g_ucs4_to_utf8 (text, -1, NULL, NULL, NULL);
109  tutor_draw_paragraph (utf8_text);
110  g_free (utf8_text);
111  }
112  g_free (hlp);
113 }
114 
115 /*
116  * Creates a random weird word
117  */
118 void
119 adapt_create_word (gunichar word[MAX_WORD_LEN + 1])
120 {
121  gchar *hlp;
122  gint i, n;
123  gint vlen, clen, slen;
124  gunichar vowels[20];
125  gunichar consonants[4 * KEY_LINE_LEN];
126  gunichar symbols[4 * KEY_LINE_LEN];
127 
128  vlen = keyb_get_vowels (vowels);
129  clen = keyb_get_consonants (consonants);
130  slen = keyb_get_symbols (symbols);
131 
132  n = rand () % (MAX_WORD_LEN - 1) + 1;
133  for (i = 0; i < n; i++)
134  {
135  if ((rand () % 25))
136  {
137  /* Literal */
138  if (i % 2) /* vowel */
139  if (rand () % 30)
140  word[i] = vowels[rand () % vlen];
141  else
142  word[i] = consonants[rand () % clen];
143  else if (rand () % 50) /* consonant */
144  word[i] = consonants[rand () % clen];
145  else
146  word[i] = vowels[rand () % vlen];
147  if (i == 0 && !(rand () % 7)) /* capital */
148  word[0] = keyb_unichar_toupper (word[0]);
149  }
150  else
151  {
152  /* Symbol */
153  word[i] = symbols[rand () % slen];
154  if (word[i] == L'\\' && i > 0)
155  word[i] = L'-';
156  if (word[i] == L'ยด' && i > 0)
157  word[i] = L'`';
158  if (rand () % 5 || word[i] == L'-' || word[i] == L'\\')
159  { /* space after symbol ==> end of word (most often) */
160  word[i + 1] = L'\0';
161  return;
162  }
163  }
164 
165  /* Avoid double diacritics */
166  if (i > 0)
167  if (keyb_is_diacritic (word[i - 1]) && keyb_is_diacritic (word[i]))
168  if ( tutor_is_tibetan() )
169  {
170  // Tibetan vowels are diacritics
171  word[i] = consonants[rand () % clen];
172  }
173  else
174  {
175  word[i] = vowels[rand () % vlen];
176  }
177  }
178  /*
179  * Last char
180  */
181  if (rand () % 20)
182  if ( tutor_is_tibetan() )
183  {
184  // Tibetan vowels are diacritics
185  word[i] = consonants[rand () % clen];
186  }
187  else
188  {
189  word[i] = vowels[rand () % vlen];
190  }
191  else
192  {
193  hlp = main_preferences_get_string ("interface", "language");
194  if (g_str_has_prefix (hlp, "ur"))
195  word[n] = URDU_COMMA;
196  else if (tutor_is_tibetan())
197  word[n] = TIBETAN_COMMA;
198  else if (trans_lang_has_stopmark ())
199  word[n] = L',';
200  g_free (hlp);
201  }
202 
203  /*
204  * Null terminated unistring
205  */
206  word[n + 1] = L'\0';
207 }
208 
209 /*
210  * Creates a random number
211  */
212 void
213 adapt_create_number (gunichar ucs4_word[MAX_WORD_LEN + 1])
214 {
215  gint i;
216  gint alen;
217  gboolean arabic;
218  const gchar digits[11] = "0123456789";
219  gunichar altnums[30];
220 
221  arabic = TRUE;
222  alen = keyb_get_altnums (altnums);
223  if (alen > 5)
224  arabic = rand () % 7 ? FALSE : TRUE;
225  for (i = 0; i < 4; i++)
226  {
227  if (arabic)
228  ucs4_word[i] = digits[rand () % 10];
229  else
230  ucs4_word[i] = altnums[rand () % alen];
231  }
232  ucs4_word[4] = L'\0';
233 }
234 
235 /**********************************************************************
236  * Put on the screen the final comments
237  */
238 void
240 {
241  gchar *tmp_str;
242  GtkWidget *wg;
243  GtkTextBuffer *buf;
244 
245  /*
246  * Comments
247  */
248  if (accuracy < tutor_goal_level(0))
249  tmp_str = g_strdup (":-(\n");
250  else if (accuracy < tutor_goal_level(1))
251  tmp_str = g_strdup_printf (_(" Your accuracy rate is below %.0f%%...\n"
252  " Could you please try again to improve it?\n"), tutor_goal_level(1));
253  else if (accuracy < tutor_goal_level(2))
254  tmp_str = g_strdup_printf (_(" You are doing well. But...\n"
255  " Could you make the accuracy reach %.0f%%?\n"), tutor_goal_level(2));
256  else if (accuracy < tutor_goal_accuracy ())
257  tmp_str = g_strdup_printf (_(" You are almost there,"
258  " but your accuracy rate is still below %.0f%%.\n"
259  " Try a few more times,"
260  " or maybe you're getting upset, so go to another kind of exercise.\n"),
262  else
263  {
264  tmp_str = g_strdup_printf (_(" Very good!\n"
265  " You succeeded with an accuracy rate above %.0f%%.\n"
266  " Now it is time to increase your velocity.\n"
267  " Go to the 3rd exercise at the main menu.\n"),
269 
271  /*
272  gtk_widget_hide (get_wg ("togglebutton_toomuch_errors"));
273  */
274  }
275 
276  wg = get_wg ("text_tutor");
277  buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (wg));
278  gtk_text_buffer_insert_at_cursor (buf, tmp_str, strlen (tmp_str));
279  g_free (tmp_str);
280 }
gulong accur_error_total()
Definition: accuracy.c:375
void accur_terror_reset()
Definition: accuracy.c:55
gboolean accur_create_word(gunichar word[9+1])
Definition: accuracy.c:543
gint accur_ttime_n_get(void)
Definition: accuracy.c:82
gdouble accur_profi_aver(gint idx)
Definition: accuracy.c:387
#define PROFI_LIMIT
Definition: accuracy.h:27
#define ERROR_LIMIT
Definition: accuracy.h:26
void adapt_comment(gdouble accuracy)
Definition: adaptability.c:239
void adapt_create_number(gunichar ucs4_word[9+1])
Definition: adaptability.c:213
void adapt_draw_random_pattern()
Definition: adaptability.c:39
void adapt_create_word(gunichar word[9+1])
Definition: adaptability.c:119
#define LINES
Definition: adaptability.h:18
#define MAX_WORD_LEN
Definition: adaptability.h:20
#define WORDS
Definition: adaptability.h:19
GtkWidget * get_wg(gchar *name)
Definition: auxiliar.c:40
#define _(String)
Definition: auxiliar.h:45
guint i
Definition: keyboard.c:55
gunichar keyb_unichar_toupper(gunichar uchar)
Definition: keyboard.c:591
gint keyb_get_altnums(gunichar *altnums)
Definition: keyboard.c:567
guint j
Definition: keyboard.c:56
gint keyb_get_consonants(gunichar *consonants)
Definition: keyboard.c:518
gint keyb_get_vowels(gunichar *vows)
Definition: keyboard.c:491
gboolean keyb_is_diacritic(gunichar chr)
Definition: keyboard.c:476
gint keyb_get_symbols(gunichar *symbols)
Definition: keyboard.c:543
const gunichar vowels[]
Definition: keyboard.c:73
#define DEVANAGARI_STOP
Definition: keyboard.h:26
#define KEY_LINE_LEN
Definition: keyboard.h:21
#define URDU_COMMA
Definition: keyboard.h:25
#define TIBETAN_COMMA
Definition: keyboard.h:29
#define TIBETAN_WORD_DELIMITER
Definition: keyboard.h:27
#define TIBETAN_STOP
Definition: keyboard.h:28
#define URDU_STOP
Definition: keyboard.h:24
gchar * main_preferences_get_string(gchar *group, gchar *key)
Definition: main.c:109
gboolean trans_lang_has_stopmark()
Definition: translation.c:240
gboolean tutor_is_tibetan()
Definition: tutor.c:121
gdouble tutor_goal_level(guint n)
Definition: tutor.c:279
gdouble tutor_goal_accuracy()
Definition: tutor.c:240
double accuracy
Definition: tutor.c:67
void tutor_draw_paragraph(gchar *utf8_text)
Definition: tutor.c:1575