1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
Description: Learn words in UTF-8.
RTL languages are not handled properly; the code needs to be
reorganized to use Pango in order to support them.
.
Fix memory leak in ClearDictionary.
Check fscanf result and close stream in Niall_LoadDictionary.
Bug-Debian: https://bugs.debian.org/284416
Author: Yavor Doganov <yavor@gnu.org>
Forwarded: no
Last-Update: 2018-10-17
---
--- gniall.orig/niall.c
+++ gniall/niall.c
@@ -33,6 +33,7 @@
#include <string.h>
#include <ctype.h>
#include <time.h>
+#include <glib.h>
#include "niall.h"
@@ -60,6 +61,7 @@
};
WORD *WordList;
+static GRegex *re = NULL;
extern void Niall_Print( char *fmt, ... );
extern void Niall_Warning( char *fmt, ... );
@@ -216,78 +218,34 @@
/* Add the words in a processed sentence to the dictionary.
*/
-static void AddWords(char *Buffer)
+static void AddWords(gchar **words)
{
int LastWord=0;
int i;
- while(strlen(Buffer))
- {
- while(isspace(*Buffer)) Buffer++;
- for(i=0;(!isspace(Buffer[i]))&&(Buffer[i]!=0);i++);
+ for (i=0; i < g_strv_length (words); i++)
+ if (strlen(words[i]))
+ LastWord = IndexWord(words[i], LastWord);
- if(strlen(Buffer)==0) /* No more words */
- {
- if(LastWord) IndexWord(END_SENTENCE,LastWord);
- }
- else if(Buffer[i]==0)
- {
- LastWord=IndexWord(Buffer,LastWord);
- IndexWord(END_SENTENCE,LastWord);
- Buffer += i;
- }
- else
- {
- Buffer[i] = 0;
- LastWord = IndexWord(Buffer,LastWord);
- Buffer += (i+1);
- }
- }
+ IndexWord(END_SENTENCE,LastWord);
+ g_strfreev(words);
}
/* Process a line of text
*/
void Niall_Learn(char *Buffer)
{
- char c;
- int i,j;
+ gchar **words;
+ gchar *str;
- /* Remove trailing spaces
- */
- for(i=strlen(Buffer)-1;i>=0;i--)
- {
- if(!isspace(Buffer[i])) break;
- Buffer[i]=0;
- }
+ if (!re)
+ re = g_regex_new("\\W+", 0, 0, NULL);
- /* Strip punctuation and break sentence if necessary
- */
- for(i=0;i<strlen(Buffer);i++)
- {
- c=Buffer[i];
+ str = g_utf8_strdown(Buffer, -1);
+ words = g_regex_split(re, str, 0);
+ g_free(str);
- if(( c=='.' )||( c==',' ))
- {
- if( i+1 < strlen(Buffer))
- {
- Buffer[i]=0;
- Niall_Learn(&Buffer[i+1]);
- }
- Buffer[i]=0;
- break;
- }
- else if(isspace(c))
- {
- Buffer[i]=' ';
- }
- else if(!isalnum(c))
- {
- for(j=i+1;j<=strlen(Buffer);j++) Buffer[j-1]=Buffer[j];
- i--;
- }
- else Buffer[i]=tolower(c);
- }
- AddWords(Buffer);
+ AddWords(words);
}
@@ -364,7 +322,9 @@
*/
void Niall_Reply(char *Buffer,int BufSize)
{
- WORD *Word;
+ gchar *str, buf[7];
+ gunichar c;
+ gint len;
/* Clear the buffer.
*/
@@ -387,9 +347,14 @@
/* Speak some words of wisdom.
*/
StringWord(Buffer,BufSize,WordList);
- Buffer[0]=toupper(Buffer[0]);
+ c = g_unichar_toupper(g_utf8_get_char(Buffer));
+ len = g_unichar_to_utf8(c, buf);
+ buf[len] = '\0';
+ str = g_strdup_printf("%s%s.", buf,
+ g_utf8_offset_to_pointer(Buffer, 1));
fprintf(stderr, "speaking 1\n");
- safeStrcat(Buffer,BufSize,".");
+ strcpy(Buffer, str);
+ g_free(str);
fprintf(stderr, "speaking 2\n");
}
@@ -413,6 +378,7 @@
free(thisAscn);
}
nextWord=thisWord->Next;
+ free(thisWord->Data);
free(thisWord);
}
WordList=NULL;
@@ -514,21 +480,23 @@
Niall_Warning("File %s not found.",FileName);
return;
}
- fscanf(fHandle,"%s %d\n",Buffer,&nWords);
- if((strcmp(Buffer,FILE_ID))||(nWords<2))
+ k = fscanf(fHandle,"%s %d\n",Buffer,&nWords);
+ if(k!=2||(strcmp(Buffer,FILE_ID))||(nWords<2))
{
Niall_Warning("File %s is not a valid Niall file.",FileName);
+ fclose(fHandle);
return;
}
ClearDictionary();
for(i=0;i<nWords;i++)
{
- fscanf(fHandle,"%4d: %s %d|",&d,Buffer,&nAsocs);
- if((d!=i)||(nAsocs<1))
+ k = fscanf(fHandle,"%4d: %s %d|",&d,Buffer,&nAsocs);
+ if(k!=3||(d!=i)||(nAsocs<1))
{
Niall_Warning("Word %d is corrupted.",i);
Niall_NewDictionary();
+ fclose(fHandle);
return;
}
if(Buffer[0]=='>')
@@ -537,6 +505,7 @@
{
Niall_Warning("Word %d is corrupted.",i);
Niall_NewDictionary();
+ fclose(fHandle);
return;
}
Buffer[0]=0;
@@ -546,44 +515,38 @@
for(j=0;j<nAsocs;)
{
- fscanf(fHandle," %d(%d)",&w,&p);
- if(w>=nWords)
+ k = fscanf(fHandle," %d(%d)",&w,&p);
+ if(k!=2||w>=nWords)
{
Niall_Warning("Word %d/Assoc %d is corrupted.",i,j);
Niall_NewDictionary();
+ fclose(fHandle);
return;
}
for(k=0;k<p;k++) Associate(Word,w);
j+=p;
}
}
+ fclose(fHandle);
}
/* Remove punctuation from a word
*/
static void StripPunctuation(char *Buffer)
{
- char c;
- int i,j;
+ gchar **words;
+ gchar *str;
+
+ if (!re)
+ re = g_regex_new("\\W+", 0, 0, NULL);
/* Strip punctuation and remove extra words
*/
- for(i=0;i<strlen(Buffer);i++)
- {
- c=Buffer[i];
-
- if(( c=='.' )||( c==',' )||(isspace(c)))
- {
- Buffer[i]=0;
- break;
- }
- else if(!isalnum(c))
- {
- for(j=i+1;j<=strlen(Buffer);j++) Buffer[j-1]=Buffer[j];
- i--;
- }
- else Buffer[i]=tolower(c);
- }
+ str = g_utf8_strdown(Buffer, -1);
+ words = g_regex_split(re, str, 0);
+ g_free(str);
+ strcpy(Buffer, words[0]);
+ g_strfreev(words);
}
/* Correct a spelling
@@ -750,6 +713,8 @@
void Niall_Free(void)
{
ClearDictionary();
+ if (re)
+ g_regex_unref(re);
}
/*******************************************************************************/
--- gniall.orig/gnome.c
+++ gniall/gnome.c
@@ -280,6 +280,7 @@
GtkWidget *label;
GtkWidget *carea;
GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
+ gchar *orig, *corr;
dialogCorrect = gtk_dialog_new_with_buttons("Corrections",
GTK_WINDOW(windowMain),
@@ -309,8 +310,12 @@
if(gtk_dialog_run(GTK_DIALOG(dialogCorrect))==GTK_RESPONSE_ACCEPT)
{
- Niall_CorrectSpelling(gtk_entry_get_text(GTK_ENTRY(entryIncorrect)),
- gtk_entry_get_text(GTK_ENTRY(entryCorrected)));
+ /* StripPunctiation may modify strings, so pass on dupes. */
+ orig = g_strdup(gtk_entry_get_text(GTK_ENTRY(entryIncorrect)));
+ corr = g_strdup(gtk_entry_get_text(GTK_ENTRY(entryCorrected)));
+ Niall_CorrectSpelling(orig, corr);
+ g_free(orig);
+ g_free(corr);
wordsSaved=FALSE;
}
|