| 1 |
|
| |
| 2 |
|
| /*===========================================================================*/ |
| 3 |
|
| |
| 4 |
|
| /* |
| 5 |
|
| * Copyright (C) 1998 Jason Hutchens |
| 6 |
|
| * |
| 7 |
|
| * This program is free software; you can redistribute it and/or modify it |
| 8 |
|
| * under the terms of the GNU General Public License as published by the Free |
| 9 |
|
| * Software Foundation; either version 2 of the license or (at your option) |
| 10 |
|
| * any later version. |
| 11 |
|
| * |
| 12 |
|
| * This program is distributed in the hope that it will be useful, but |
| 13 |
|
| * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 |
|
| * or FITNESS FOR A PARTICULAR PURPOSE. See the Gnu Public License for more |
| 15 |
|
| * details. |
| 16 |
|
| * |
| 17 |
|
| * You should have received a copy of the GNU General Public License along |
| 18 |
|
| * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 |
|
| * 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 |
|
| */ |
| 21 |
|
| |
| 22 |
|
| /*===========================================================================*/ |
| 23 |
|
| |
| 24 |
|
| /* |
| 25 |
|
| * $Id: megahal.c,v 1.6 2002/10/16 04:32:53 davidw Exp $ |
| 26 |
|
| * |
| 27 |
|
| * File: megahal.c |
| 28 |
|
| * |
| 29 |
|
| * Program: MegaHAL |
| 30 |
|
| * |
| 31 |
|
| * Purpose: To simulate a natural language conversation with a psychotic |
| 32 |
|
| * computer. This is achieved by learning from the user's |
| 33 |
|
| * input using a third-order Markov model on the word level. |
| 34 |
|
| * Words are considered to be sequences of characters separated |
| 35 |
|
| * by whitespace and punctuation. Replies are generated |
| 36 |
|
| * randomly based on a keyword, and they are scored using |
| 37 |
|
| * measures of surprise. |
| 38 |
|
| * |
| 39 |
|
| * Author: Mr. Jason L. Hutchens (http://www.amristar.com.au/~hutch/) |
| 40 |
|
| * |
| 41 |
|
| * WWW: http://megahal.sourceforge.net |
| 42 |
|
| * |
| 43 |
|
| * Compilation Notes |
| 44 |
|
| * ================= |
| 45 |
|
| * |
| 46 |
|
| * When compiling, be sure to link with the maths library so that the |
| 47 |
|
| * log() function can be found. |
| 48 |
|
| * |
| 49 |
|
| * On the Macintosh, add the library SpeechLib to your project. It is |
| 50 |
|
| * very important that you set the attributes to Import Weak. You can |
| 51 |
|
| * do this by selecting the lib and then use Project Inspector from the |
| 52 |
|
| * Window menu. |
| 53 |
|
| * |
| 54 |
|
| * CREDITS |
| 55 |
|
| * ======= |
| 56 |
|
| * |
| 57 |
|
| * Amiga (AmigaOS) |
| 58 |
|
| * --------------- |
| 59 |
|
| * Dag Agren (dagren@ra.abo.fi) |
| 60 |
|
| * |
| 61 |
|
| * DEC (OSF) |
| 62 |
|
| * --------- |
| 63 |
|
| * Jason Hutchens (hutch@ciips.ee.uwa.edu.au) |
| 64 |
|
| * |
| 65 |
|
| * Macintosh |
| 66 |
|
| * --------- |
| 67 |
|
| * Paul Baxter (pbaxter@assistivetech.com) |
| 68 |
|
| * Doug Turner (dturner@best.com) |
| 69 |
|
| * |
| 70 |
|
| * PC (Linux) |
| 71 |
|
| * ---------- |
| 72 |
|
| * Jason Hutchens (hutch@ciips.ee.uwa.edu.au) |
| 73 |
|
| * |
| 74 |
|
| * PC (OS/2) |
| 75 |
|
| * --------- |
| 76 |
|
| * Bjorn Karlowsky (?) |
| 77 |
|
| * |
| 78 |
|
| * PC (Windows 3.11) |
| 79 |
|
| * ----------------- |
| 80 |
|
| * Jim Crawford (pfister_@hotmail.com) |
| 81 |
|
| * |
| 82 |
|
| * PC (Windows '95) |
| 83 |
|
| * ---------------- |
| 84 |
|
| * Jason Hutchens (hutch@ciips.ee.uwa.edu.au) |
| 85 |
|
| * |
| 86 |
|
| * PPC (Linux) |
| 87 |
|
| * ----------- |
| 88 |
|
| * Lucas Vergnettes (Lucasv@sdf.lonestar.org) |
| 89 |
|
| * |
| 90 |
|
| * SGI (Irix) |
| 91 |
|
| * ---------- |
| 92 |
|
| * Jason Hutchens (hutch@ciips.ee.uwa.edu.au) |
| 93 |
|
| * |
| 94 |
|
| * Sun (SunOS) |
| 95 |
|
| * ----------- |
| 96 |
|
| * Jason Hutchens (hutch@ciips.ee.uwa.edu.au) |
| 97 |
|
| */ |
| 98 |
|
| |
| 99 |
|
| /*===========================================================================*/ |
| 100 |
|
| |
| 101 |
|
| #include <stdio.h> |
| 102 |
|
| #include <stdlib.h> |
| 103 |
|
| #include <stdarg.h> |
| 104 |
|
| #ifndef _MSC_VER |
| 105 |
|
| #include <unistd.h> |
| 106 |
|
| #include <getopt.h> |
| 107 |
|
| #endif |
| 108 |
|
| #if !defined(AMIGA) && !defined(__mac_os) && !defined (__FreeBSD__) |
| 109 |
|
| // FreeBSD malloc.h is empty and gives error |
| 110 |
|
| // Tested on FreeBSD 5.4 |
| 111 |
|
| #include <malloc.h> |
| 112 |
|
| #endif |
| 113 |
|
| #include <string.h> |
| 114 |
|
| #include <signal.h> |
| 115 |
|
| #include <math.h> |
| 116 |
|
| #include <time.h> |
| 117 |
|
| #include <ctype.h> |
| 118 |
|
| #if defined(__mac_os) |
| 119 |
|
| #include <types.h> |
| 120 |
|
| #include <Speech.h> |
| 121 |
|
| #else |
| 122 |
|
| #include <sys/types.h> |
| 123 |
|
| #endif |
| 124 |
|
| #include "megahal.h" |
| 125 |
|
| #if defined(DEBUG) |
| 126 |
|
| #include "debug.h" |
| 127 |
|
| #endif |
| 128 |
|
| |
| 129 |
|
| #define P_THINK 40 |
| 130 |
|
| #define D_KEY 100000 |
| 131 |
|
| #define V_KEY 50000 |
| 132 |
|
| #define D_THINK 500000 |
| 133 |
|
| #define V_THINK 250000 |
| 134 |
|
| |
| 135 |
|
| #define MIN(a,b) ((a)<(b))?(a):(b) |
| 136 |
|
| |
| 137 |
|
| #define COOKIE "MegaHALv8" |
| 138 |
|
| #define TIMEOUT 1 |
| 139 |
|
| |
| 140 |
|
| #define DEFAULT "." |
| 141 |
|
| |
| 142 |
|
| #define COMMAND_SIZE (sizeof(command)/sizeof(command[0])) |
| 143 |
|
| |
| 144 |
|
| #define BYTE1 unsigned char |
| 145 |
|
| #define BYTE2 unsigned short |
| 146 |
|
| #define BYTE4 unsigned long |
| 147 |
|
| |
| 148 |
|
| #ifdef __mac_os |
| 149 |
|
| #define bool Boolean |
| 150 |
|
| #endif |
| 151 |
|
| |
| 152 |
|
| #ifdef DOS |
| 153 |
|
| #define SEP "\\" |
| 154 |
|
| #else |
| 155 |
|
| #define SEP "/" |
| 156 |
|
| #endif |
| 157 |
|
| |
| 158 |
|
| #ifdef AMIGA |
| 159 |
|
| #undef toupper |
| 160 |
|
| #define toupper(x) ToUpper(x) |
| 161 |
|
| #undef tolower |
| 162 |
|
| #define tolower(x) ToLower(x) |
| 163 |
|
| #undef isalpha |
| 164 |
|
| #define isalpha(x) IsAlpha(_AmigaLocale,x) |
| 165 |
|
| #undef isalnum |
| 166 |
|
| #define isalnum(x) IsAlNum(_AmigaLocale,x) |
| 167 |
|
| #undef isdigit |
| 168 |
|
| #define isdigit(x) IsDigit(_AmigaLocale,x) |
| 169 |
|
| #undef isspace |
| 170 |
|
| #define isspace(x) IsSpace(_AmigaLocale,x) |
| 171 |
|
| #endif |
| 172 |
|
| |
| 173 |
|
| #ifndef __mac_os |
| 174 |
|
| #undef FALSE |
| 175 |
|
| #undef TRUE |
| 176 |
|
| typedef enum { FALSE, TRUE } bool; |
| 177 |
|
| #endif |
| 178 |
|
| |
| 179 |
|
| typedef struct { |
| 180 |
|
| BYTE1 length; |
| 181 |
|
| char *word; |
| 182 |
|
| } STRING; |
| 183 |
|
| |
| 184 |
|
| typedef struct { |
| 185 |
|
| BYTE4 size; |
| 186 |
|
| STRING *entry; |
| 187 |
|
| BYTE2 *index; |
| 188 |
|
| } DICTIONARY; |
| 189 |
|
| |
| 190 |
|
| typedef struct { |
| 191 |
|
| BYTE2 size; |
| 192 |
|
| STRING *from; |
| 193 |
|
| STRING *to; |
| 194 |
|
| } SWAP; |
| 195 |
|
| |
| 196 |
|
| typedef struct NODE { |
| 197 |
|
| BYTE2 symbol; |
| 198 |
|
| BYTE4 usage; |
| 199 |
|
| BYTE2 count; |
| 200 |
|
| BYTE2 branch; |
| 201 |
|
| struct NODE **tree; |
| 202 |
|
| } TREE; |
| 203 |
|
| |
| 204 |
|
| typedef struct { |
| 205 |
|
| BYTE1 order; |
| 206 |
|
| TREE *forward; |
| 207 |
|
| TREE *backward; |
| 208 |
|
| TREE **context; |
| 209 |
|
| DICTIONARY *dictionary; |
| 210 |
|
| } MODEL; |
| 211 |
|
| |
| 212 |
|
| typedef enum { UNKNOWN, QUIT, EXIT, SAVE, DELAY, HELP, SPEECH, VOICELIST, VOICE, BRAIN, QUIET} COMMAND_WORDS; |
| 213 |
|
| |
| 214 |
|
| typedef struct { |
| 215 |
|
| STRING word; |
| 216 |
|
| char *helpstring; |
| 217 |
|
| COMMAND_WORDS command; |
| 218 |
|
| } COMMAND; |
| 219 |
|
| |
| 220 |
|
| /*===========================================================================*/ |
| 221 |
|
| |
| 222 |
|
| static int width=75; |
| 223 |
|
| static int order=5; |
| 224 |
|
| |
| 225 |
|
| static bool typing_delay=FALSE; |
| 226 |
|
| static bool noprompt=FALSE; |
| 227 |
|
| static bool speech=FALSE; |
| 228 |
|
| static bool quiet=FALSE; |
| 229 |
|
| static bool nowrap=FALSE; |
| 230 |
|
| static bool nobanner=FALSE; |
| 231 |
|
| |
| 232 |
|
| static char *errorfilename = "megahal.log"; |
| 233 |
|
| static char *statusfilename = "megahal.txt"; |
| 234 |
|
| static DICTIONARY *words=NULL; |
| 235 |
|
| static DICTIONARY *greets=NULL; |
| 236 |
|
| static MODEL *model=NULL; |
| 237 |
|
| |
| 238 |
|
| static FILE *errorfp; |
| 239 |
|
| static FILE *statusfp; |
| 240 |
|
| |
| 241 |
|
| static DICTIONARY *ban=NULL; |
| 242 |
|
| static DICTIONARY *aux=NULL; |
| 243 |
|
| static DICTIONARY *fin=NULL; |
| 244 |
|
| static DICTIONARY *grt=NULL; |
| 245 |
|
| static SWAP *swp=NULL; |
| 246 |
|
| static bool used_key; |
| 247 |
|
| static char *directory=NULL; |
| 248 |
|
| static char *last=NULL; |
| 249 |
|
| |
| 250 |
|
| static COMMAND command[] = { |
| 251 |
|
| { { 4, "QUIT" }, "quits the program and saves MegaHAL's brain", QUIT }, |
| 252 |
|
| { { 4, "EXIT" }, "exits the program *without* saving MegaHAL's brain", EXIT }, |
| 253 |
|
| { { 4, "SAVE" }, "saves the current MegaHAL brain", SAVE }, |
| 254 |
|
| { { 5, "DELAY" }, "toggles MegaHAL's typing delay (off by default)", DELAY }, |
| 255 |
|
| { { 6, "SPEECH" }, "toggles MegaHAL's speech (off by default)", SPEECH }, |
| 256 |
|
| { { 6, "VOICES" }, "list available voices for speech", VOICELIST }, |
| 257 |
|
| { { 5, "VOICE" }, "switches to voice specified", VOICE }, |
| 258 |
|
| { { 5, "BRAIN" }, "change to another MegaHAL personality", BRAIN }, |
| 259 |
|
| { { 4, "HELP" }, "displays this message", HELP }, |
| 260 |
|
| { { 5, "QUIET" }, "toggles MegaHAL's responses (on by default)",QUIET}, |
| 261 |
|
| /* |
| 262 |
|
| { { 5, "STATS" }, "Display stats", STATS}, |
| 263 |
|
| { { 5, "STATS-SESSION" }, "Display stats for this session only",STATS_SESSION}, |
| 264 |
|
| { { 5, "STATS-ALL" },"Display stats for the whole lifetime",STATS-ALL}, |
| 265 |
|
| */ |
| 266 |
|
| }; |
| 267 |
|
| |
| 268 |
|
| #ifdef AMIGA |
| 269 |
|
| struct Locale *_AmigaLocale; |
| 270 |
|
| #endif |
| 271 |
|
| |
| 272 |
|
| #ifdef __mac_os |
| 273 |
|
| Boolean gSpeechExists = false; |
| 274 |
|
| SpeechChannel gSpeechChannel = nil; |
| 275 |
|
| #endif |
| 276 |
|
| |
| 277 |
|
| /* FIXME - these need to be static */ |
| 278 |
|
| |
| 279 |
|
| static void add_aux(MODEL *, DICTIONARY *, STRING); |
| 280 |
|
| static void add_key(MODEL *, DICTIONARY *, STRING); |
| 281 |
|
| static void add_node(TREE *, TREE *, int); |
| 282 |
|
| static void add_swap(SWAP *, char *, char *); |
| 283 |
|
| static TREE *add_symbol(TREE *, BYTE2); |
| 284 |
|
| static BYTE2 add_word(DICTIONARY *, STRING); |
| 285 |
|
| static int babble(MODEL *, DICTIONARY *, DICTIONARY *); |
| 286 |
|
| static bool boundary(char *, int); |
| 287 |
|
| static void capitalize(char *); |
| 288 |
|
| static void changevoice(DICTIONARY *, int); |
| 289 |
|
| static void change_personality(DICTIONARY *, int, MODEL **); |
| 290 |
|
| static void delay(char *); |
| 291 |
|
| static void die(int); |
| 292 |
|
| static bool dissimilar(DICTIONARY *, DICTIONARY *); |
| 293 |
|
| static void error(char *, char *, ...); |
| 294 |
|
| static float evaluate_reply(MODEL *, DICTIONARY *, DICTIONARY *); |
| 295 |
|
| static COMMAND_WORDS execute_command(DICTIONARY *, int *); |
| 296 |
|
| static void exithal(void); |
| 297 |
|
| static TREE *find_symbol(TREE *, int); |
| 298 |
|
| static TREE *find_symbol_add(TREE *, int); |
| 299 |
|
| static BYTE2 find_word(DICTIONARY *, STRING); |
| 300 |
|
| static char *generate_reply(MODEL *, DICTIONARY *); |
| 301 |
|
| static void help(void); |
| 302 |
|
| static void ignore(int); |
| 303 |
|
| static bool initialize_error(char *); |
| 304 |
|
| #ifdef __mac_os |
| 305 |
|
| static bool initialize_speech(void); |
| 306 |
|
| #endif |
| 307 |
|
| static bool initialize_status(char *); |
| 308 |
|
| static void learn(MODEL *, DICTIONARY *); |
| 309 |
|
| static void listvoices(void); |
| 310 |
|
| static void make_greeting(DICTIONARY *); |
| 311 |
|
| static void make_words(char *, DICTIONARY *); |
| 312 |
|
| static DICTIONARY *new_dictionary(void); |
| 313 |
|
| |
| 314 |
|
| static char *read_input(char *); |
| 315 |
|
| static void save_model(char *, MODEL *); |
| 316 |
|
| #ifdef __mac_os |
| 317 |
|
| static char *strdup(const char *); |
| 318 |
|
| #endif |
| 319 |
|
| static void upper(char *); |
| 320 |
|
| static void write_input(char *); |
| 321 |
|
| static void write_output(char *); |
| 322 |
|
| #if defined(DOS) || defined(__mac_os) |
| 323 |
|
| static void usleep(int); |
| 324 |
|
| #endif |
| 325 |
|
| |
| 326 |
|
| #if defined(_MSC_VER) || defined(__MINGW32_VERSION) |
| 327 |
|
| #include <windows.h> |
| 328 |
|
| #define usleep(i) Sleep(i) |
| 329 |
|
| #endif |
| 330 |
|
| |
| 331 |
|
| |
| 332 |
|
| static char *format_output(char *); |
| 333 |
|
| static void free_dictionary(DICTIONARY *); |
| 334 |
|
| static void free_model(MODEL *); |
| 335 |
|
| static void free_tree(TREE *); |
| 336 |
|
| static void free_word(STRING); |
| 337 |
|
| static void free_words(DICTIONARY *); |
| 338 |
|
| static void initialize_context(MODEL *); |
| 339 |
|
| static void initialize_dictionary(DICTIONARY *); |
| 340 |
|
| static DICTIONARY *initialize_list(char *); |
| 341 |
|
| static SWAP *initialize_swap(char *); |
| 342 |
|
| static void load_dictionary(FILE *, DICTIONARY *); |
| 343 |
|
| static bool load_model(char *, MODEL *); |
| 344 |
|
| static void load_personality(MODEL **); |
| 345 |
|
| static void load_tree(FILE *, TREE *); |
| 346 |
|
| static void load_word(FILE *, DICTIONARY *); |
| 347 |
|
| static DICTIONARY *make_keywords(MODEL *, DICTIONARY *); |
| 348 |
|
| static char *make_output(DICTIONARY *); |
| 349 |
|
| static MODEL *new_model(int); |
| 350 |
|
| static TREE *new_node(void); |
| 351 |
|
| static SWAP *new_swap(void); |
| 352 |
|
| static bool print_header(FILE *); |
| 353 |
|
| static bool progress(char *, int, int); |
| 354 |
|
| static DICTIONARY *reply(MODEL *, DICTIONARY *); |
| 355 |
|
| static void save_dictionary(FILE *, DICTIONARY *); |
| 356 |
|
| static void save_tree(FILE *, TREE *); |
| 357 |
|
| static void save_word(FILE *, STRING); |
| 358 |
|
| static int search_dictionary(DICTIONARY *, STRING, bool *); |
| 359 |
|
| static int search_node(TREE *, int, bool *); |
| 360 |
|
| static int seed(MODEL *, DICTIONARY *); |
| 361 |
|
| static void show_dictionary(DICTIONARY *); |
| 362 |
|
| static void speak(char *); |
| 363 |
|
| static bool status(char *, ...); |
| 364 |
|
| static void train(MODEL *, char *); |
| 365 |
|
| static void typein(char); |
| 366 |
|
| static void update_context(MODEL *, int); |
| 367 |
|
| static void update_model(MODEL *, int); |
| 368 |
|
| static bool warn(char *, char *, ...); |
| 369 |
|
| static int wordcmp(STRING, STRING); |
| 370 |
|
| static bool word_exists(DICTIONARY *, STRING); |
| 371 |
|
| static int rnd(int); |
| 372 |
|
| |
| 373 |
|
| |
| 374 |
|
| /* Function: setnoprompt |
| 375 |
|
| |
| 376 |
|
| Purpose: Set noprompt variable. |
| 377 |
|
| |
| 378 |
|
| */ |
| 379 |
1 |
| void megahal_setnoprompt(void) |
| 380 |
|
| { |
| 381 |
1 |
| noprompt = TRUE; |
| 382 |
1 |
| } |
| 383 |
|
| |
| 384 |
1 |
| void megahal_setnowrap (void) |
| 385 |
|
| { |
| 386 |
1 |
| nowrap = TRUE; |
| 387 |
1 |
| } |
| 388 |
1 |
| void megahal_setnobanner (void) |
| 389 |
|
| { |
| 390 |
1 |
| nobanner = TRUE; |
| 391 |
1 |
| } |
| 392 |
|
| |
| 393 |
0 |
| void megahal_seterrorfile(char *filename) |
| 394 |
|
| { |
| 395 |
0 |
| errorfilename = filename; |
| 396 |
0 |
| } |
| 397 |
0 |
| void megahal_setstatusfile(char *filename) |
| 398 |
|
| { |
| 399 |
0 |
| statusfilename = filename; |
| 400 |
0 |
| } |
| 401 |
|
| |
| 402 |
|
| /* |
| 403 |
|
| megahal_initialize -- |
| 404 |
|
| |
| 405 |
|
| Initialize various brains and files. |
| 406 |
|
| |
| 407 |
|
| Results: |
| 408 |
|
| |
| 409 |
|
| None. |
| 410 |
|
| */ |
| 411 |
|
| |
| 412 |
1 |
| void megahal_initialize(void) |
| 413 |
|
| { |
| 414 |
1 |
| errorfp = stderr; |
| 415 |
1 |
| statusfp = stdout; |
| 416 |
|
| |
| 417 |
|
| // initialize_error(errorfilename); |
| 418 |
|
| // initialize_status(statusfilename); |
| 419 |
1 |
| ignore(0); |
| 420 |
|
| |
| 421 |
|
| #ifdef AMIGA |
| 422 |
|
| _AmigaLocale=OpenLocale(NULL); |
| 423 |
|
| #endif |
| 424 |
|
| #ifdef __mac_os |
| 425 |
|
| gSpeechExists = initialize_speech(); |
| 426 |
|
| #endif |
| 427 |
1 |
▶ | if(!nobanner) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!nobanner) |
0 |
1 |
1 |
|
| 428 |
0 |
| fprintf(stdout, |
| 429 |
|
| "+------------------------------------------------------------------------+\n" |
| 430 |
|
| "| |\n" |
| 431 |
|
| "| # # ###### #### ## # # ## # |\n" |
| 432 |
|
| "| ## ## # # # # # # # # # # ### |\n" |
| 433 |
|
| "| # ## # ##### # # # ###### # # # # # |\n" |
| 434 |
|
| "| # # # # ### ###### # # ###### # # # ### |\n" |
| 435 |
|
| "| # # # # # # # # # # # # # # # # |\n" |
| 436 |
|
| "| # # ###### #### # # # # # # ###### # ###r6 |\n" |
| 437 |
|
| "| |\n" |
| 438 |
|
| "| Copyright(C) 1998 Jason Hutchens |\n" |
| 439 |
|
| "+------------------------------------------------------------------------+\n" |
| 440 |
|
| ); |
| 441 |
|
| |
| 442 |
1 |
| words = new_dictionary(); |
| 443 |
1 |
| greets = new_dictionary(); |
| 444 |
1 |
| change_personality(NULL, 0, &model); |
| 445 |
1 |
| } |
| 446 |
|
| |
| 447 |
|
| /* |
| 448 |
|
| megahal_do_reply -- |
| 449 |
|
| |
| 450 |
|
| Take string as input, and return allocated string as output. The |
| 451 |
|
| user is responsible for freeing this memory. |
| 452 |
|
| |
| 453 |
|
| */ |
| 454 |
|
| |
| 455 |
1 |
| char *megahal_do_reply(char *input, int log) |
| 456 |
|
| { |
| 457 |
1 |
| char *output = NULL; |
| 458 |
|
| |
| 459 |
1 |
▶ | if (log != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (log != 0) |
0 |
1 |
1 |
|
| 460 |
0 |
| write_input(input); /* log input if so desired */ |
| 461 |
|
| |
| 462 |
1 |
| upper(input); |
| 463 |
|
| |
| 464 |
1 |
| make_words(input, words); |
| 465 |
|
| |
| 466 |
1 |
| learn(model, words); |
| 467 |
1 |
| output = generate_reply(model, words); |
| 468 |
1 |
| capitalize(output); |
| 469 |
1 |
| return output; |
| 470 |
|
| } |
| 471 |
|
| |
| 472 |
|
| /* |
| 473 |
|
| megahal_initial_greeting -- |
| 474 |
|
| |
| 475 |
|
| This function returns an initial greeting. It can be used to start |
| 476 |
|
| Megahal conversations, but it isn't necessary. |
| 477 |
|
| |
| 478 |
|
| */ |
| 479 |
|
| |
| 480 |
0 |
| char *megahal_initial_greeting(void) |
| 481 |
|
| { |
| 482 |
|
| char *output; |
| 483 |
|
| |
| 484 |
0 |
| make_greeting(greets); |
| 485 |
0 |
| output = generate_reply(model, greets); |
| 486 |
0 |
| return output; |
| 487 |
|
| } |
| 488 |
|
| |
| 489 |
|
| /* |
| 490 |
|
| megahal_output -- |
| 491 |
|
| |
| 492 |
|
| This function pretty prints output. |
| 493 |
|
| |
| 494 |
|
| Wrapper function to have things in the right namespace. |
| 495 |
|
| |
| 496 |
|
| */ |
| 497 |
|
| |
| 498 |
0 |
| void megahal_output(char *output) |
| 499 |
|
| { |
| 500 |
0 |
▶ | if(!quiet) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!quiet) |
0 |
0 |
0 |
|
| 501 |
0 |
| write_output(output); |
| 502 |
0 |
| } |
| 503 |
|
| |
| 504 |
|
| /* |
| 505 |
|
| megahal_input -- |
| 506 |
|
| |
| 507 |
|
| Get a string from stdin, using a prompt. |
| 508 |
|
| |
| 509 |
|
| */ |
| 510 |
|
| |
| 511 |
0 |
| char *megahal_input(char *prompt) |
| 512 |
|
| { |
| 513 |
0 |
▶ | if (noprompt) |
Branch: 1 branch
| Branch | True | False | Total |
| if (noprompt) |
0 |
0 |
0 |
|
| 514 |
0 |
| return read_input(""); |
| 515 |
|
| else |
| 516 |
0 |
| return read_input(prompt); |
| 517 |
|
| } |
| 518 |
|
| |
| 519 |
|
| /* |
| 520 |
|
| megahal_command -- |
| 521 |
|
| |
| 522 |
|
| Check to see if input is a megahal command, and if so, act upon it. |
| 523 |
|
| |
| 524 |
|
| Returns 1 if it is a command, 0 if it is not. |
| 525 |
|
| |
| 526 |
|
| */ |
| 527 |
|
| |
| 528 |
0 |
| int megahal_command(char *input) |
| 529 |
|
| { |
| 530 |
0 |
| int position = 0; |
| 531 |
|
| char *output; |
| 532 |
|
| |
| 533 |
0 |
| make_words(input,words); |
| 534 |
0 |
| switch(execute_command(words, &position)) { |
| 535 |
0 |
| case EXIT: |
| 536 |
0 |
| exithal(); |
| 537 |
0 |
| break; |
| 538 |
0 |
| case QUIT: |
| 539 |
0 |
| save_model("megahal.brn", model); |
| 540 |
0 |
| exithal(); |
| 541 |
0 |
| break; |
| 542 |
0 |
| case SAVE: |
| 543 |
0 |
| save_model("megahal.brn", model); |
| 544 |
0 |
| break; |
| 545 |
0 |
| case DELAY: |
| 546 |
0 |
| typing_delay=!typing_delay; |
| 547 |
0 |
▶ | printf("MegaHAL typing is now %s.\n", typing_delay?"on":"off"); |
Branch: 1 branch
| Branch | True | False | Total |
| printf("MegaHAL typing is now %s.\n", typing_delay?"on":"off"); |
0 |
0 |
0 |
|
| 548 |
0 |
| return 1; |
| 549 |
0 |
| case SPEECH: |
| 550 |
0 |
| speech=!speech; |
| 551 |
0 |
▶ | printf("MegaHAL speech is now %s.\n", speech?"on":"off"); |
Branch: 1 branch
| Branch | True | False | Total |
| printf("MegaHAL speech is now %s.\n", speech?"on":"off"); |
0 |
0 |
0 |
|
| 552 |
0 |
| return 1; |
| 553 |
0 |
| case HELP: |
| 554 |
0 |
| help(); |
| 555 |
0 |
| return 1; |
| 556 |
0 |
| case VOICELIST: |
| 557 |
0 |
| listvoices(); |
| 558 |
0 |
| return 1; |
| 559 |
0 |
| case VOICE: |
| 560 |
0 |
| changevoice(words, position); |
| 561 |
0 |
| return 1; |
| 562 |
0 |
| case BRAIN: |
| 563 |
0 |
| change_personality(words, position, &model); |
| 564 |
0 |
| make_greeting(greets); |
| 565 |
0 |
| output=generate_reply(model, greets); |
| 566 |
0 |
| write_output(output); |
| 567 |
0 |
| return 1; |
| 568 |
0 |
| case QUIET: |
| 569 |
0 |
| quiet=!quiet; |
| 570 |
0 |
| return 1; |
| 571 |
0 |
| default: |
| 572 |
0 |
| return 0; |
| 573 |
|
| } |
| 574 |
0 |
| return 0; |
| 575 |
|
| } |
| 576 |
|
| |
| 577 |
|
| /* |
| 578 |
|
| megahal_cleanup -- |
| 579 |
|
| |
| 580 |
|
| Clean up everything. Prepare for exit. |
| 581 |
|
| |
| 582 |
|
| */ |
| 583 |
|
| |
| 584 |
1 |
| void megahal_cleanup(void) |
| 585 |
|
| { |
| 586 |
1 |
| save_model("megahal.brn", model); |
| 587 |
|
| |
| 588 |
|
| #ifdef AMIGA |
| 589 |
|
| CloseLocale(_AmigaLocale); |
| 590 |
|
| #endif |
| 591 |
1 |
| } |
| 592 |
|
| |
| 593 |
|
| |
| 594 |
|
| |
| 595 |
|
| /*---------------------------------------------------------------------------*/ |
| 596 |
|
| |
| 597 |
|
| /* |
| 598 |
|
| * Function: Execute_Command |
| 599 |
|
| * |
| 600 |
|
| * Purpose: Detect whether the user has typed a command, and |
| 601 |
|
| * execute the corresponding function. |
| 602 |
|
| */ |
| 603 |
0 |
| COMMAND_WORDS execute_command(DICTIONARY *words, int *position) |
| 604 |
|
| { |
| 605 |
|
| register int i; |
| 606 |
|
| register int j; |
| 607 |
|
| |
| 608 |
|
| /* |
| 609 |
|
| * If there is only one word, then it can't be a command. |
| 610 |
|
| */ |
| 611 |
0 |
| *position=words->size+1; |
| 612 |
0 |
▶ | if(words->size<=1) return(UNKNOWN); |
Branch: 1 branch
| Branch | True | False | Total |
| if(words->size<=1) return(UNKNOWN); |
0 |
0 |
0 |
|
| 613 |
|
| |
| 614 |
|
| /* |
| 615 |
|
| * Search through the word array. If a command prefix is found, |
| 616 |
|
| * then try to match the following word with a command word. If |
| 617 |
|
| * a match is found, then return a command identifier. If the |
| 618 |
|
| * Following word is a number, then change the judge. Otherwise, |
| 619 |
|
| * continue the search. |
| 620 |
|
| */ |
| 621 |
0 |
▶ | for(i=0; i<words->size-1; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<words->size-1; ++i) |
0 |
0 |
0 |
|
| 622 |
|
| /* |
| 623 |
|
| * The command prefix was found. |
| 624 |
|
| */ |
| 625 |
0 |
▶ | if(words->entry[i].word[words->entry[i].length - 1] == '#') { |
Branch: 1 branch
| Branch | True | False | Total |
| if(words->entry[i].word[words->entry[i].length - 1] == '#') { |
0 |
0 |
0 |
|
| 626 |
|
| /* |
| 627 |
|
| * Look for a command word. |
| 628 |
|
| */ |
| 629 |
0 |
▶ | for(j = 0; j < COMMAND_SIZE; ++j) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j = 0; j < COMMAND_SIZE; ++j) |
0 |
0 |
0 |
|
| 630 |
0 |
▶ | if(wordcmp(command[j].word, words->entry[i + 1]) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(wordcmp(command[j].word, words->entry[i + 1]) == 0) { |
0 |
0 |
0 |
|
| 631 |
0 |
| *position = i + 1; |
| 632 |
0 |
| return(command[j].command); |
| 633 |
|
| } |
| 634 |
|
| } |
| 635 |
|
| |
| 636 |
0 |
| return(UNKNOWN); |
| 637 |
|
| } |
| 638 |
|
| |
| 639 |
|
| /*---------------------------------------------------------------------------*/ |
| 640 |
|
| |
| 641 |
|
| /* |
| 642 |
|
| * Function: ExitHAL |
| 643 |
|
| * |
| 644 |
|
| * Purpose: Terminate the program. |
| 645 |
|
| */ |
| 646 |
0 |
| void exithal(void) |
| 647 |
|
| { |
| 648 |
|
| #ifdef __mac_os |
| 649 |
|
| /* |
| 650 |
|
| * Must be called because it does use some system memory |
| 651 |
|
| */ |
| 652 |
|
| if (gSpeechChannel) { |
| 653 |
|
| StopSpeech(gSpeechChannel); |
| 654 |
|
| DisposeSpeechChannel(gSpeechChannel); |
| 655 |
|
| gSpeechChannel = nil; |
| 656 |
|
| } |
| 657 |
|
| #endif |
| 658 |
|
| |
| 659 |
0 |
| exit(0); |
| 660 |
|
| } |
| 661 |
|
| |
| 662 |
|
| /*---------------------------------------------------------------------------*/ |
| 663 |
|
| |
| 664 |
|
| /* |
| 665 |
|
| * Function: Read_Input |
| 666 |
|
| * |
| 667 |
|
| * Purpose: Read an input string from the user. |
| 668 |
|
| */ |
| 669 |
0 |
| char *read_input(char *prompt) |
| 670 |
|
| { |
| 671 |
|
| static char *input=NULL; |
| 672 |
|
| bool finish; |
| 673 |
|
| int length; |
| 674 |
|
| int c; |
| 675 |
|
| |
| 676 |
|
| /* |
| 677 |
|
| * Perform some initializations. The finish boolean variable is used |
| 678 |
|
| * to detect a double line-feed, while length contains the number of |
| 679 |
|
| * characters in the input string. |
| 680 |
|
| */ |
| 681 |
0 |
| finish=FALSE; |
| 682 |
0 |
| length=0; |
| 683 |
0 |
▶ | if(input==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(input==NULL) { |
0 |
0 |
0 |
|
| 684 |
0 |
| input=(char *)malloc(sizeof(char)); |
| 685 |
0 |
▶ | if(input==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(input==NULL) { |
0 |
0 |
0 |
|
| 686 |
0 |
| error("read_input", "Unable to allocate the input string"); |
| 687 |
0 |
| return(input); |
| 688 |
|
| } |
| 689 |
|
| } |
| 690 |
|
| |
| 691 |
|
| /* |
| 692 |
|
| * Display the prompt to the user. |
| 693 |
|
| */ |
| 694 |
0 |
| fprintf(stdout, prompt); |
| 695 |
0 |
| fflush(stdout); |
| 696 |
|
| |
| 697 |
|
| /* |
| 698 |
|
| * Loop forever, reading characters and putting them into the input |
| 699 |
|
| * string. |
| 700 |
|
| */ |
| 701 |
|
| while(TRUE) { |
| 702 |
|
| |
| 703 |
|
| /* |
| 704 |
|
| * Read a single character from stdin. |
| 705 |
|
| */ |
| 706 |
0 |
| c=getc(stdin); |
| 707 |
|
| |
| 708 |
|
| /* |
| 709 |
|
| * If the character is a line-feed, then set the finish variable |
| 710 |
|
| * to TRUE. If it already is TRUE, then this is a double line-feed, |
| 711 |
|
| * in which case we should exit. After a line-feed, display the |
| 712 |
|
| * prompt again, and set the character to the space character, as |
| 713 |
|
| * we don't permit linefeeds to appear in the input. |
| 714 |
|
| */ |
| 715 |
0 |
▶ | if((char)(c)=='\n') { |
Branch: 1 branch
| Branch | True | False | Total |
| if((char)(c)=='\n') { |
0 |
0 |
0 |
|
| 716 |
0 |
▶ | if(finish==TRUE) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if(finish==TRUE) break; |
0 |
0 |
0 |
|
| 717 |
0 |
| fprintf(stdout, prompt); |
| 718 |
0 |
| fflush(stdout); |
| 719 |
0 |
| finish=TRUE; |
| 720 |
0 |
| c=32; |
| 721 |
|
| } else { |
| 722 |
0 |
| finish=FALSE; |
| 723 |
|
| } |
| 724 |
|
| |
| 725 |
|
| /* |
| 726 |
|
| * Re-allocate the input string so that it can hold one more |
| 727 |
|
| * character. |
| 728 |
|
| */ |
| 729 |
0 |
| ++length; |
| 730 |
0 |
| input=(char *)realloc((char *)input,sizeof(char)*(length+1)); |
| 731 |
0 |
▶ | if(input==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(input==NULL) { |
0 |
0 |
0 |
|
| 732 |
0 |
| error("read_input", "Unable to re-allocate the input string"); |
| 733 |
0 |
| return(NULL); |
| 734 |
|
| } |
| 735 |
|
| |
| 736 |
|
| /* |
| 737 |
|
| * Add the character just read to the input string. |
| 738 |
|
| */ |
| 739 |
0 |
| input[length-1]=(char)c; |
| 740 |
0 |
| input[length]='\0'; |
| 741 |
|
| } |
| 742 |
|
| |
| 743 |
0 |
▶ | while(isspace(input[length-1])) --length; |
Branch: 1 branch
| Branch | True | False | Total |
| while(isspace(input[length-1])) --length; |
0 |
0 |
0 |
|
| 744 |
0 |
| input[length]='\0'; |
| 745 |
|
| |
| 746 |
|
| /* |
| 747 |
|
| * We have finished, so return the input string. |
| 748 |
|
| */ |
| 749 |
0 |
| return(input); |
| 750 |
|
| } |
| 751 |
|
| |
| 752 |
|
| /*---------------------------------------------------------------------------*/ |
| 753 |
|
| |
| 754 |
|
| /* |
| 755 |
|
| * Function: Initialize_Error |
| 756 |
|
| * |
| 757 |
|
| * Purpose: Close the current error file pointer, and open a new one. |
| 758 |
|
| */ |
| 759 |
0 |
| bool initialize_error(char *filename) |
| 760 |
|
| { |
| 761 |
0 |
▶ | if(errorfp!=stderr) fclose(errorfp); |
Branch: 1 branch
| Branch | True | False | Total |
| if(errorfp!=stderr) fclose(errorfp); |
0 |
0 |
0 |
|
| 762 |
|
| |
| 763 |
0 |
▶ | if(filename==NULL) return(TRUE); |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) return(TRUE); |
0 |
0 |
0 |
|
| 764 |
|
| |
| 765 |
0 |
| errorfp = fopen(filename, "a"); |
| 766 |
0 |
▶ | if(errorfp==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(errorfp==NULL) { |
0 |
0 |
0 |
|
| 767 |
0 |
| errorfp=stderr; |
| 768 |
0 |
| return(FALSE); |
| 769 |
|
| } |
| 770 |
0 |
| return(print_header(errorfp)); |
| 771 |
|
| } |
| 772 |
|
| |
| 773 |
|
| /*---------------------------------------------------------------------------*/ |
| 774 |
|
| |
| 775 |
|
| /* |
| 776 |
|
| * Function: Error |
| 777 |
|
| * |
| 778 |
|
| * Purpose: Print the specified message to the error file. |
| 779 |
|
| */ |
| 780 |
0 |
| void error(char *title, char *fmt, ...) |
| 781 |
|
| { |
| 782 |
|
| va_list argp; |
| 783 |
|
| |
| 784 |
0 |
| fprintf(errorfp, "%s: ", title); |
| 785 |
0 |
| va_start(argp, fmt); |
| 786 |
0 |
| vfprintf(errorfp, fmt, argp); |
| 787 |
0 |
| va_end(argp); |
| 788 |
0 |
| fprintf(errorfp, ".\n"); |
| 789 |
0 |
| fflush(errorfp); |
| 790 |
|
| |
| 791 |
|
| // fprintf(stderr, "MegaHAL died for some reason; check the error log.\n"); |
| 792 |
|
| |
| 793 |
0 |
| exit(1); |
| 794 |
|
| } |
| 795 |
|
| |
| 796 |
|
| /*---------------------------------------------------------------------------*/ |
| 797 |
|
| |
| 798 |
1 |
| bool warn(char *title, char *fmt, ...) |
| 799 |
|
| { |
| 800 |
|
| va_list argp; |
| 801 |
|
| |
| 802 |
1 |
| fprintf(errorfp, "%s: ", title); |
| 803 |
1 |
| va_start(argp, fmt); |
| 804 |
1 |
| vfprintf(errorfp, fmt, argp); |
| 805 |
1 |
| va_end(argp); |
| 806 |
1 |
| fprintf(errorfp, ".\n"); |
| 807 |
1 |
| fflush(errorfp); |
| 808 |
|
| |
| 809 |
|
| // fprintf(stderr, "MegaHAL emitted a warning; check the error log.\n"); |
| 810 |
|
| |
| 811 |
1 |
| return(TRUE); |
| 812 |
|
| } |
| 813 |
|
| |
| 814 |
|
| /*---------------------------------------------------------------------------*/ |
| 815 |
|
| |
| 816 |
|
| /* |
| 817 |
|
| * Function: Initialize_Status |
| 818 |
|
| * |
| 819 |
|
| * Purpose: Close the current status file pointer, and open a new one. |
| 820 |
|
| */ |
| 821 |
0 |
| bool initialize_status(char *filename) |
| 822 |
|
| { |
| 823 |
0 |
▶ | if(statusfp!=stdout) fclose(statusfp); |
Branch: 1 branch
| Branch | True | False | Total |
| if(statusfp!=stdout) fclose(statusfp); |
0 |
0 |
0 |
|
| 824 |
0 |
▶ | if(filename==NULL) return(FALSE); |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) return(FALSE); |
0 |
0 |
0 |
|
| 825 |
0 |
| statusfp=fopen(filename, "a"); |
| 826 |
0 |
▶ | if(statusfp==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(statusfp==NULL) { |
0 |
0 |
0 |
|
| 827 |
0 |
| statusfp=stdout; |
| 828 |
0 |
| return(FALSE); |
| 829 |
|
| } |
| 830 |
0 |
| return(print_header(statusfp)); |
| 831 |
|
| } |
| 832 |
|
| |
| 833 |
|
| /*---------------------------------------------------------------------------*/ |
| 834 |
|
| |
| 835 |
|
| /* |
| 836 |
|
| * Function: Status |
| 837 |
|
| * |
| 838 |
|
| * Purpose: Print the specified message to the status file. |
| 839 |
|
| */ |
| 840 |
0 |
| bool status(char *fmt, ...) |
| 841 |
|
| { |
| 842 |
|
| va_list argp; |
| 843 |
|
| |
| 844 |
0 |
| va_start(argp, fmt); |
| 845 |
0 |
| vfprintf(statusfp, fmt, argp); |
| 846 |
0 |
| va_end(argp); |
| 847 |
0 |
| fflush(statusfp); |
| 848 |
|
| |
| 849 |
0 |
| return(TRUE); |
| 850 |
|
| } |
| 851 |
|
| |
| 852 |
|
| /*---------------------------------------------------------------------------*/ |
| 853 |
|
| |
| 854 |
|
| /* |
| 855 |
|
| * Function: Print_Header |
| 856 |
|
| * |
| 857 |
|
| * Purpose: Display a copyright message and timestamp. |
| 858 |
|
| */ |
| 859 |
0 |
| bool print_header(FILE *file) |
| 860 |
|
| { |
| 861 |
|
| time_t clock; |
| 862 |
|
| char timestamp[1024]; |
| 863 |
|
| struct tm *local; |
| 864 |
|
| |
| 865 |
0 |
| clock=time(NULL); |
| 866 |
0 |
| local=localtime(&clock); |
| 867 |
0 |
| strftime(timestamp, 1024, "Start at: [%Y/%m/%d %H:%M:%S]\n", local); |
| 868 |
|
| |
| 869 |
0 |
| fprintf(file, "MegaHALv8\n"); |
| 870 |
0 |
| fprintf(file, "Copyright (C) 1998 Jason Hutchens\n"); |
| 871 |
0 |
| fprintf(file, timestamp); |
| 872 |
0 |
| fflush(file); |
| 873 |
|
| |
| 874 |
0 |
| return(TRUE); |
| 875 |
|
| } |
| 876 |
|
| |
| 877 |
|
| /*---------------------------------------------------------------------------*/ |
| 878 |
|
| |
| 879 |
|
| /* |
| 880 |
|
| * Function: Write_Output |
| 881 |
|
| * |
| 882 |
|
| * Purpose: Display the output string. |
| 883 |
|
| */ |
| 884 |
0 |
| void write_output(char *output) |
| 885 |
|
| { |
| 886 |
|
| char *formatted; |
| 887 |
|
| char *bit; |
| 888 |
|
| |
| 889 |
0 |
| capitalize(output); |
| 890 |
0 |
| speak(output); |
| 891 |
|
| |
| 892 |
0 |
| width=75; |
| 893 |
0 |
| formatted=format_output(output); |
| 894 |
0 |
| delay(formatted); |
| 895 |
0 |
| width=64; |
| 896 |
0 |
| formatted=format_output(output); |
| 897 |
|
| |
| 898 |
0 |
| bit=strtok(formatted, "\n"); |
| 899 |
0 |
▶ | if(bit==NULL) (void)status("MegaHAL: %s\n", formatted); |
Branch: 1 branch
| Branch | True | False | Total |
| if(bit==NULL) (void)status("MegaHAL: %s\n", formatted); |
0 |
0 |
0 |
|
| 900 |
0 |
▶ | while(bit!=NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(bit!=NULL) { |
0 |
0 |
0 |
|
| 901 |
0 |
| (void)status("MegaHAL: %s\n", bit); |
| 902 |
0 |
| bit=strtok(NULL, "\n"); |
| 903 |
|
| } |
| 904 |
0 |
| } |
| 905 |
|
| |
| 906 |
|
| /*---------------------------------------------------------------------------*/ |
| 907 |
|
| |
| 908 |
|
| /* |
| 909 |
|
| * Function: Capitalize |
| 910 |
|
| * |
| 911 |
|
| * Purpose: Convert a string to look nice. |
| 912 |
|
| */ |
| 913 |
1 |
| void capitalize(char *string) |
| 914 |
|
| { |
| 915 |
|
| register int i; |
| 916 |
1 |
| bool start=TRUE; |
| 917 |
|
| |
| 918 |
49 |
▶ | for(i=0; i<(int)strlen(string); ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<(int)strlen(string); ++i) { |
48 |
1 |
49 |
|
| 919 |
48 |
▶ | if(isalpha(string[i])) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(isalpha(string[i])) { |
37 |
11 |
48 |
|
| 920 |
37 |
▶ | if(start==TRUE) string[i]=(char)toupper((int)string[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| if(start==TRUE) string[i]=(char)toupper((int)string[i]); |
1 |
36 |
37 |
|
| 921 |
36 |
| else string[i]=(char)tolower((int)string[i]); |
| 922 |
37 |
| start=FALSE; |
| 923 |
|
| } |
| 924 |
48 |
▶ | if((i>2)&&(strchr("!.?", string[i-1])!=NULL)&&(isspace(string[i]))) |
Branch: 3 branches
| Branch | True | False | Total |
| if((i>2)&&(strchr("!.?", string[i-1])!=NULL)&&(isspace(string[i]))) |
45 |
3 |
48 |
| if((i>2)&&(strchr("!.?", string[i-1])!=NULL)&&(isspace(string[i]))) |
0 |
45 |
45 |
| if((i>2)&&(strchr("!.?", string[i-1])!=NULL)&&(isspace(string[i]))) |
0 |
0 |
0 |
|
| 925 |
0 |
| start=TRUE; |
| 926 |
|
| } |
| 927 |
1 |
| } |
| 928 |
|
| |
| 929 |
|
| /*---------------------------------------------------------------------------*/ |
| 930 |
|
| |
| 931 |
|
| /* |
| 932 |
|
| * Function: Upper |
| 933 |
|
| * |
| 934 |
|
| * Purpose: Convert a string to its uppercase representation. |
| 935 |
|
| */ |
| 936 |
350 |
| void upper(char *string) |
| 937 |
|
| { |
| 938 |
|
| register int i; |
| 939 |
|
| |
| 940 |
20117 |
▶ | for(i=0; i<(int)strlen(string); ++i) string[i]=(char)toupper((int)string[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<(int)strlen(string); ++i) string[i]=(char)toupper((int)string[i]); |
19767 |
350 |
20117 |
|
| 941 |
350 |
| } |
| 942 |
|
| |
| 943 |
|
| /*---------------------------------------------------------------------------*/ |
| 944 |
|
| |
| 945 |
|
| /* |
| 946 |
|
| * Function: Write_Input |
| 947 |
|
| * |
| 948 |
|
| * Purpose: Log the user's input |
| 949 |
|
| */ |
| 950 |
0 |
| void write_input(char *input) |
| 951 |
|
| { |
| 952 |
|
| char *formatted; |
| 953 |
|
| char *bit; |
| 954 |
|
| |
| 955 |
0 |
| width=64; |
| 956 |
0 |
| formatted=format_output(input); |
| 957 |
|
| |
| 958 |
0 |
| bit=strtok(formatted, "\n"); |
| 959 |
0 |
▶ | if(bit==NULL) (void)status("User: %s\n", formatted); |
Branch: 1 branch
| Branch | True | False | Total |
| if(bit==NULL) (void)status("User: %s\n", formatted); |
0 |
0 |
0 |
|
| 960 |
0 |
▶ | while(bit!=NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(bit!=NULL) { |
0 |
0 |
0 |
|
| 961 |
0 |
| (void)status("User: %s\n", bit); |
| 962 |
0 |
| bit=strtok(NULL, "\n"); |
| 963 |
|
| } |
| 964 |
0 |
| } |
| 965 |
|
| |
| 966 |
|
| /*---------------------------------------------------------------------------*/ |
| 967 |
|
| |
| 968 |
|
| /* |
| 969 |
|
| * Function: Format_Output |
| 970 |
|
| * |
| 971 |
|
| * Purpose: Format a string to display nicely on a terminal of a given |
| 972 |
|
| * width. |
| 973 |
|
| */ |
| 974 |
0 |
| static char *format_output(char *output) |
| 975 |
|
| { |
| 976 |
|
| static char *formatted=NULL; |
| 977 |
|
| register int i,j,c; |
| 978 |
|
| int l; |
| 979 |
0 |
▶ | if(formatted==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(formatted==NULL) { |
0 |
0 |
0 |
|
| 980 |
0 |
| formatted=(char *)malloc(sizeof(char)); |
| 981 |
0 |
▶ | if(formatted==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(formatted==NULL) { |
0 |
0 |
0 |
|
| 982 |
0 |
| error("format_output", "Unable to allocate formatted"); |
| 983 |
0 |
| return("ERROR"); |
| 984 |
|
| } |
| 985 |
|
| } |
| 986 |
|
| |
| 987 |
0 |
| formatted=(char *)realloc((char *)formatted, sizeof(char)*(strlen(output)+2)); |
| 988 |
0 |
▶ | if(formatted==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(formatted==NULL) { |
0 |
0 |
0 |
|
| 989 |
0 |
| error("format_output", "Unable to re-allocate formatted"); |
| 990 |
0 |
| return("ERROR"); |
| 991 |
|
| } |
| 992 |
|
| |
| 993 |
0 |
| l=0; |
| 994 |
0 |
| j=0; |
| 995 |
0 |
▶ | for(i=0; i<(int)strlen(output); ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<(int)strlen(output); ++i) { |
0 |
0 |
0 |
|
| 996 |
0 |
▶ | if((l==0)&&(isspace(output[i]))) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if((l==0)&&(isspace(output[i]))) continue; |
0 |
0 |
0 |
| if((l==0)&&(isspace(output[i]))) continue; |
0 |
0 |
0 |
|
| 997 |
0 |
| formatted[j]=output[i]; |
| 998 |
0 |
| ++j; |
| 999 |
0 |
| ++l; |
| 1000 |
0 |
▶ | if(!nowrap) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!nowrap) |
0 |
0 |
0 |
|
| 1001 |
0 |
▶ | if(l>=width) |
Branch: 1 branch
| Branch | True | False | Total |
| if(l>=width) |
0 |
0 |
0 |
|
| 1002 |
0 |
▶ | for(c=j-1; c>0; --c) |
Branch: 1 branch
| Branch | True | False | Total |
| for(c=j-1; c>0; --c) |
0 |
0 |
0 |
|
| 1003 |
0 |
▶ | if(formatted[c]==' ') { |
Branch: 1 branch
| Branch | True | False | Total |
| if(formatted[c]==' ') { |
0 |
0 |
0 |
|
| 1004 |
0 |
| formatted[c]='\n'; |
| 1005 |
0 |
| l=j-c-1; |
| 1006 |
0 |
| break; |
| 1007 |
|
| } |
| 1008 |
|
| } |
| 1009 |
0 |
▶ | if((j>0)&&(formatted[j-1]!='\n')) { |
Branch: 2 branches
| Branch | True | False | Total |
| if((j>0)&&(formatted[j-1]!='\n')) { |
0 |
0 |
0 |
| if((j>0)&&(formatted[j-1]!='\n')) { |
0 |
0 |
0 |
|
| 1010 |
0 |
| formatted[j]='\n'; |
| 1011 |
0 |
| ++j; |
| 1012 |
|
| } |
| 1013 |
0 |
| formatted[j]='\0'; |
| 1014 |
|
| |
| 1015 |
0 |
| return(formatted); |
| 1016 |
|
| } |
| 1017 |
|
| |
| 1018 |
|
| /*---------------------------------------------------------------------------*/ |
| 1019 |
|
| |
| 1020 |
|
| /* |
| 1021 |
|
| * Function: Add_Word |
| 1022 |
|
| * |
| 1023 |
|
| * Purpose: Add a word to a dictionary, and return the identifier |
| 1024 |
|
| * assigned to the word. If the word already exists in |
| 1025 |
|
| * the dictionary, then return its current identifier |
| 1026 |
|
| * without adding it again. |
| 1027 |
|
| */ |
| 1028 |
7281 |
| BYTE2 add_word(DICTIONARY *dictionary, STRING word) |
| 1029 |
|
| { |
| 1030 |
|
| register int i; |
| 1031 |
|
| int position; |
| 1032 |
|
| bool found; |
| 1033 |
|
| |
| 1034 |
|
| /* |
| 1035 |
|
| * If the word's already in the dictionary, there is no need to add it |
| 1036 |
|
| */ |
| 1037 |
7281 |
| position=search_dictionary(dictionary, word, &found); |
| 1038 |
7281 |
▶ | if(found==TRUE) goto succeed; |
Branch: 1 branch
| Branch | True | False | Total |
| if(found==TRUE) goto succeed; |
5956 |
1325 |
7281 |
|
| 1039 |
|
| |
| 1040 |
|
| /* |
| 1041 |
|
| * Increase the number of words in the dictionary |
| 1042 |
|
| */ |
| 1043 |
1325 |
| dictionary->size+=1; |
| 1044 |
|
| |
| 1045 |
|
| /* |
| 1046 |
|
| * Allocate one more entry for the word index |
| 1047 |
|
| */ |
| 1048 |
1325 |
▶ | if(dictionary->index==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(dictionary->index==NULL) { |
2 |
1323 |
1325 |
|
| 1049 |
2 |
| dictionary->index=(BYTE2 *)malloc(sizeof(BYTE2)* |
| 1050 |
2 |
| (dictionary->size)); |
| 1051 |
|
| } else { |
| 1052 |
1323 |
| dictionary->index=(BYTE2 *)realloc((BYTE2 *) |
| 1053 |
1323 |
| (dictionary->index),sizeof(BYTE2)*(dictionary->size)); |
| 1054 |
|
| } |
| 1055 |
1325 |
▶ | if(dictionary->index==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(dictionary->index==NULL) { |
0 |
1325 |
1325 |
|
| 1056 |
0 |
| error("add_word", "Unable to reallocate the index."); |
| 1057 |
0 |
| goto fail; |
| 1058 |
|
| } |
| 1059 |
|
| |
| 1060 |
|
| /* |
| 1061 |
|
| * Allocate one more entry for the word array |
| 1062 |
|
| */ |
| 1063 |
1325 |
▶ | if(dictionary->entry==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(dictionary->entry==NULL) { |
2 |
1323 |
1325 |
|
| 1064 |
2 |
| dictionary->entry=(STRING *)malloc(sizeof(STRING)*(dictionary->size)); |
| 1065 |
|
| } else { |
| 1066 |
1323 |
| dictionary->entry=(STRING *)realloc((STRING *)(dictionary->entry), |
| 1067 |
1323 |
| sizeof(STRING)*(dictionary->size)); |
| 1068 |
|
| } |
| 1069 |
1325 |
▶ | if(dictionary->entry==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(dictionary->entry==NULL) { |
0 |
1325 |
1325 |
|
| 1070 |
0 |
| error("add_word", "Unable to reallocate the dictionary to %d elements.", dictionary->size); |
| 1071 |
0 |
| goto fail; |
| 1072 |
|
| } |
| 1073 |
|
| |
| 1074 |
|
| /* |
| 1075 |
|
| * Copy the new word into the word array |
| 1076 |
|
| */ |
| 1077 |
1325 |
| dictionary->entry[dictionary->size-1].length=word.length; |
| 1078 |
1325 |
| dictionary->entry[dictionary->size-1].word=(char *)malloc(sizeof(char)* |
| 1079 |
1325 |
| (word.length)); |
| 1080 |
1325 |
▶ | if(dictionary->entry[dictionary->size-1].word==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(dictionary->entry[dictionary->size-1].word==NULL) { |
0 |
1325 |
1325 |
|
| 1081 |
0 |
| error("add_word", "Unable to allocate the word."); |
| 1082 |
0 |
| goto fail; |
| 1083 |
|
| } |
| 1084 |
9392 |
▶ | for(i=0; i<word.length; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<word.length; ++i) |
8067 |
1325 |
9392 |
|
| 1085 |
8067 |
| dictionary->entry[dictionary->size-1].word[i]=word.word[i]; |
| 1086 |
|
| |
| 1087 |
|
| /* |
| 1088 |
|
| * Shuffle the word index to keep it sorted alphabetically |
| 1089 |
|
| */ |
| 1090 |
444007 |
▶ | for(i=(dictionary->size-1); i>position; --i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=(dictionary->size-1); i>position; --i) |
442682 |
1325 |
444007 |
|
| 1091 |
442682 |
| dictionary->index[i]=dictionary->index[i-1]; |
| 1092 |
|
| |
| 1093 |
|
| /* |
| 1094 |
|
| * Copy the new symbol identifier into the word index |
| 1095 |
|
| */ |
| 1096 |
1325 |
| dictionary->index[position]=dictionary->size-1; |
| 1097 |
|
| |
| 1098 |
7281 |
| succeed: |
| 1099 |
7281 |
| return(dictionary->index[position]); |
| 1100 |
|
| |
| 1101 |
0 |
| fail: |
| 1102 |
0 |
| return(0); |
| 1103 |
|
| } |
| 1104 |
|
| |
| 1105 |
|
| /*---------------------------------------------------------------------------*/ |
| 1106 |
|
| |
| 1107 |
|
| /* |
| 1108 |
|
| * Function: Search_Dictionary |
| 1109 |
|
| * |
| 1110 |
|
| * Purpose: Search the dictionary for the specified word, returning its |
| 1111 |
|
| * position in the index if found, or the position where it |
| 1112 |
|
| * should be inserted otherwise. |
| 1113 |
|
| */ |
| 1114 |
107747 |
| int search_dictionary(DICTIONARY *dictionary, STRING word, bool *find) |
| 1115 |
|
| { |
| 1116 |
|
| int position; |
| 1117 |
|
| int min; |
| 1118 |
|
| int max; |
| 1119 |
|
| int middle; |
| 1120 |
|
| int compar; |
| 1121 |
|
| |
| 1122 |
|
| /* |
| 1123 |
|
| * If the dictionary is empty, then obviously the word won't be found |
| 1124 |
|
| */ |
| 1125 |
107747 |
▶ | if(dictionary->size==0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(dictionary->size==0) { |
1473 |
106274 |
107747 |
|
| 1126 |
1473 |
| position=0; |
| 1127 |
1473 |
| goto notfound; |
| 1128 |
|
| } |
| 1129 |
|
| |
| 1130 |
|
| /* |
| 1131 |
|
| * Initialize the lower and upper bounds of the search |
| 1132 |
|
| */ |
| 1133 |
106274 |
| min=0; |
| 1134 |
106274 |
| max=dictionary->size-1; |
| 1135 |
|
| /* |
| 1136 |
|
| * Search repeatedly, halving the search space each time, until either |
| 1137 |
|
| * the entry is found, or the search space becomes empty |
| 1138 |
|
| */ |
| 1139 |
|
| while(TRUE) { |
| 1140 |
|
| /* |
| 1141 |
|
| * See whether the middle element of the search space is greater |
| 1142 |
|
| * than, equal to, or less than the element being searched for. |
| 1143 |
|
| */ |
| 1144 |
627706 |
| middle=(min+max)/2; |
| 1145 |
627706 |
| compar=wordcmp(word, dictionary->entry[dictionary->index[middle]]); |
| 1146 |
|
| /* |
| 1147 |
|
| * If it is equal then we have found the element. Otherwise we |
| 1148 |
|
| * can halve the search space accordingly. |
| 1149 |
|
| */ |
| 1150 |
627706 |
▶ | if(compar==0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(compar==0) { |
57970 |
569736 |
627706 |
|
| 1151 |
57970 |
| position=middle; |
| 1152 |
57970 |
| goto found; |
| 1153 |
569736 |
▶ | } else if(compar>0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if(compar>0) { |
159747 |
409989 |
569736 |
|
| 1154 |
159747 |
▶ | if(max==middle) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(max==middle) { |
16546 |
143201 |
159747 |
|
| 1155 |
16546 |
| position=middle+1; |
| 1156 |
16546 |
| goto notfound; |
| 1157 |
|
| } |
| 1158 |
143201 |
| min=middle+1; |
| 1159 |
|
| } else { |
| 1160 |
409989 |
▶ | if(min==middle) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(min==middle) { |
31758 |
378231 |
409989 |
|
| 1161 |
31758 |
| position=middle; |
| 1162 |
31758 |
| goto notfound; |
| 1163 |
|
| } |
| 1164 |
378231 |
| max=middle-1; |
| 1165 |
|
| } |
| 1166 |
|
| } |
| 1167 |
|
| |
| 1168 |
57970 |
| found: |
| 1169 |
57970 |
| *find=TRUE; |
| 1170 |
57970 |
| return(position); |
| 1171 |
|
| |
| 1172 |
49777 |
| notfound: |
| 1173 |
49777 |
| *find=FALSE; |
| 1174 |
49777 |
| return(position); |
| 1175 |
|
| } |
| 1176 |
|
| |
| 1177 |
|
| /*---------------------------------------------------------------------------*/ |
| 1178 |
|
| |
| 1179 |
|
| /* |
| 1180 |
|
| * Function: Find_Word |
| 1181 |
|
| * |
| 1182 |
|
| * Purpose: Return the symbol corresponding to the word specified. |
| 1183 |
|
| * We assume that the word with index zero is equal to a |
| 1184 |
|
| * NULL word, indicating an error condition. |
| 1185 |
|
| */ |
| 1186 |
100466 |
| BYTE2 find_word(DICTIONARY *dictionary, STRING word) |
| 1187 |
|
| { |
| 1188 |
|
| int position; |
| 1189 |
|
| bool found; |
| 1190 |
|
| |
| 1191 |
100466 |
| position=search_dictionary(dictionary, word, &found); |
| 1192 |
|
| |
| 1193 |
100466 |
▶ | if(found==TRUE) return(dictionary->index[position]); |
Branch: 1 branch
| Branch | True | False | Total |
| if(found==TRUE) return(dictionary->index[position]); |
52014 |
48452 |
100466 |
|
| 1194 |
48452 |
| else return(0); |
| 1195 |
|
| } |
| 1196 |
|
| |
| 1197 |
|
| /*---------------------------------------------------------------------------*/ |
| 1198 |
|
| |
| 1199 |
|
| /* |
| 1200 |
|
| * Function: Wordcmp |
| 1201 |
|
| * |
| 1202 |
|
| * Purpose: Compare two words, and return an integer indicating whether |
| 1203 |
|
| * the first word is less than, equal to or greater than the |
| 1204 |
|
| * second word. |
| 1205 |
|
| */ |
| 1206 |
645171 |
| int wordcmp(STRING word1, STRING word2) |
| 1207 |
|
| { |
| 1208 |
|
| register int i; |
| 1209 |
|
| int bound; |
| 1210 |
|
| |
| 1211 |
645171 |
| bound=MIN(word1.length,word2.length); |
| 1212 |
|
| |
| 1213 |
976625 |
▶ | for(i=0; i<bound; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<bound; ++i) |
903757 |
72868 |
976625 |
|
| 1214 |
903757 |
▶ | if(toupper(word1.word[i])!=toupper(word2.word[i])) |
Branch: 1 branch
| Branch | True | False | Total |
| if(toupper(word1.word[i])!=toupper(word2.word[i])) |
572303 |
331454 |
903757 |
|
| 1215 |
572303 |
| return((int)(toupper(word1.word[i])-toupper(word2.word[i]))); |
| 1216 |
|
| |
| 1217 |
72868 |
▶ | if(word1.length<word2.length) return(-1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(word1.length<word2.length) return(-1); |
12699 |
60169 |
72868 |
|
| 1218 |
60169 |
▶ | if(word1.length>word2.length) return(1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(word1.length>word2.length) return(1); |
1966 |
58203 |
60169 |
|
| 1219 |
|
| |
| 1220 |
58203 |
| return(0); |
| 1221 |
|
| } |
| 1222 |
|
| |
| 1223 |
|
| /*---------------------------------------------------------------------------*/ |
| 1224 |
|
| |
| 1225 |
|
| /* |
| 1226 |
|
| * Function: Free_Dictionary |
| 1227 |
|
| * |
| 1228 |
|
| * Purpose: Release the memory consumed by the dictionary. |
| 1229 |
|
| */ |
| 1230 |
1097 |
| void free_dictionary(DICTIONARY *dictionary) |
| 1231 |
|
| { |
| 1232 |
1097 |
▶ | if(dictionary==NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(dictionary==NULL) return; |
3 |
1094 |
1097 |
|
| 1233 |
1094 |
▶ | if(dictionary->entry!=NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(dictionary->entry!=NULL) { |
1090 |
4 |
1094 |
|
| 1234 |
1090 |
| free(dictionary->entry); |
| 1235 |
1090 |
| dictionary->entry=NULL; |
| 1236 |
|
| } |
| 1237 |
1094 |
▶ | if(dictionary->index!=NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(dictionary->index!=NULL) { |
0 |
1094 |
1094 |
|
| 1238 |
0 |
| free(dictionary->index); |
| 1239 |
0 |
| dictionary->index=NULL; |
| 1240 |
|
| } |
| 1241 |
1094 |
| dictionary->size=0; |
| 1242 |
|
| } |
| 1243 |
|
| |
| 1244 |
|
| /*---------------------------------------------------------------------------*/ |
| 1245 |
|
| |
| 1246 |
1 |
| void free_model(MODEL *model) |
| 1247 |
|
| { |
| 1248 |
1 |
▶ | if(model==NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(model==NULL) return; |
1 |
0 |
1 |
|
| 1249 |
0 |
▶ | if(model->forward!=NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(model->forward!=NULL) { |
0 |
0 |
0 |
|
| 1250 |
0 |
| free_tree(model->forward); |
| 1251 |
|
| } |
| 1252 |
0 |
▶ | if(model->backward!=NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(model->backward!=NULL) { |
0 |
0 |
0 |
|
| 1253 |
0 |
| free_tree(model->backward); |
| 1254 |
|
| } |
| 1255 |
0 |
▶ | if(model->context!=NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(model->context!=NULL) { |
0 |
0 |
0 |
|
| 1256 |
0 |
| free(model->context); |
| 1257 |
|
| } |
| 1258 |
0 |
▶ | if(model->dictionary!=NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(model->dictionary!=NULL) { |
0 |
0 |
0 |
|
| 1259 |
0 |
| free_dictionary(model->dictionary); |
| 1260 |
0 |
| free(model->dictionary); |
| 1261 |
|
| } |
| 1262 |
0 |
| free(model); |
| 1263 |
|
| } |
| 1264 |
|
| |
| 1265 |
|
| /*---------------------------------------------------------------------------*/ |
| 1266 |
|
| |
| 1267 |
0 |
| void free_tree(TREE *tree) |
| 1268 |
|
| { |
| 1269 |
|
| static int level=0; |
| 1270 |
|
| register int i; |
| 1271 |
|
| |
| 1272 |
0 |
▶ | if(tree==NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(tree==NULL) return; |
0 |
0 |
0 |
|
| 1273 |
|
| |
| 1274 |
0 |
▶ | if(tree->tree!=NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(tree->tree!=NULL) { |
0 |
0 |
0 |
|
| 1275 |
0 |
▶ | if(level==0) progress("Freeing tree", 0, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(level==0) progress("Freeing tree", 0, 1); |
0 |
0 |
0 |
|
| 1276 |
0 |
▶ | for(i=0; i<tree->branch; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<tree->branch; ++i) { |
0 |
0 |
0 |
|
| 1277 |
0 |
| ++level; |
| 1278 |
0 |
| free_tree(tree->tree[i]); |
| 1279 |
0 |
| --level; |
| 1280 |
0 |
▶ | if(level==0) progress(NULL, i, tree->branch); |
Branch: 1 branch
| Branch | True | False | Total |
| if(level==0) progress(NULL, i, tree->branch); |
0 |
0 |
0 |
|
| 1281 |
|
| } |
| 1282 |
0 |
▶ | if(level==0) progress(NULL, 1, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(level==0) progress(NULL, 1, 1); |
0 |
0 |
0 |
|
| 1283 |
0 |
| free(tree->tree); |
| 1284 |
|
| } |
| 1285 |
0 |
| free(tree); |
| 1286 |
|
| } |
| 1287 |
|
| |
| 1288 |
|
| /*---------------------------------------------------------------------------*/ |
| 1289 |
|
| |
| 1290 |
|
| /* |
| 1291 |
|
| * Function: Initialize_Dictionary |
| 1292 |
|
| * |
| 1293 |
|
| * Purpose: Add dummy words to the dictionary. |
| 1294 |
|
| */ |
| 1295 |
1 |
| void initialize_dictionary(DICTIONARY *dictionary) |
| 1296 |
|
| { |
| 1297 |
1 |
| STRING word={ 7, "<ERROR>" }; |
| 1298 |
1 |
| STRING end={ 5, "<FIN>" }; |
| 1299 |
|
| |
| 1300 |
1 |
| (void)add_word(dictionary, word); |
| 1301 |
1 |
| (void)add_word(dictionary, end); |
| 1302 |
1 |
| } |
| 1303 |
|
| |
| 1304 |
|
| /*---------------------------------------------------------------------------*/ |
| 1305 |
|
| |
| 1306 |
|
| /* |
| 1307 |
|
| * Function: New_Dictionary |
| 1308 |
|
| * |
| 1309 |
|
| * Purpose: Allocate room for a new dictionary. |
| 1310 |
|
| */ |
| 1311 |
10 |
| DICTIONARY *new_dictionary(void) |
| 1312 |
|
| { |
| 1313 |
10 |
| DICTIONARY *dictionary=NULL; |
| 1314 |
|
| |
| 1315 |
10 |
| dictionary=(DICTIONARY *)malloc(sizeof(DICTIONARY)); |
| 1316 |
10 |
▶ | if(dictionary==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(dictionary==NULL) { |
0 |
10 |
10 |
|
| 1317 |
0 |
| error("new_dictionary", "Unable to allocate dictionary."); |
| 1318 |
0 |
| return(NULL); |
| 1319 |
|
| } |
| 1320 |
|
| |
| 1321 |
10 |
| dictionary->size=0; |
| 1322 |
10 |
| dictionary->index=NULL; |
| 1323 |
10 |
| dictionary->entry=NULL; |
| 1324 |
|
| |
| 1325 |
10 |
| return(dictionary); |
| 1326 |
|
| } |
| 1327 |
|
| |
| 1328 |
|
| /*---------------------------------------------------------------------------*/ |
| 1329 |
|
| |
| 1330 |
|
| /* |
| 1331 |
|
| * Function: Save_Dictionary |
| 1332 |
|
| * |
| 1333 |
|
| * Purpose: Save a dictionary to the specified file. |
| 1334 |
|
| */ |
| 1335 |
1 |
| void save_dictionary(FILE *file, DICTIONARY *dictionary) |
| 1336 |
|
| { |
| 1337 |
|
| register int i; |
| 1338 |
|
| |
| 1339 |
1 |
| fwrite(&(dictionary->size), sizeof(BYTE4), 1, file); |
| 1340 |
1 |
| progress("Saving dictionary", 0, 1); |
| 1341 |
1319 |
▶ | for(i=0; i<dictionary->size; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<dictionary->size; ++i) { |
1318 |
1 |
1319 |
|
| 1342 |
1318 |
| save_word(file, dictionary->entry[i]); |
| 1343 |
1318 |
| progress(NULL, i, dictionary->size); |
| 1344 |
|
| } |
| 1345 |
1 |
| progress(NULL, 1, 1); |
| 1346 |
1 |
| } |
| 1347 |
|
| |
| 1348 |
|
| /*---------------------------------------------------------------------------*/ |
| 1349 |
|
| |
| 1350 |
|
| /* |
| 1351 |
|
| * Function: Load_Dictionary |
| 1352 |
|
| * |
| 1353 |
|
| * Purpose: Load a dictionary from the specified file. |
| 1354 |
|
| */ |
| 1355 |
0 |
| void load_dictionary(FILE *file, DICTIONARY *dictionary) |
| 1356 |
|
| { |
| 1357 |
|
| register int i; |
| 1358 |
|
| int size; |
| 1359 |
|
| |
| 1360 |
0 |
| fread(&size, sizeof(BYTE4), 1, file); |
| 1361 |
0 |
| progress("Loading dictionary", 0, 1); |
| 1362 |
0 |
▶ | for(i=0; i<size; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<size; ++i) { |
0 |
0 |
0 |
|
| 1363 |
0 |
| load_word(file, dictionary); |
| 1364 |
0 |
| progress(NULL, i, size); |
| 1365 |
|
| } |
| 1366 |
0 |
| progress(NULL, 1, 1); |
| 1367 |
0 |
| } |
| 1368 |
|
| |
| 1369 |
|
| /*---------------------------------------------------------------------------*/ |
| 1370 |
|
| |
| 1371 |
|
| /* |
| 1372 |
|
| * Function: Save_Word |
| 1373 |
|
| * |
| 1374 |
|
| * Purpose: Save a dictionary word to a file. |
| 1375 |
|
| */ |
| 1376 |
1318 |
| void save_word(FILE *file, STRING word) |
| 1377 |
|
| { |
| 1378 |
|
| register int i; |
| 1379 |
|
| |
| 1380 |
1318 |
| fwrite(&(word.length), sizeof(BYTE1), 1, file); |
| 1381 |
9362 |
▶ | for(i=0; i<word.length; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<word.length; ++i) |
8044 |
1318 |
9362 |
|
| 1382 |
8044 |
| fwrite(&(word.word[i]), sizeof(char), 1, file); |
| 1383 |
1318 |
| } |
| 1384 |
|
| |
| 1385 |
|
| /*---------------------------------------------------------------------------*/ |
| 1386 |
|
| |
| 1387 |
|
| /* |
| 1388 |
|
| * Function: Load_Word |
| 1389 |
|
| * |
| 1390 |
|
| * Purpose: Load a dictionary word from a file. |
| 1391 |
|
| */ |
| 1392 |
0 |
| void load_word(FILE *file, DICTIONARY *dictionary) |
| 1393 |
|
| { |
| 1394 |
|
| register int i; |
| 1395 |
|
| STRING word; |
| 1396 |
|
| |
| 1397 |
0 |
| fread(&(word.length), sizeof(BYTE1), 1, file); |
| 1398 |
0 |
| word.word=(char *)malloc(sizeof(char)*word.length); |
| 1399 |
0 |
▶ | if(word.word==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(word.word==NULL) { |
0 |
0 |
0 |
|
| 1400 |
0 |
| error("load_word", "Unable to allocate word"); |
| 1401 |
0 |
| return; |
| 1402 |
|
| } |
| 1403 |
0 |
▶ | for(i=0; i<word.length; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<word.length; ++i) |
0 |
0 |
0 |
|
| 1404 |
0 |
| fread(&(word.word[i]), sizeof(char), 1, file); |
| 1405 |
0 |
| add_word(dictionary, word); |
| 1406 |
0 |
| free(word.word); |
| 1407 |
|
| } |
| 1408 |
|
| |
| 1409 |
|
| /*---------------------------------------------------------------------------*/ |
| 1410 |
|
| |
| 1411 |
|
| /* |
| 1412 |
|
| * Function: New_Node |
| 1413 |
|
| * |
| 1414 |
|
| * Purpose: Allocate a new node for the n-gram tree, and initialise |
| 1415 |
|
| * its contents to sensible values. |
| 1416 |
|
| */ |
| 1417 |
50043 |
| TREE *new_node(void) |
| 1418 |
|
| { |
| 1419 |
50043 |
| TREE *node=NULL; |
| 1420 |
|
| |
| 1421 |
|
| /* |
| 1422 |
|
| * Allocate memory for the new node |
| 1423 |
|
| */ |
| 1424 |
50043 |
| node=(TREE *)malloc(sizeof(TREE)); |
| 1425 |
50043 |
▶ | if(node==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(node==NULL) { |
0 |
50043 |
50043 |
|
| 1426 |
0 |
| error("new_node", "Unable to allocate the node."); |
| 1427 |
0 |
| goto fail; |
| 1428 |
|
| } |
| 1429 |
|
| |
| 1430 |
|
| /* |
| 1431 |
|
| * Initialise the contents of the node |
| 1432 |
|
| */ |
| 1433 |
50043 |
| node->symbol=0; |
| 1434 |
50043 |
| node->usage=0; |
| 1435 |
50043 |
| node->count=0; |
| 1436 |
50043 |
| node->branch=0; |
| 1437 |
50043 |
| node->tree=NULL; |
| 1438 |
|
| |
| 1439 |
50043 |
| return(node); |
| 1440 |
|
| |
| 1441 |
0 |
| fail: |
| 1442 |
0 |
▶ | if(node!=NULL) free(node); |
Branch: 1 branch
| Branch | True | False | Total |
| if(node!=NULL) free(node); |
0 |
0 |
0 |
|
| 1443 |
0 |
| return(NULL); |
| 1444 |
|
| } |
| 1445 |
|
| |
| 1446 |
|
| /*---------------------------------------------------------------------------*/ |
| 1447 |
|
| |
| 1448 |
|
| /* |
| 1449 |
|
| * Function: New_Model |
| 1450 |
|
| * |
| 1451 |
|
| * Purpose: Create and initialise a new ngram model. |
| 1452 |
|
| */ |
| 1453 |
1 |
| MODEL *new_model(int order) |
| 1454 |
|
| { |
| 1455 |
1 |
| MODEL *model=NULL; |
| 1456 |
|
| |
| 1457 |
1 |
| model=(MODEL *)malloc(sizeof(MODEL)); |
| 1458 |
1 |
▶ | if(model==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(model==NULL) { |
0 |
1 |
1 |
|
| 1459 |
0 |
| error("new_model", "Unable to allocate model."); |
| 1460 |
0 |
| goto fail; |
| 1461 |
|
| } |
| 1462 |
|
| |
| 1463 |
1 |
| model->order=order; |
| 1464 |
1 |
| model->forward=new_node(); |
| 1465 |
1 |
| model->backward=new_node(); |
| 1466 |
1 |
| model->context=(TREE **)malloc(sizeof(TREE *)*(order+2)); |
| 1467 |
1 |
▶ | if(model->context==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(model->context==NULL) { |
0 |
1 |
1 |
|
| 1468 |
0 |
| error("new_model", "Unable to allocate context array."); |
| 1469 |
0 |
| goto fail; |
| 1470 |
|
| } |
| 1471 |
1 |
| initialize_context(model); |
| 1472 |
1 |
| model->dictionary=new_dictionary(); |
| 1473 |
1 |
| initialize_dictionary(model->dictionary); |
| 1474 |
|
| |
| 1475 |
1 |
| return(model); |
| 1476 |
|
| |
| 1477 |
0 |
| fail: |
| 1478 |
0 |
| return(NULL); |
| 1479 |
|
| } |
| 1480 |
|
| |
| 1481 |
|
| /*---------------------------------------------------------------------------*/ |
| 1482 |
|
| |
| 1483 |
|
| /* |
| 1484 |
|
| * Function: Update_Model |
| 1485 |
|
| * |
| 1486 |
|
| * Purpose: Update the model with the specified symbol. |
| 1487 |
|
| */ |
| 1488 |
15244 |
| void update_model(MODEL *model, int symbol) |
| 1489 |
|
| { |
| 1490 |
|
| register int i; |
| 1491 |
|
| |
| 1492 |
|
| /* |
| 1493 |
|
| * Update all of the models in the current context with the specified |
| 1494 |
|
| * symbol. |
| 1495 |
|
| */ |
| 1496 |
106708 |
▶ | for(i=(model->order+1); i>0; --i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=(model->order+1); i>0; --i) |
91464 |
15244 |
106708 |
|
| 1497 |
91464 |
▶ | if(model->context[i-1]!=NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(model->context[i-1]!=NULL) |
80964 |
10500 |
91464 |
|
| 1498 |
80964 |
| model->context[i]=add_symbol(model->context[i-1], (BYTE2)symbol); |
| 1499 |
|
| |
| 1500 |
15244 |
| return; |
| 1501 |
|
| } |
| 1502 |
|
| |
| 1503 |
|
| /*---------------------------------------------------------------------------*/ |
| 1504 |
|
| |
| 1505 |
|
| /* |
| 1506 |
|
| * Function: Update_Context |
| 1507 |
|
| * |
| 1508 |
|
| * Purpose: Update the context of the model without adding the symbol. |
| 1509 |
|
| */ |
| 1510 |
48836 |
| void update_context(MODEL *model, int symbol) |
| 1511 |
|
| { |
| 1512 |
|
| register int i; |
| 1513 |
|
| |
| 1514 |
341852 |
▶ | for(i=(model->order+1); i>0; --i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=(model->order+1); i>0; --i) |
293016 |
48836 |
341852 |
|
| 1515 |
293016 |
▶ | if(model->context[i-1]!=NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(model->context[i-1]!=NULL) |
249451 |
43565 |
293016 |
|
| 1516 |
249451 |
| model->context[i]=find_symbol(model->context[i-1], symbol); |
| 1517 |
48836 |
| } |
| 1518 |
|
| |
| 1519 |
|
| /*---------------------------------------------------------------------------*/ |
| 1520 |
|
| |
| 1521 |
|
| /* |
| 1522 |
|
| * Function: Add_Symbol |
| 1523 |
|
| * |
| 1524 |
|
| * Purpose: Update the statistics of the specified tree with the |
| 1525 |
|
| * specified symbol, which may mean growing the tree if the |
| 1526 |
|
| * symbol hasn't been seen in this context before. |
| 1527 |
|
| */ |
| 1528 |
80964 |
| TREE *add_symbol(TREE *tree, BYTE2 symbol) |
| 1529 |
|
| { |
| 1530 |
80964 |
| TREE *node=NULL; |
| 1531 |
|
| |
| 1532 |
|
| /* |
| 1533 |
|
| * Search for the symbol in the subtree of the tree node. |
| 1534 |
|
| */ |
| 1535 |
80964 |
| node=find_symbol_add(tree, symbol); |
| 1536 |
|
| |
| 1537 |
|
| /* |
| 1538 |
|
| * Increment the symbol counts |
| 1539 |
|
| */ |
| 1540 |
80964 |
▶ | if((node->count<65535)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if((node->count<65535)) { |
80964 |
0 |
80964 |
|
| 1541 |
80964 |
| node->count+=1; |
| 1542 |
80964 |
| tree->usage+=1; |
| 1543 |
|
| } |
| 1544 |
|
| |
| 1545 |
80964 |
| return(node); |
| 1546 |
|
| } |
| 1547 |
|
| |
| 1548 |
|
| /*---------------------------------------------------------------------------*/ |
| 1549 |
|
| |
| 1550 |
|
| /* |
| 1551 |
|
| * Function: Find_Symbol |
| 1552 |
|
| * |
| 1553 |
|
| * Purpose: Return a pointer to the child node, if one exists, which |
| 1554 |
|
| * contains the specified symbol. |
| 1555 |
|
| */ |
| 1556 |
278719 |
| TREE *find_symbol(TREE *node, int symbol) |
| 1557 |
|
| { |
| 1558 |
|
| register int i; |
| 1559 |
278719 |
| TREE *found=NULL; |
| 1560 |
278719 |
| bool found_symbol=FALSE; |
| 1561 |
|
| |
| 1562 |
|
| /* |
| 1563 |
|
| * Perform a binary search for the symbol. |
| 1564 |
|
| */ |
| 1565 |
278719 |
| i=search_node(node, symbol, &found_symbol); |
| 1566 |
278719 |
▶ | if(found_symbol==TRUE) found=node->tree[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| if(found_symbol==TRUE) found=node->tree[i]; |
278719 |
0 |
278719 |
|
| 1567 |
|
| |
| 1568 |
278719 |
| return(found); |
| 1569 |
|
| } |
| 1570 |
|
| |
| 1571 |
|
| /*---------------------------------------------------------------------------*/ |
| 1572 |
|
| |
| 1573 |
|
| /* |
| 1574 |
|
| * Function: Find_Symbol_Add |
| 1575 |
|
| * |
| 1576 |
|
| * Purpose: This function is conceptually similar to find_symbol, |
| 1577 |
|
| * apart from the fact that if the symbol is not found, |
| 1578 |
|
| * a new node is automatically allocated and added to the |
| 1579 |
|
| * tree. |
| 1580 |
|
| */ |
| 1581 |
80964 |
| TREE *find_symbol_add(TREE *node, int symbol) |
| 1582 |
|
| { |
| 1583 |
|
| register int i; |
| 1584 |
80964 |
| TREE *found=NULL; |
| 1585 |
80964 |
| bool found_symbol=FALSE; |
| 1586 |
|
| |
| 1587 |
|
| /* |
| 1588 |
|
| * Perform a binary search for the symbol. If the symbol isn't found, |
| 1589 |
|
| * attach a new sub-node to the tree node so that it remains sorted. |
| 1590 |
|
| */ |
| 1591 |
80964 |
| i=search_node(node, symbol, &found_symbol); |
| 1592 |
80964 |
▶ | if(found_symbol==TRUE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(found_symbol==TRUE) { |
30923 |
50041 |
80964 |
|
| 1593 |
30923 |
| found=node->tree[i]; |
| 1594 |
|
| } else { |
| 1595 |
50041 |
| found=new_node(); |
| 1596 |
50041 |
| found->symbol=symbol; |
| 1597 |
50041 |
| add_node(node, found, i); |
| 1598 |
|
| } |
| 1599 |
|
| |
| 1600 |
80964 |
| return(found); |
| 1601 |
|
| } |
| 1602 |
|
| |
| 1603 |
|
| /*---------------------------------------------------------------------------*/ |
| 1604 |
|
| |
| 1605 |
|
| /* |
| 1606 |
|
| * Function: Add_Node |
| 1607 |
|
| * |
| 1608 |
|
| * Purpose: Attach a new child node to the sub-tree of the tree |
| 1609 |
|
| * specified. |
| 1610 |
|
| */ |
| 1611 |
50041 |
| void add_node(TREE *tree, TREE *node, int position) |
| 1612 |
|
| { |
| 1613 |
|
| register int i; |
| 1614 |
|
| |
| 1615 |
|
| /* |
| 1616 |
|
| * Allocate room for one more child node, which may mean allocating |
| 1617 |
|
| * the sub-tree from scratch. |
| 1618 |
|
| */ |
| 1619 |
50041 |
▶ | if(tree->tree==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(tree->tree==NULL) { |
36912 |
13129 |
50041 |
|
| 1620 |
36912 |
| tree->tree=(TREE **)malloc(sizeof(TREE *)*(tree->branch+1)); |
| 1621 |
|
| } else { |
| 1622 |
13129 |
| tree->tree=(TREE **)realloc((TREE **)(tree->tree),sizeof(TREE *)* |
| 1623 |
13129 |
| (tree->branch+1)); |
| 1624 |
|
| } |
| 1625 |
50041 |
▶ | if(tree->tree==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(tree->tree==NULL) { |
0 |
50041 |
50041 |
|
| 1626 |
0 |
| error("add_node", "Unable to reallocate subtree."); |
| 1627 |
0 |
| return; |
| 1628 |
|
| } |
| 1629 |
|
| |
| 1630 |
|
| /* |
| 1631 |
|
| * Shuffle the nodes down so that we can insert the new node at the |
| 1632 |
|
| * subtree index given by position. |
| 1633 |
|
| */ |
| 1634 |
90428 |
▶ | for(i=tree->branch; i>position; --i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=tree->branch; i>position; --i) |
40387 |
50041 |
90428 |
|
| 1635 |
40387 |
| tree->tree[i]=tree->tree[i-1]; |
| 1636 |
|
| |
| 1637 |
|
| /* |
| 1638 |
|
| * Add the new node to the sub-tree. |
| 1639 |
|
| */ |
| 1640 |
50041 |
| tree->tree[position]=node; |
| 1641 |
50041 |
| tree->branch+=1; |
| 1642 |
|
| } |
| 1643 |
|
| |
| 1644 |
|
| /*---------------------------------------------------------------------------*/ |
| 1645 |
|
| |
| 1646 |
|
| /* |
| 1647 |
|
| * Function: Search_Node |
| 1648 |
|
| * |
| 1649 |
|
| * Purpose: Perform a binary search for the specified symbol on the |
| 1650 |
|
| * subtree of the given node. Return the position of the |
| 1651 |
|
| * child node in the subtree if the symbol was found, or the |
| 1652 |
|
| * position where it should be inserted to keep the subtree |
| 1653 |
|
| * sorted if it wasn't. |
| 1654 |
|
| */ |
| 1655 |
359683 |
| int search_node(TREE *node, int symbol, bool *found_symbol) |
| 1656 |
|
| { |
| 1657 |
|
| register int position; |
| 1658 |
|
| int min; |
| 1659 |
|
| int max; |
| 1660 |
|
| int middle; |
| 1661 |
|
| int compar; |
| 1662 |
|
| |
| 1663 |
|
| /* |
| 1664 |
|
| * Handle the special case where the subtree is empty. |
| 1665 |
|
| */ |
| 1666 |
359683 |
▶ | if(node->branch==0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->branch==0) { |
36912 |
322771 |
359683 |
|
| 1667 |
36912 |
| position=0; |
| 1668 |
36912 |
| goto notfound; |
| 1669 |
|
| } |
| 1670 |
|
| |
| 1671 |
|
| /* |
| 1672 |
|
| * Perform a binary search on the subtree. |
| 1673 |
|
| */ |
| 1674 |
322771 |
| min=0; |
| 1675 |
322771 |
| max=node->branch-1; |
| 1676 |
|
| while(TRUE) { |
| 1677 |
1343109 |
| middle=(min+max)/2; |
| 1678 |
1343109 |
| compar=symbol-node->tree[middle]->symbol; |
| 1679 |
1343109 |
▶ | if(compar==0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(compar==0) { |
309642 |
1033467 |
1343109 |
|
| 1680 |
309642 |
| position=middle; |
| 1681 |
309642 |
| goto found; |
| 1682 |
1033467 |
▶ | } else if(compar>0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if(compar>0) { |
430810 |
602657 |
1033467 |
|
| 1683 |
430810 |
▶ | if(max==middle) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(max==middle) { |
9499 |
421311 |
430810 |
|
| 1684 |
9499 |
| position=middle+1; |
| 1685 |
9499 |
| goto notfound; |
| 1686 |
|
| } |
| 1687 |
421311 |
| min=middle+1; |
| 1688 |
|
| } else { |
| 1689 |
602657 |
▶ | if(min==middle) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(min==middle) { |
3630 |
599027 |
602657 |
|
| 1690 |
3630 |
| position=middle; |
| 1691 |
3630 |
| goto notfound; |
| 1692 |
|
| } |
| 1693 |
599027 |
| max=middle-1; |
| 1694 |
|
| } |
| 1695 |
|
| } |
| 1696 |
|
| |
| 1697 |
309642 |
| found: |
| 1698 |
309642 |
| *found_symbol=TRUE; |
| 1699 |
309642 |
| return(position); |
| 1700 |
|
| |
| 1701 |
50041 |
| notfound: |
| 1702 |
50041 |
| *found_symbol=FALSE; |
| 1703 |
50041 |
| return(position); |
| 1704 |
|
| } |
| 1705 |
|
| |
| 1706 |
|
| /*---------------------------------------------------------------------------*/ |
| 1707 |
|
| |
| 1708 |
|
| /* |
| 1709 |
|
| * Function: Initialize_Context |
| 1710 |
|
| * |
| 1711 |
|
| * Purpose: Set the context of the model to a default value. |
| 1712 |
|
| */ |
| 1713 |
3667 |
| void initialize_context(MODEL *model) |
| 1714 |
|
| { |
| 1715 |
|
| register int i; |
| 1716 |
|
| |
| 1717 |
25669 |
▶ | for(i=0; i<=model->order; ++i) model->context[i]=NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<=model->order; ++i) model->context[i]=NULL; |
22002 |
3667 |
25669 |
|
| 1718 |
3667 |
| } |
| 1719 |
|
| |
| 1720 |
|
| /*---------------------------------------------------------------------------*/ |
| 1721 |
|
| |
| 1722 |
|
| /* |
| 1723 |
|
| * Function: Learn |
| 1724 |
|
| * |
| 1725 |
|
| * Purpose: Learn from the user's input. |
| 1726 |
|
| */ |
| 1727 |
350 |
| void learn(MODEL *model, DICTIONARY *words) |
| 1728 |
|
| { |
| 1729 |
|
| register int i; |
| 1730 |
|
| BYTE2 symbol; |
| 1731 |
|
| |
| 1732 |
|
| /* |
| 1733 |
|
| * We only learn from inputs which are long enough |
| 1734 |
|
| */ |
| 1735 |
350 |
▶ | if(words->size<=(model->order)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(words->size<=(model->order)) return; |
0 |
350 |
350 |
|
| 1736 |
|
| |
| 1737 |
|
| /* |
| 1738 |
|
| * Train the model in the forwards direction. Start by initializing |
| 1739 |
|
| * the context of the model. |
| 1740 |
|
| */ |
| 1741 |
350 |
| initialize_context(model); |
| 1742 |
350 |
| model->context[0]=model->forward; |
| 1743 |
7622 |
▶ | for(i=0; i<words->size; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<words->size; ++i) { |
7272 |
350 |
7622 |
|
| 1744 |
|
| /* |
| 1745 |
|
| * Add the symbol to the model's dictionary if necessary, and then |
| 1746 |
|
| * update the forward model accordingly. |
| 1747 |
|
| */ |
| 1748 |
7272 |
| symbol=add_word(model->dictionary, words->entry[i]); |
| 1749 |
7272 |
| update_model(model, symbol); |
| 1750 |
|
| } |
| 1751 |
|
| /* |
| 1752 |
|
| * Add the sentence-terminating symbol. |
| 1753 |
|
| */ |
| 1754 |
350 |
| update_model(model, 1); |
| 1755 |
|
| |
| 1756 |
|
| /* |
| 1757 |
|
| * Train the model in the backwards direction. Start by initializing |
| 1758 |
|
| * the context of the model. |
| 1759 |
|
| */ |
| 1760 |
350 |
| initialize_context(model); |
| 1761 |
350 |
| model->context[0]=model->backward; |
| 1762 |
7622 |
▶ | for(i=words->size-1; i>=0; --i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=words->size-1; i>=0; --i) { |
7272 |
350 |
7622 |
|
| 1763 |
|
| /* |
| 1764 |
|
| * Find the symbol in the model's dictionary, and then update |
| 1765 |
|
| * the backward model accordingly. |
| 1766 |
|
| */ |
| 1767 |
7272 |
| symbol=find_word(model->dictionary, words->entry[i]); |
| 1768 |
7272 |
| update_model(model, symbol); |
| 1769 |
|
| } |
| 1770 |
|
| /* |
| 1771 |
|
| * Add the sentence-terminating symbol. |
| 1772 |
|
| */ |
| 1773 |
350 |
| update_model(model, 1); |
| 1774 |
|
| |
| 1775 |
350 |
| return; |
| 1776 |
|
| } |
| 1777 |
|
| |
| 1778 |
|
| /*---------------------------------------------------------------------------*/ |
| 1779 |
|
| |
| 1780 |
|
| /* |
| 1781 |
|
| * Function: Train |
| 1782 |
|
| * |
| 1783 |
|
| * Purpose: Infer a MegaHAL brain from the contents of a text file. |
| 1784 |
|
| */ |
| 1785 |
1 |
| void train(MODEL *model, char *filename) |
| 1786 |
|
| { |
| 1787 |
|
| FILE *file; |
| 1788 |
|
| char buffer[1024]; |
| 1789 |
1 |
| DICTIONARY *words=NULL; |
| 1790 |
|
| int length; |
| 1791 |
|
| |
| 1792 |
1 |
▶ | if(filename==NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) return; |
0 |
1 |
1 |
|
| 1793 |
|
| |
| 1794 |
1 |
| file=fopen(filename, "r"); |
| 1795 |
1 |
▶ | if(file==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(file==NULL) { |
0 |
1 |
1 |
|
| 1796 |
0 |
| printf("Unable to find the personality %s\n", filename); |
| 1797 |
0 |
| return; |
| 1798 |
|
| } |
| 1799 |
|
| |
| 1800 |
1 |
| fseek(file, 0, 2); |
| 1801 |
1 |
| length=ftell(file); |
| 1802 |
1 |
| rewind(file); |
| 1803 |
|
| |
| 1804 |
1 |
| words=new_dictionary(); |
| 1805 |
|
| |
| 1806 |
1 |
| progress("Training from file", 0, 1); |
| 1807 |
398 |
▶ | while(!feof(file)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(!feof(file)) { |
398 |
0 |
398 |
|
| 1808 |
|
| |
| 1809 |
398 |
▶ | if(fgets(buffer, 1024, file)==NULL) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if(fgets(buffer, 1024, file)==NULL) break; |
1 |
397 |
398 |
|
| 1810 |
397 |
▶ | if(buffer[0]=='#') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if(buffer[0]=='#') continue; |
48 |
349 |
397 |
|
| 1811 |
|
| |
| 1812 |
349 |
| buffer[strlen(buffer)-1]='\0'; |
| 1813 |
|
| |
| 1814 |
349 |
| upper(buffer); |
| 1815 |
349 |
| make_words(buffer, words); |
| 1816 |
349 |
| learn(model, words); |
| 1817 |
|
| |
| 1818 |
349 |
| progress(NULL, ftell(file), length); |
| 1819 |
|
| |
| 1820 |
|
| } |
| 1821 |
1 |
| progress(NULL, 1, 1); |
| 1822 |
|
| |
| 1823 |
1 |
| free_dictionary(words); |
| 1824 |
1 |
| fclose(file); |
| 1825 |
|
| } |
| 1826 |
|
| |
| 1827 |
|
| /*---------------------------------------------------------------------------*/ |
| 1828 |
|
| |
| 1829 |
|
| /* |
| 1830 |
|
| * Function: Show_Dictionary |
| 1831 |
|
| * |
| 1832 |
|
| * Purpose: Display the dictionary for training purposes. |
| 1833 |
|
| */ |
| 1834 |
1 |
| void show_dictionary(DICTIONARY *dictionary) |
| 1835 |
|
| { |
| 1836 |
|
| register int i; |
| 1837 |
|
| register int j; |
| 1838 |
|
| FILE *file; |
| 1839 |
|
| |
| 1840 |
1 |
| file=fopen("megahal.dic", "w"); |
| 1841 |
1 |
▶ | if(file==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(file==NULL) { |
0 |
1 |
1 |
|
| 1842 |
0 |
| warn("show_dictionary", "Unable to open file"); |
| 1843 |
0 |
| return; |
| 1844 |
|
| } |
| 1845 |
|
| |
| 1846 |
1319 |
▶ | for(i=0; i<dictionary->size; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<dictionary->size; ++i) { |
1318 |
1 |
1319 |
|
| 1847 |
9362 |
▶ | for(j=0; j<dictionary->entry[i].length; ++j) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<dictionary->entry[i].length; ++j) |
8044 |
1318 |
9362 |
|
| 1848 |
8044 |
| fprintf(file, "%c", dictionary->entry[i].word[j]); |
| 1849 |
1318 |
| fprintf(file, "\n"); |
| 1850 |
|
| } |
| 1851 |
|
| |
| 1852 |
1 |
| fclose(file); |
| 1853 |
|
| } |
| 1854 |
|
| |
| 1855 |
|
| /*---------------------------------------------------------------------------*/ |
| 1856 |
|
| |
| 1857 |
|
| /* |
| 1858 |
|
| * Function: Save_Model |
| 1859 |
|
| * |
| 1860 |
|
| * Purpose: Save the current state to a MegaHAL brain file. |
| 1861 |
|
| */ |
| 1862 |
1 |
| void save_model(char *modelname, MODEL *model) |
| 1863 |
|
| { |
| 1864 |
|
| FILE *file; |
| 1865 |
|
| static char *filename=NULL; |
| 1866 |
|
| |
| 1867 |
1 |
▶ | if(filename==NULL) filename=(char *)malloc(sizeof(char)*1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) filename=(char *)malloc(sizeof(char)*1); |
1 |
0 |
1 |
|
| 1868 |
|
| |
| 1869 |
|
| /* |
| 1870 |
|
| * Allocate memory for the filename |
| 1871 |
|
| */ |
| 1872 |
1 |
| filename=(char *)realloc(filename, |
| 1873 |
1 |
| sizeof(char)*(strlen(directory)+strlen(SEP)+12)); |
| 1874 |
1 |
▶ | if(filename==NULL) error("save_model","Unable to allocate filename"); |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) error("save_model","Unable to allocate filename"); |
0 |
1 |
1 |
|
| 1875 |
|
| |
| 1876 |
1 |
| show_dictionary(model->dictionary); |
| 1877 |
1 |
▶ | if(filename==NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) return; |
0 |
1 |
1 |
|
| 1878 |
|
| |
| 1879 |
1 |
| sprintf(filename, "%s%smegahal.brn", directory, SEP); |
| 1880 |
1 |
| file=fopen(filename, "wb"); |
| 1881 |
1 |
▶ | if(file==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(file==NULL) { |
0 |
1 |
1 |
|
| 1882 |
0 |
| warn("save_model", "Unable to open file `%s'", filename); |
| 1883 |
0 |
| return; |
| 1884 |
|
| } |
| 1885 |
|
| |
| 1886 |
1 |
| fwrite(COOKIE, sizeof(char), strlen(COOKIE), file); |
| 1887 |
1 |
| fwrite(&(model->order), sizeof(BYTE1), 1, file); |
| 1888 |
1 |
| save_tree(file, model->forward); |
| 1889 |
1 |
| save_tree(file, model->backward); |
| 1890 |
1 |
| save_dictionary(file, model->dictionary); |
| 1891 |
|
| |
| 1892 |
1 |
| fclose(file); |
| 1893 |
|
| } |
| 1894 |
|
| |
| 1895 |
|
| /*---------------------------------------------------------------------------*/ |
| 1896 |
|
| |
| 1897 |
|
| /* |
| 1898 |
|
| * Function: Save_Tree |
| 1899 |
|
| * |
| 1900 |
|
| * Purpose: Save a tree structure to the specified file. |
| 1901 |
|
| */ |
| 1902 |
50043 |
| void save_tree(FILE *file, TREE *node) |
| 1903 |
|
| { |
| 1904 |
|
| static int level=0; |
| 1905 |
|
| register int i; |
| 1906 |
|
| |
| 1907 |
50043 |
| fwrite(&(node->symbol), sizeof(BYTE2), 1, file); |
| 1908 |
50043 |
| fwrite(&(node->usage), sizeof(BYTE4), 1, file); |
| 1909 |
50043 |
| fwrite(&(node->count), sizeof(BYTE2), 1, file); |
| 1910 |
50043 |
| fwrite(&(node->branch), sizeof(BYTE2), 1, file); |
| 1911 |
|
| |
| 1912 |
50043 |
▶ | if(level==0) progress("Saving tree", 0, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(level==0) progress("Saving tree", 0, 1); |
2 |
50041 |
50043 |
|
| 1913 |
100084 |
▶ | for(i=0; i<node->branch; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<node->branch; ++i) { |
50041 |
50043 |
100084 |
|
| 1914 |
50041 |
| ++level; |
| 1915 |
50041 |
| save_tree(file, node->tree[i]); |
| 1916 |
50041 |
| --level; |
| 1917 |
50041 |
▶ | if(level==0) progress(NULL, i, node->branch); |
Branch: 1 branch
| Branch | True | False | Total |
| if(level==0) progress(NULL, i, node->branch); |
2634 |
47407 |
50041 |
|
| 1918 |
|
| } |
| 1919 |
50043 |
▶ | if(level==0) progress(NULL, 1, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(level==0) progress(NULL, 1, 1); |
2 |
50041 |
50043 |
|
| 1920 |
50043 |
| } |
| 1921 |
|
| |
| 1922 |
|
| /*---------------------------------------------------------------------------*/ |
| 1923 |
|
| |
| 1924 |
|
| /* |
| 1925 |
|
| * Function: Load_Tree |
| 1926 |
|
| * |
| 1927 |
|
| * Purpose: Load a tree structure from the specified file. |
| 1928 |
|
| */ |
| 1929 |
0 |
| void load_tree(FILE *file, TREE *node) |
| 1930 |
|
| { |
| 1931 |
|
| static int level=0; |
| 1932 |
|
| register int i; |
| 1933 |
|
| |
| 1934 |
0 |
| fread(&(node->symbol), sizeof(BYTE2), 1, file); |
| 1935 |
0 |
| fread(&(node->usage), sizeof(BYTE4), 1, file); |
| 1936 |
0 |
| fread(&(node->count), sizeof(BYTE2), 1, file); |
| 1937 |
0 |
| fread(&(node->branch), sizeof(BYTE2), 1, file); |
| 1938 |
|
| |
| 1939 |
0 |
▶ | if(node->branch==0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->branch==0) return; |
0 |
0 |
0 |
|
| 1940 |
|
| |
| 1941 |
0 |
| node->tree=(TREE **)malloc(sizeof(TREE *)*(node->branch)); |
| 1942 |
0 |
▶ | if(node->tree==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->tree==NULL) { |
0 |
0 |
0 |
|
| 1943 |
0 |
| error("load_tree", "Unable to allocate subtree"); |
| 1944 |
0 |
| return; |
| 1945 |
|
| } |
| 1946 |
|
| |
| 1947 |
0 |
▶ | if(level==0) progress("Loading tree", 0, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(level==0) progress("Loading tree", 0, 1); |
0 |
0 |
0 |
|
| 1948 |
0 |
▶ | for(i=0; i<node->branch; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<node->branch; ++i) { |
0 |
0 |
0 |
|
| 1949 |
0 |
| node->tree[i]=new_node(); |
| 1950 |
0 |
| ++level; |
| 1951 |
0 |
| load_tree(file, node->tree[i]); |
| 1952 |
0 |
| --level; |
| 1953 |
0 |
▶ | if(level==0) progress(NULL, i, node->branch); |
Branch: 1 branch
| Branch | True | False | Total |
| if(level==0) progress(NULL, i, node->branch); |
0 |
0 |
0 |
|
| 1954 |
|
| } |
| 1955 |
0 |
▶ | if(level==0) progress(NULL, 1, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(level==0) progress(NULL, 1, 1); |
0 |
0 |
0 |
|
| 1956 |
|
| } |
| 1957 |
|
| |
| 1958 |
|
| /*---------------------------------------------------------------------------*/ |
| 1959 |
|
| |
| 1960 |
|
| /* |
| 1961 |
|
| * Function: Load_Model |
| 1962 |
|
| * |
| 1963 |
|
| * Purpose: Load a model into memory. |
| 1964 |
|
| */ |
| 1965 |
1 |
| bool load_model(char *filename, MODEL *model) |
| 1966 |
|
| { |
| 1967 |
|
| FILE *file; |
| 1968 |
|
| char cookie[16]; |
| 1969 |
|
| |
| 1970 |
|
| |
| 1971 |
1 |
▶ | if(filename==NULL) return(FALSE); |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) return(FALSE); |
0 |
1 |
1 |
|
| 1972 |
|
| |
| 1973 |
1 |
| file=fopen(filename, "rb"); |
| 1974 |
|
| |
| 1975 |
1 |
▶ | if(file==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(file==NULL) { |
1 |
0 |
1 |
|
| 1976 |
1 |
| warn("load_model", "Unable to open file `%s'", filename); |
| 1977 |
1 |
| return(FALSE); |
| 1978 |
|
| } |
| 1979 |
|
| |
| 1980 |
|
| |
| 1981 |
0 |
| fread(cookie, sizeof(char), strlen(COOKIE), file); |
| 1982 |
0 |
▶ | if(strncmp(cookie, COOKIE, strlen(COOKIE))!=0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(strncmp(cookie, COOKIE, strlen(COOKIE))!=0) { |
0 |
0 |
0 |
|
| 1983 |
0 |
| warn("load_model", "File `%s' is not a MegaHAL brain", filename); |
| 1984 |
0 |
| goto fail; |
| 1985 |
|
| } |
| 1986 |
|
| |
| 1987 |
0 |
| fread(&(model->order), sizeof(BYTE1), 1, file); |
| 1988 |
0 |
| load_tree(file, model->forward); |
| 1989 |
0 |
| load_tree(file, model->backward); |
| 1990 |
0 |
| load_dictionary(file, model->dictionary); |
| 1991 |
|
| |
| 1992 |
0 |
| return(TRUE); |
| 1993 |
0 |
| fail: |
| 1994 |
0 |
| fclose(file); |
| 1995 |
|
| |
| 1996 |
0 |
| return(FALSE); |
| 1997 |
|
| } |
| 1998 |
|
| |
| 1999 |
|
| /*---------------------------------------------------------------------------*/ |
| 2000 |
|
| |
| 2001 |
|
| /* |
| 2002 |
|
| * Function: Make_Words |
| 2003 |
|
| * |
| 2004 |
|
| * Purpose: Break a string into an array of words. |
| 2005 |
|
| */ |
| 2006 |
350 |
| void make_words(char *input, DICTIONARY *words) |
| 2007 |
|
| { |
| 2008 |
350 |
| int offset=0; |
| 2009 |
|
| |
| 2010 |
|
| /* |
| 2011 |
|
| * Clear the entries in the dictionary |
| 2012 |
|
| */ |
| 2013 |
350 |
| free_dictionary(words); |
| 2014 |
|
| |
| 2015 |
|
| /* |
| 2016 |
|
| * If the string is empty then do nothing, for it contains no words. |
| 2017 |
|
| */ |
| 2018 |
350 |
▶ | if(strlen(input)==0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(strlen(input)==0) return; |
0 |
350 |
350 |
|
| 2019 |
|
| |
| 2020 |
|
| /* |
| 2021 |
|
| * Loop forever. |
| 2022 |
|
| */ |
| 2023 |
|
| while(1) { |
| 2024 |
|
| |
| 2025 |
|
| /* |
| 2026 |
|
| * If the current character is of the same type as the previous |
| 2027 |
|
| * character, then include it in the word. Otherwise, terminate |
| 2028 |
|
| * the current word. |
| 2029 |
|
| */ |
| 2030 |
27039 |
▶ | if(boundary(input, offset)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(boundary(input, offset)) { |
7272 |
19767 |
27039 |
|
| 2031 |
|
| /* |
| 2032 |
|
| * Add the word to the dictionary |
| 2033 |
|
| */ |
| 2034 |
7272 |
▶ | if(words->entry==NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(words->entry==NULL) |
350 |
6922 |
7272 |
|
| 2035 |
350 |
| words->entry=(STRING *)malloc((words->size+1)*sizeof(STRING)); |
| 2036 |
|
| else |
| 2037 |
6922 |
| words->entry=(STRING *)realloc(words->entry, (words->size+1)*sizeof(STRING)); |
| 2038 |
|
| |
| 2039 |
7272 |
▶ | if(words->entry==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(words->entry==NULL) { |
0 |
7272 |
7272 |
|
| 2040 |
0 |
| error("make_words", "Unable to reallocate dictionary"); |
| 2041 |
0 |
| return; |
| 2042 |
|
| } |
| 2043 |
|
| |
| 2044 |
7272 |
| words->entry[words->size].length=offset; |
| 2045 |
7272 |
| words->entry[words->size].word=input; |
| 2046 |
7272 |
| words->size+=1; |
| 2047 |
|
| |
| 2048 |
7272 |
▶ | if(offset==(int)strlen(input)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if(offset==(int)strlen(input)) break; |
350 |
6922 |
7272 |
|
| 2049 |
6922 |
| input+=offset; |
| 2050 |
6922 |
| offset=0; |
| 2051 |
|
| } else { |
| 2052 |
19767 |
| ++offset; |
| 2053 |
|
| } |
| 2054 |
|
| } |
| 2055 |
|
| |
| 2056 |
|
| /* |
| 2057 |
|
| * If the last word isn't punctuation, then replace it with a |
| 2058 |
|
| * full-stop character. |
| 2059 |
|
| */ |
| 2060 |
350 |
▶ | if(isalnum(words->entry[words->size-1].word[0])) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(isalnum(words->entry[words->size-1].word[0])) { |
0 |
350 |
350 |
|
| 2061 |
0 |
▶ | if(words->entry==NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(words->entry==NULL) |
0 |
0 |
0 |
|
| 2062 |
0 |
| words->entry=(STRING *)malloc((words->size+1)*sizeof(STRING)); |
| 2063 |
|
| else |
| 2064 |
0 |
| words->entry=(STRING *)realloc(words->entry, (words->size+1)*sizeof(STRING)); |
| 2065 |
0 |
▶ | if(words->entry==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(words->entry==NULL) { |
0 |
0 |
0 |
|
| 2066 |
0 |
| error("make_words", "Unable to reallocate dictionary"); |
| 2067 |
0 |
| return; |
| 2068 |
|
| } |
| 2069 |
|
| |
| 2070 |
0 |
| words->entry[words->size].length=1; |
| 2071 |
0 |
| words->entry[words->size].word="."; |
| 2072 |
0 |
| ++words->size; |
| 2073 |
|
| } |
| 2074 |
350 |
▶ | else if(strchr("!.?", words->entry[words->size-1].word[words->entry[words->size-1].length-1])==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if(strchr("!.?", words->entry[words->size-1].word[words->entry[words->size-1].length-1])==NULL) { |
1 |
349 |
350 |
|
| 2075 |
1 |
| words->entry[words->size-1].length=1; |
| 2076 |
1 |
| words->entry[words->size-1].word="."; |
| 2077 |
|
| } |
| 2078 |
|
| |
| 2079 |
350 |
| return; |
| 2080 |
|
| } |
| 2081 |
|
| |
| 2082 |
|
| /*---------------------------------------------------------------------------*/ |
| 2083 |
|
| /* |
| 2084 |
|
| * Function: Boundary |
| 2085 |
|
| * |
| 2086 |
|
| * Purpose: Return whether or not a word boundary exists in a string |
| 2087 |
|
| * at the specified location. |
| 2088 |
|
| */ |
| 2089 |
27039 |
| bool boundary(char *string, int position) |
| 2090 |
|
| { |
| 2091 |
27039 |
▶ | if(position==0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(position==0) |
7272 |
19767 |
27039 |
|
| 2092 |
7272 |
| return(FALSE); |
| 2093 |
|
| |
| 2094 |
19767 |
▶ | if(position==(int)strlen(string)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(position==(int)strlen(string)) |
350 |
19417 |
19767 |
|
| 2095 |
350 |
| return(TRUE); |
| 2096 |
|
| |
| 2097 |
19417 |
| if( |
| 2098 |
19417 |
▶ | (string[position]=='\'')&& |
Branch: 1 branch
| Branch | True | False | Total |
| (string[position]=='\'')&& |
99 |
19318 |
19417 |
|
| 2099 |
99 |
▶ | (isalpha(string[position-1])!=0)&& |
Branch: 1 branch
| Branch | True | False | Total |
| (isalpha(string[position-1])!=0)&& |
99 |
0 |
99 |
|
| 2100 |
99 |
▶ | (isalpha(string[position+1])!=0) |
Branch: 1 branch
| Branch | True | False | Total |
| (isalpha(string[position+1])!=0) |
99 |
0 |
99 |
|
| 2101 |
|
| ) |
| 2102 |
99 |
| return(FALSE); |
| 2103 |
|
| |
| 2104 |
19318 |
▶ | if( |
Branch: 1 branch
| Branch | True | False | Total |
| if( |
12413 |
6905 |
19318 |
|
| 2105 |
12413 |
| (position>1)&& |
| 2106 |
12413 |
▶ | (string[position-1]=='\'')&& |
Branch: 1 branch
| Branch | True | False | Total |
| (string[position-1]=='\'')&& |
99 |
12314 |
12413 |
|
| 2107 |
99 |
▶ | (isalpha(string[position-2])!=0)&& |
Branch: 1 branch
| Branch | True | False | Total |
| (isalpha(string[position-2])!=0)&& |
99 |
0 |
99 |
|
| 2108 |
99 |
▶ | (isalpha(string[position])!=0) |
Branch: 1 branch
| Branch | True | False | Total |
| (isalpha(string[position])!=0) |
99 |
0 |
99 |
|
| 2109 |
|
| ) |
| 2110 |
99 |
| return(FALSE); |
| 2111 |
|
| |
| 2112 |
19219 |
| if( |
| 2113 |
19219 |
▶ | (isalpha(string[position])!=0)&& |
Branch: 1 branch
| Branch | True | False | Total |
| (isalpha(string[position])!=0)&& |
15322 |
3897 |
19219 |
|
| 2114 |
15322 |
▶ | (isalpha(string[position-1])==0) |
Branch: 1 branch
| Branch | True | False | Total |
| (isalpha(string[position-1])==0) |
3282 |
12040 |
15322 |
|
| 2115 |
|
| ) |
| 2116 |
3282 |
| return(TRUE); |
| 2117 |
|
| |
| 2118 |
15937 |
| if( |
| 2119 |
15937 |
▶ | (isalpha(string[position])==0)&& |
Branch: 1 branch
| Branch | True | False | Total |
| (isalpha(string[position])==0)&& |
3897 |
12040 |
15937 |
|
| 2120 |
3897 |
▶ | (isalpha(string[position-1])!=0) |
Branch: 1 branch
| Branch | True | False | Total |
| (isalpha(string[position-1])!=0) |
3632 |
265 |
3897 |
|
| 2121 |
|
| ) |
| 2122 |
3632 |
| return(TRUE); |
| 2123 |
|
| |
| 2124 |
12305 |
▶ | if(isdigit(string[position])!=isdigit(string[position-1])) |
Branch: 1 branch
| Branch | True | False | Total |
| if(isdigit(string[position])!=isdigit(string[position-1])) |
8 |
12297 |
12305 |
|
| 2125 |
8 |
| return(TRUE); |
| 2126 |
|
| |
| 2127 |
12297 |
| return(FALSE); |
| 2128 |
|
| } |
| 2129 |
|
| |
| 2130 |
|
| /*---------------------------------------------------------------------------*/ |
| 2131 |
|
| /* |
| 2132 |
|
| * Function: Make_Greeting |
| 2133 |
|
| * |
| 2134 |
|
| * Purpose: Put some special words into the dictionary so that the |
| 2135 |
|
| * program will respond as if to a new judge. |
| 2136 |
|
| */ |
| 2137 |
0 |
| void make_greeting(DICTIONARY *words) |
| 2138 |
|
| { |
| 2139 |
|
| register int i; |
| 2140 |
|
| |
| 2141 |
0 |
▶ | for(i=0; i<words->size; ++i) free(words->entry[i].word); |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<words->size; ++i) free(words->entry[i].word); |
0 |
0 |
0 |
|
| 2142 |
0 |
| free_dictionary(words); |
| 2143 |
0 |
▶ | if(grt->size>0) (void)add_word(words, grt->entry[rnd(grt->size)]); |
Branch: 1 branch
| Branch | True | False | Total |
| if(grt->size>0) (void)add_word(words, grt->entry[rnd(grt->size)]); |
0 |
0 |
0 |
|
| 2144 |
0 |
| } |
| 2145 |
|
| |
| 2146 |
|
| /*---------------------------------------------------------------------------*/ |
| 2147 |
|
| /* |
| 2148 |
|
| * Function: Generate_Reply |
| 2149 |
|
| * |
| 2150 |
|
| * Purpose: Take a string of user input and return a string of output |
| 2151 |
|
| * which may vaguely be construed as containing a reply to |
| 2152 |
|
| * whatever is in the input string. |
| 2153 |
|
| */ |
| 2154 |
1 |
| char *generate_reply(MODEL *model, DICTIONARY *words) |
| 2155 |
|
| { |
| 2156 |
|
| static DICTIONARY *dummy=NULL; |
| 2157 |
|
| DICTIONARY *replywords; |
| 2158 |
|
| DICTIONARY *keywords; |
| 2159 |
|
| float surprise; |
| 2160 |
|
| float max_surprise; |
| 2161 |
|
| char *output; |
| 2162 |
|
| static char *output_none=NULL; |
| 2163 |
|
| int count; |
| 2164 |
|
| int basetime; |
| 2165 |
1 |
| int timeout = TIMEOUT; |
| 2166 |
|
| |
| 2167 |
|
| /* |
| 2168 |
|
| * Create an array of keywords from the words in the user's input |
| 2169 |
|
| */ |
| 2170 |
1 |
| keywords=make_keywords(model, words); |
| 2171 |
|
| |
| 2172 |
|
| /* |
| 2173 |
|
| * Make sure some sort of reply exists |
| 2174 |
|
| */ |
| 2175 |
1 |
▶ | if(output_none==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(output_none==NULL) { |
1 |
0 |
1 |
|
| 2176 |
1 |
| output_none=malloc(40); |
| 2177 |
1 |
▶ | if(output_none!=NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(output_none!=NULL) |
1 |
0 |
1 |
|
| 2178 |
1 |
| strcpy(output_none, "I don't know enough to answer you yet!"); |
| 2179 |
|
| } |
| 2180 |
1 |
| output=output_none; |
| 2181 |
1 |
▶ | if(dummy == NULL) dummy = new_dictionary(); |
Branch: 1 branch
| Branch | True | False | Total |
| if(dummy == NULL) dummy = new_dictionary(); |
1 |
0 |
1 |
|
| 2182 |
1 |
| replywords = reply(model, dummy); |
| 2183 |
1 |
▶ | if(dissimilar(words, replywords) == TRUE) output = make_output(replywords); |
Branch: 1 branch
| Branch | True | False | Total |
| if(dissimilar(words, replywords) == TRUE) output = make_output(replywords); |
1 |
0 |
1 |
|
| 2184 |
|
| |
| 2185 |
|
| /* |
| 2186 |
|
| * Loop for the specified waiting period, generating and evaluating |
| 2187 |
|
| * replies |
| 2188 |
|
| */ |
| 2189 |
1 |
| max_surprise=(float)-1.0; |
| 2190 |
1 |
| count=0; |
| 2191 |
1 |
| basetime=time(NULL); |
| 2192 |
|
| /* progress("Generating reply", 0, 1); */ |
| 2193 |
|
| do { |
| 2194 |
741 |
| replywords=reply(model, keywords); |
| 2195 |
741 |
| surprise=evaluate_reply(model, keywords, replywords); |
| 2196 |
741 |
| ++count; |
| 2197 |
741 |
▶ | if((surprise>max_surprise)&&(dissimilar(words, replywords)==TRUE)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if((surprise>max_surprise)&&(dissimilar(words, replywords)==TRUE)) { |
9 |
732 |
741 |
| if((surprise>max_surprise)&&(dissimilar(words, replywords)==TRUE)) { |
9 |
0 |
9 |
|
| 2198 |
9 |
| max_surprise=surprise; |
| 2199 |
9 |
| output=make_output(replywords); |
| 2200 |
|
| } |
| 2201 |
|
| /* progress(NULL, (time(NULL)-basetime),timeout); */ |
| 2202 |
741 |
▶ | } while((time(NULL)-basetime)<timeout); |
Branch: 1 branch
| Branch | True | False | Total |
| } while((time(NULL)-basetime)<timeout); |
740 |
1 |
741 |
|
| 2203 |
1 |
| progress(NULL, 1, 1); |
| 2204 |
|
| |
| 2205 |
|
| /* |
| 2206 |
|
| * Return the best answer we generated |
| 2207 |
|
| */ |
| 2208 |
1 |
| return(output); |
| 2209 |
|
| } |
| 2210 |
|
| |
| 2211 |
|
| /*---------------------------------------------------------------------------*/ |
| 2212 |
|
| |
| 2213 |
|
| /* |
| 2214 |
|
| * Function: Dissimilar |
| 2215 |
|
| * |
| 2216 |
|
| * Purpose: Return TRUE or FALSE depending on whether the dictionaries |
| 2217 |
|
| * are the same or not. |
| 2218 |
|
| */ |
| 2219 |
10 |
| bool dissimilar(DICTIONARY *words1, DICTIONARY *words2) |
| 2220 |
|
| { |
| 2221 |
|
| register int i; |
| 2222 |
|
| |
| 2223 |
10 |
▶ | if(words1->size!=words2->size) return(TRUE); |
Branch: 1 branch
| Branch | True | False | Total |
| if(words1->size!=words2->size) return(TRUE); |
10 |
0 |
10 |
|
| 2224 |
0 |
▶ | for(i=0; i<words1->size; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<words1->size; ++i) |
0 |
0 |
0 |
|
| 2225 |
0 |
▶ | if(wordcmp(words1->entry[i], words2->entry[i])!=0) return(TRUE); |
Branch: 1 branch
| Branch | True | False | Total |
| if(wordcmp(words1->entry[i], words2->entry[i])!=0) return(TRUE); |
0 |
0 |
0 |
|
| 2226 |
0 |
| return(FALSE); |
| 2227 |
|
| } |
| 2228 |
|
| |
| 2229 |
|
| /*---------------------------------------------------------------------------*/ |
| 2230 |
|
| |
| 2231 |
|
| /* |
| 2232 |
|
| * Function: Make_Keywords |
| 2233 |
|
| * |
| 2234 |
|
| * Purpose: Put all the interesting words from the user's input into |
| 2235 |
|
| * a keywords dictionary, which will be used when generating |
| 2236 |
|
| * a reply. |
| 2237 |
|
| */ |
| 2238 |
1 |
| DICTIONARY *make_keywords(MODEL *model, DICTIONARY *words) |
| 2239 |
|
| { |
| 2240 |
|
| static DICTIONARY *keys=NULL; |
| 2241 |
|
| register int i; |
| 2242 |
|
| register int j; |
| 2243 |
|
| int c; |
| 2244 |
|
| |
| 2245 |
1 |
▶ | if(keys==NULL) keys=new_dictionary(); |
Branch: 1 branch
| Branch | True | False | Total |
| if(keys==NULL) keys=new_dictionary(); |
1 |
0 |
1 |
|
| 2246 |
1 |
▶ | for(i=0; i<keys->size; ++i) free(keys->entry[i].word); |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<keys->size; ++i) free(keys->entry[i].word); |
0 |
1 |
1 |
|
| 2247 |
1 |
| free_dictionary(keys); |
| 2248 |
|
| |
| 2249 |
15 |
▶ | for(i=0; i<words->size; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<words->size; ++i) { |
14 |
1 |
15 |
|
| 2250 |
|
| /* |
| 2251 |
|
| * Find the symbol ID of the word. If it doesn't exist in |
| 2252 |
|
| * the model, or if it begins with a non-alphanumeric |
| 2253 |
|
| * character, or if it is in the exclusion array, then |
| 2254 |
|
| * skip over it. |
| 2255 |
|
| */ |
| 2256 |
14 |
| c=0; |
| 2257 |
14 |
▶ | for(j=0; j<swp->size; ++j) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<swp->size; ++j) |
0 |
14 |
14 |
|
| 2258 |
0 |
▶ | if(wordcmp(swp->from[j], words->entry[i])==0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(wordcmp(swp->from[j], words->entry[i])==0) { |
0 |
0 |
0 |
|
| 2259 |
0 |
| add_key(model, keys, swp->to[j]); |
| 2260 |
0 |
| ++c; |
| 2261 |
|
| } |
| 2262 |
14 |
▶ | if(c==0) add_key(model, keys, words->entry[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| if(c==0) add_key(model, keys, words->entry[i]); |
14 |
0 |
14 |
|
| 2263 |
|
| } |
| 2264 |
|
| |
| 2265 |
15 |
▶ | if(keys->size>0) for(i=0; i<words->size; ++i) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(keys->size>0) for(i=0; i<words->size; ++i) { |
1 |
0 |
1 |
| if(keys->size>0) for(i=0; i<words->size; ++i) { |
14 |
1 |
15 |
|
| 2266 |
|
| |
| 2267 |
14 |
| c=0; |
| 2268 |
14 |
▶ | for(j=0; j<swp->size; ++j) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<swp->size; ++j) |
0 |
14 |
14 |
|
| 2269 |
0 |
▶ | if(wordcmp(swp->from[j], words->entry[i])==0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(wordcmp(swp->from[j], words->entry[i])==0) { |
0 |
0 |
0 |
|
| 2270 |
0 |
| add_aux(model, keys, swp->to[j]); |
| 2271 |
0 |
| ++c; |
| 2272 |
|
| } |
| 2273 |
14 |
▶ | if(c==0) add_aux(model, keys, words->entry[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| if(c==0) add_aux(model, keys, words->entry[i]); |
14 |
0 |
14 |
|
| 2274 |
|
| } |
| 2275 |
|
| |
| 2276 |
1 |
| return(keys); |
| 2277 |
|
| } |
| 2278 |
|
| |
| 2279 |
|
| /*---------------------------------------------------------------------------*/ |
| 2280 |
|
| |
| 2281 |
|
| /* |
| 2282 |
|
| * Function: Add_Key |
| 2283 |
|
| * |
| 2284 |
|
| * Purpose: Add a word to the keyword dictionary. |
| 2285 |
|
| */ |
| 2286 |
14 |
| void add_key(MODEL *model, DICTIONARY *keys, STRING word) |
| 2287 |
|
| { |
| 2288 |
|
| int symbol; |
| 2289 |
|
| |
| 2290 |
14 |
| symbol=find_word(model->dictionary, word); |
| 2291 |
14 |
▶ | if(symbol==0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(symbol==0) return; |
0 |
14 |
14 |
|
| 2292 |
14 |
▶ | if(isalnum(word.word[0])==0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(isalnum(word.word[0])==0) return; |
7 |
7 |
14 |
|
| 2293 |
7 |
| symbol=find_word(ban, word); |
| 2294 |
7 |
▶ | if(symbol!=0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(symbol!=0) return; |
0 |
7 |
7 |
|
| 2295 |
7 |
| symbol=find_word(aux, word); |
| 2296 |
7 |
▶ | if(symbol!=0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(symbol!=0) return; |
0 |
7 |
7 |
|
| 2297 |
|
| |
| 2298 |
7 |
| add_word(keys, word); |
| 2299 |
|
| } |
| 2300 |
|
| |
| 2301 |
|
| /*---------------------------------------------------------------------------*/ |
| 2302 |
|
| |
| 2303 |
|
| /* |
| 2304 |
|
| * Function: Add_Aux |
| 2305 |
|
| * |
| 2306 |
|
| * Purpose: Add an auxilliary keyword to the keyword dictionary. |
| 2307 |
|
| */ |
| 2308 |
14 |
| void add_aux(MODEL *model, DICTIONARY *keys, STRING word) |
| 2309 |
|
| { |
| 2310 |
|
| int symbol; |
| 2311 |
|
| |
| 2312 |
14 |
| symbol=find_word(model->dictionary, word); |
| 2313 |
14 |
▶ | if(symbol==0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(symbol==0) return; |
0 |
14 |
14 |
|
| 2314 |
14 |
▶ | if(isalnum(word.word[0])==0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(isalnum(word.word[0])==0) return; |
7 |
7 |
14 |
|
| 2315 |
7 |
| symbol=find_word(aux, word); |
| 2316 |
7 |
▶ | if(symbol==0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(symbol==0) return; |
7 |
0 |
7 |
|
| 2317 |
|
| |
| 2318 |
0 |
| add_word(keys, word); |
| 2319 |
|
| } |
| 2320 |
|
| |
| 2321 |
|
| /*---------------------------------------------------------------------------*/ |
| 2322 |
|
| |
| 2323 |
|
| /* |
| 2324 |
|
| * Function: Reply |
| 2325 |
|
| * |
| 2326 |
|
| * Purpose: Generate a dictionary of reply words appropriate to the |
| 2327 |
|
| * given dictionary of keywords. |
| 2328 |
|
| */ |
| 2329 |
742 |
| DICTIONARY *reply(MODEL *model, DICTIONARY *keys) |
| 2330 |
|
| { |
| 2331 |
|
| static DICTIONARY *replies=NULL; |
| 2332 |
|
| register int i; |
| 2333 |
|
| int symbol; |
| 2334 |
742 |
| bool start=TRUE; |
| 2335 |
|
| |
| 2336 |
742 |
▶ | if(replies==NULL) replies=new_dictionary(); |
Branch: 1 branch
| Branch | True | False | Total |
| if(replies==NULL) replies=new_dictionary(); |
1 |
741 |
742 |
|
| 2337 |
742 |
| free_dictionary(replies); |
| 2338 |
|
| |
| 2339 |
|
| /* |
| 2340 |
|
| * Start off by making sure that the model's context is empty. |
| 2341 |
|
| */ |
| 2342 |
742 |
| initialize_context(model); |
| 2343 |
742 |
| model->context[0]=model->forward; |
| 2344 |
742 |
| used_key=FALSE; |
| 2345 |
|
| |
| 2346 |
|
| /* |
| 2347 |
|
| * Generate the reply in the forward direction. |
| 2348 |
|
| */ |
| 2349 |
|
| while(TRUE) { |
| 2350 |
|
| /* |
| 2351 |
|
| * Get a random symbol from the current context. |
| 2352 |
|
| */ |
| 2353 |
8376 |
▶ | if(start==TRUE) symbol=seed(model, keys); |
Branch: 1 branch
| Branch | True | False | Total |
| if(start==TRUE) symbol=seed(model, keys); |
742 |
7634 |
8376 |
|
| 2354 |
7634 |
| else symbol=babble(model, keys, replies); |
| 2355 |
8376 |
▶ | if((symbol==0)||(symbol==1)) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if((symbol==0)||(symbol==1)) break; |
8376 |
0 |
8376 |
| if((symbol==0)||(symbol==1)) break; |
7634 |
742 |
8376 |
|
| 2356 |
7634 |
| start=FALSE; |
| 2357 |
|
| |
| 2358 |
|
| /* |
| 2359 |
|
| * Append the symbol to the reply dictionary. |
| 2360 |
|
| */ |
| 2361 |
7634 |
▶ | if(replies->entry==NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(replies->entry==NULL) |
742 |
6892 |
7634 |
|
| 2362 |
742 |
| replies->entry=(STRING *)malloc((replies->size+1)*sizeof(STRING)); |
| 2363 |
|
| else |
| 2364 |
6892 |
| replies->entry=(STRING *)realloc(replies->entry, (replies->size+1)*sizeof(STRING)); |
| 2365 |
7634 |
▶ | if(replies->entry==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(replies->entry==NULL) { |
0 |
7634 |
7634 |
|
| 2366 |
0 |
| error("reply", "Unable to reallocate dictionary"); |
| 2367 |
0 |
| return(NULL); |
| 2368 |
|
| } |
| 2369 |
|
| |
| 2370 |
7634 |
| replies->entry[replies->size].length= |
| 2371 |
7634 |
| model->dictionary->entry[symbol].length; |
| 2372 |
7634 |
| replies->entry[replies->size].word= |
| 2373 |
7634 |
| model->dictionary->entry[symbol].word; |
| 2374 |
7634 |
| replies->size+=1; |
| 2375 |
|
| |
| 2376 |
|
| /* |
| 2377 |
|
| * Extend the current context of the model with the current symbol. |
| 2378 |
|
| */ |
| 2379 |
7634 |
| update_context(model, symbol); |
| 2380 |
|
| } |
| 2381 |
|
| |
| 2382 |
|
| /* |
| 2383 |
|
| * Start off by making sure that the model's context is empty. |
| 2384 |
|
| */ |
| 2385 |
742 |
| initialize_context(model); |
| 2386 |
742 |
| model->context[0]=model->backward; |
| 2387 |
|
| |
| 2388 |
|
| /* |
| 2389 |
|
| * Re-create the context of the model from the current reply |
| 2390 |
|
| * dictionary so that we can generate backwards to reach the |
| 2391 |
|
| * beginning of the string. |
| 2392 |
|
| */ |
| 2393 |
4504 |
▶ | if(replies->size>0) for(i=MIN(replies->size-1, model->order); i>=0; --i) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(replies->size>0) for(i=MIN(replies->size-1, model->order); i>=0; --i) { |
0 |
742 |
742 |
| if(replies->size>0) for(i=MIN(replies->size-1, model->order); i>=0; --i) { |
742 |
3762 |
4504 |
|
| 2394 |
3762 |
| symbol=find_word(model->dictionary, replies->entry[i]); |
| 2395 |
3762 |
| update_context(model, symbol); |
| 2396 |
|
| } |
| 2397 |
|
| |
| 2398 |
|
| /* |
| 2399 |
|
| * Generate the reply in the backward direction. |
| 2400 |
|
| */ |
| 2401 |
|
| while(TRUE) { |
| 2402 |
|
| /* |
| 2403 |
|
| * Get a random symbol from the current context. |
| 2404 |
|
| */ |
| 2405 |
8162 |
| symbol=babble(model, keys, replies); |
| 2406 |
8162 |
▶ | if((symbol==0)||(symbol==1)) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if((symbol==0)||(symbol==1)) break; |
8162 |
0 |
8162 |
| if((symbol==0)||(symbol==1)) break; |
7420 |
742 |
8162 |
|
| 2407 |
|
| |
| 2408 |
|
| /* |
| 2409 |
|
| * Prepend the symbol to the reply dictionary. |
| 2410 |
|
| */ |
| 2411 |
7420 |
▶ | if(replies->entry==NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(replies->entry==NULL) |
0 |
7420 |
7420 |
|
| 2412 |
0 |
| replies->entry=(STRING *)malloc((replies->size+1)*sizeof(STRING)); |
| 2413 |
|
| else |
| 2414 |
7420 |
| replies->entry=(STRING *)realloc(replies->entry, (replies->size+1)*sizeof(STRING)); |
| 2415 |
7420 |
▶ | if(replies->entry==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(replies->entry==NULL) { |
0 |
7420 |
7420 |
|
| 2416 |
0 |
| error("reply", "Unable to reallocate dictionary"); |
| 2417 |
0 |
| return(NULL); |
| 2418 |
|
| } |
| 2419 |
|
| |
| 2420 |
|
| /* |
| 2421 |
|
| * Shuffle everything up for the prepend. |
| 2422 |
|
| */ |
| 2423 |
116678 |
▶ | for(i=replies->size; i>0; --i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=replies->size; i>0; --i) { |
109258 |
7420 |
116678 |
|
| 2424 |
109258 |
| replies->entry[i].length=replies->entry[i-1].length; |
| 2425 |
109258 |
| replies->entry[i].word=replies->entry[i-1].word; |
| 2426 |
|
| } |
| 2427 |
|
| |
| 2428 |
7420 |
| replies->entry[0].length=model->dictionary->entry[symbol].length; |
| 2429 |
7420 |
| replies->entry[0].word=model->dictionary->entry[symbol].word; |
| 2430 |
7420 |
| replies->size+=1; |
| 2431 |
|
| |
| 2432 |
|
| /* |
| 2433 |
|
| * Extend the current context of the model with the current symbol. |
| 2434 |
|
| */ |
| 2435 |
7420 |
| update_context(model, symbol); |
| 2436 |
|
| } |
| 2437 |
|
| |
| 2438 |
742 |
| return(replies); |
| 2439 |
|
| } |
| 2440 |
|
| |
| 2441 |
|
| /*---------------------------------------------------------------------------*/ |
| 2442 |
|
| |
| 2443 |
|
| /* |
| 2444 |
|
| * Function: Evaluate_Reply |
| 2445 |
|
| * |
| 2446 |
|
| * Purpose: Measure the average surprise of keywords relative to the |
| 2447 |
|
| * language model. |
| 2448 |
|
| */ |
| 2449 |
741 |
| float evaluate_reply(MODEL *model, DICTIONARY *keys, DICTIONARY *words) |
| 2450 |
|
| { |
| 2451 |
|
| register int i; |
| 2452 |
|
| register int j; |
| 2453 |
|
| int symbol; |
| 2454 |
|
| float probability; |
| 2455 |
|
| int count; |
| 2456 |
741 |
| float entropy=(float)0.0; |
| 2457 |
|
| TREE *node; |
| 2458 |
741 |
| int num=0; |
| 2459 |
|
| |
| 2460 |
741 |
▶ | if(words->size<=0) return((float)0.0); |
Branch: 1 branch
| Branch | True | False | Total |
| if(words->size<=0) return((float)0.0); |
0 |
741 |
741 |
|
| 2461 |
741 |
| initialize_context(model); |
| 2462 |
741 |
| model->context[0]=model->forward; |
| 2463 |
15751 |
▶ | for(i=0; i<words->size; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<words->size; ++i) { |
15010 |
741 |
15751 |
|
| 2464 |
15010 |
| symbol=find_word(model->dictionary, words->entry[i]); |
| 2465 |
|
| |
| 2466 |
15010 |
▶ | if(find_word(keys, words->entry[i])!=0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(find_word(keys, words->entry[i])!=0) { |
3197 |
11813 |
15010 |
|
| 2467 |
3197 |
| probability=(float)0.0; |
| 2468 |
3197 |
| count=0; |
| 2469 |
3197 |
| ++num; |
| 2470 |
19182 |
▶ | for(j=0; j<model->order; ++j) if(model->context[j]!=NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| for(j=0; j<model->order; ++j) if(model->context[j]!=NULL) { |
15067 |
918 |
15985 |
| for(j=0; j<model->order; ++j) if(model->context[j]!=NULL) { |
15985 |
3197 |
19182 |
|
| 2471 |
|
| |
| 2472 |
15067 |
| node=find_symbol(model->context[j], symbol); |
| 2473 |
15067 |
| probability+=(float)(node->count)/ |
| 2474 |
15067 |
| (float)(model->context[j]->usage); |
| 2475 |
15067 |
| ++count; |
| 2476 |
|
| |
| 2477 |
|
| } |
| 2478 |
|
| |
| 2479 |
3197 |
▶ | if(count>0.0) entropy-=(float)log(probability/(float)count); |
Branch: 1 branch
| Branch | True | False | Total |
| if(count>0.0) entropy-=(float)log(probability/(float)count); |
3197 |
0 |
3197 |
|
| 2480 |
|
| } |
| 2481 |
|
| |
| 2482 |
15010 |
| update_context(model, symbol); |
| 2483 |
|
| } |
| 2484 |
|
| |
| 2485 |
741 |
| initialize_context(model); |
| 2486 |
741 |
| model->context[0]=model->backward; |
| 2487 |
15751 |
▶ | for(i=words->size-1; i>=0; --i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=words->size-1; i>=0; --i) { |
15010 |
741 |
15751 |
|
| 2488 |
15010 |
| symbol=find_word(model->dictionary, words->entry[i]); |
| 2489 |
|
| |
| 2490 |
15010 |
▶ | if(find_word(keys, words->entry[i])!=0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(find_word(keys, words->entry[i])!=0) { |
3197 |
11813 |
15010 |
|
| 2491 |
3197 |
| probability=(float)0.0; |
| 2492 |
3197 |
| count=0; |
| 2493 |
3197 |
| ++num; |
| 2494 |
19182 |
▶ | for(j=0; j<model->order; ++j) if(model->context[j]!=NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| for(j=0; j<model->order; ++j) if(model->context[j]!=NULL) { |
14201 |
1784 |
15985 |
| for(j=0; j<model->order; ++j) if(model->context[j]!=NULL) { |
15985 |
3197 |
19182 |
|
| 2495 |
|
| |
| 2496 |
14201 |
| node=find_symbol(model->context[j], symbol); |
| 2497 |
14201 |
| probability+=(float)(node->count)/ |
| 2498 |
14201 |
| (float)(model->context[j]->usage); |
| 2499 |
14201 |
| ++count; |
| 2500 |
|
| |
| 2501 |
|
| } |
| 2502 |
|
| |
| 2503 |
3197 |
▶ | if(count>0.0) entropy-=(float)log(probability/(float)count); |
Branch: 1 branch
| Branch | True | False | Total |
| if(count>0.0) entropy-=(float)log(probability/(float)count); |
3197 |
0 |
3197 |
|
| 2504 |
|
| } |
| 2505 |
|
| |
| 2506 |
15010 |
| update_context(model, symbol); |
| 2507 |
|
| } |
| 2508 |
|
| |
| 2509 |
741 |
▶ | if(num>=8) entropy/=(float)sqrt(num-1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(num>=8) entropy/=(float)sqrt(num-1); |
445 |
296 |
741 |
|
| 2510 |
741 |
▶ | if(num>=16) entropy/=(float)num; |
Branch: 1 branch
| Branch | True | False | Total |
| if(num>=16) entropy/=(float)num; |
0 |
741 |
741 |
|
| 2511 |
|
| |
| 2512 |
741 |
| return(entropy); |
| 2513 |
|
| } |
| 2514 |
|
| |
| 2515 |
|
| /*---------------------------------------------------------------------------*/ |
| 2516 |
|
| |
| 2517 |
|
| /* |
| 2518 |
|
| * Function: Make_Output |
| 2519 |
|
| * |
| 2520 |
|
| * Purpose: Generate a string from the dictionary of reply words. |
| 2521 |
|
| */ |
| 2522 |
10 |
| char *make_output(DICTIONARY *words) |
| 2523 |
|
| { |
| 2524 |
|
| static char *output=NULL; |
| 2525 |
|
| register int i; |
| 2526 |
|
| register int j; |
| 2527 |
|
| int length; |
| 2528 |
|
| static char *output_none=NULL; |
| 2529 |
|
| |
| 2530 |
10 |
▶ | if(output_none==NULL) output_none=malloc(40); |
Branch: 1 branch
| Branch | True | False | Total |
| if(output_none==NULL) output_none=malloc(40); |
1 |
9 |
10 |
|
| 2531 |
|
| |
| 2532 |
10 |
▶ | if(output==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(output==NULL) { |
1 |
9 |
10 |
|
| 2533 |
1 |
| output=(char *)malloc(sizeof(char)); |
| 2534 |
1 |
▶ | if(output==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(output==NULL) { |
0 |
1 |
1 |
|
| 2535 |
0 |
| error("make_output", "Unable to allocate output"); |
| 2536 |
0 |
| return(output_none); |
| 2537 |
|
| } |
| 2538 |
|
| } |
| 2539 |
|
| |
| 2540 |
10 |
▶ | if(words->size==0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(words->size==0) { |
0 |
10 |
10 |
|
| 2541 |
0 |
▶ | if(output_none!=NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(output_none!=NULL) |
0 |
0 |
0 |
|
| 2542 |
0 |
| strcpy(output_none, "I am utterly speechless!"); |
| 2543 |
0 |
| return(output_none); |
| 2544 |
|
| } |
| 2545 |
|
| |
| 2546 |
10 |
| length=1; |
| 2547 |
236 |
▶ | for(i=0; i<words->size; ++i) length+=words->entry[i].length; |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<words->size; ++i) length+=words->entry[i].length; |
226 |
10 |
236 |
|
| 2548 |
|
| |
| 2549 |
10 |
| output=(char *)realloc(output, sizeof(char)*length); |
| 2550 |
10 |
▶ | if(output==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(output==NULL) { |
0 |
10 |
10 |
|
| 2551 |
0 |
| error("make_output", "Unable to reallocate output."); |
| 2552 |
0 |
▶ | if(output_none!=NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(output_none!=NULL) |
0 |
0 |
0 |
|
| 2553 |
0 |
| strcpy(output_none, "I forgot what I was going to say!"); |
| 2554 |
0 |
| return(output_none); |
| 2555 |
|
| } |
| 2556 |
|
| |
| 2557 |
10 |
| length=0; |
| 2558 |
236 |
▶ | for(i=0; i<words->size; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<words->size; ++i) |
226 |
10 |
236 |
|
| 2559 |
821 |
▶ | for(j=0; j<words->entry[i].length; ++j) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<words->entry[i].length; ++j) |
595 |
226 |
821 |
|
| 2560 |
595 |
| output[length++]=words->entry[i].word[j]; |
| 2561 |
|
| |
| 2562 |
10 |
| output[length]='\0'; |
| 2563 |
|
| |
| 2564 |
10 |
| return(output); |
| 2565 |
|
| } |
| 2566 |
|
| |
| 2567 |
|
| /*---------------------------------------------------------------------------*/ |
| 2568 |
|
| |
| 2569 |
|
| /* |
| 2570 |
|
| * Function: Babble |
| 2571 |
|
| * |
| 2572 |
|
| * Purpose: Return a random symbol from the current context, or a |
| 2573 |
|
| * zero symbol identifier if we've reached either the |
| 2574 |
|
| * start or end of the sentence. Select the symbol based |
| 2575 |
|
| * on probabilities, favouring keywords. In all cases, |
| 2576 |
|
| * use the longest available context to choose the symbol. |
| 2577 |
|
| */ |
| 2578 |
15796 |
| int babble(MODEL *model, DICTIONARY *keys, DICTIONARY *words) |
| 2579 |
|
| { |
| 2580 |
|
| TREE *node; |
| 2581 |
|
| register int i; |
| 2582 |
|
| int count; |
| 2583 |
|
| int symbol; |
| 2584 |
|
| |
| 2585 |
|
| /* |
| 2586 |
|
| * Select the longest available context. |
| 2587 |
|
| */ |
| 2588 |
110572 |
▶ | for(i=0; i<=model->order; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<=model->order; ++i) |
94776 |
15796 |
110572 |
|
| 2589 |
94776 |
▶ | if(model->context[i]!=NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(model->context[i]!=NULL) |
86866 |
7910 |
94776 |
|
| 2590 |
86866 |
| node=model->context[i]; |
| 2591 |
|
| |
| 2592 |
15796 |
▶ | if(node->branch==0) return(0); |
Branch: 1 branch
| Branch | True | False | Total |
| if(node->branch==0) return(0); |
0 |
15796 |
15796 |
|
| 2593 |
|
| |
| 2594 |
|
| /* |
| 2595 |
|
| * Choose a symbol at random from this context. |
| 2596 |
|
| */ |
| 2597 |
15796 |
| i=rnd(node->branch); |
| 2598 |
15796 |
| count=rnd(node->usage); |
| 2599 |
39929 |
▶ | while(count>=0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(count>=0) { |
26467 |
13462 |
39929 |
|
| 2600 |
|
| /* |
| 2601 |
|
| * If the symbol occurs as a keyword, then use it. Only use an |
| 2602 |
|
| * auxilliary keyword if a normal keyword has already been used. |
| 2603 |
|
| */ |
| 2604 |
26467 |
| symbol=node->tree[i]->symbol; |
| 2605 |
|
| |
| 2606 |
26467 |
▶ | if( |
Branch: 1 branch
| Branch | True | False | Total |
| if( |
2567 |
23900 |
26467 |
|
| 2607 |
26467 |
| (find_word(keys, model->dictionary->entry[symbol])!=0)&& |
| 2608 |
3220 |
| ((used_key==TRUE)|| |
| 2609 |
3220 |
▶ | (find_word(aux, model->dictionary->entry[symbol])==0))&& |
Branch: 1 branch
| Branch | True | False | Total |
| (find_word(aux, model->dictionary->entry[symbol])==0))&& |
2334 |
233 |
2567 |
|
| 2610 |
2567 |
| (word_exists(words, model->dictionary->entry[symbol])==FALSE) |
| 2611 |
|
| ) { |
| 2612 |
2334 |
| used_key=TRUE; |
| 2613 |
2334 |
| break; |
| 2614 |
|
| } |
| 2615 |
24133 |
| count-=node->tree[i]->count; |
| 2616 |
24133 |
▶ | i=(i>=(node->branch-1))?0:i+1; |
Branch: 1 branch
| Branch | True | False | Total |
| i=(i>=(node->branch-1))?0:i+1; |
11388 |
12745 |
24133 |
|
| 2617 |
|
| } |
| 2618 |
|
| |
| 2619 |
15796 |
| return(symbol); |
| 2620 |
|
| } |
| 2621 |
|
| |
| 2622 |
|
| /*---------------------------------------------------------------------------*/ |
| 2623 |
|
| |
| 2624 |
|
| /* |
| 2625 |
|
| * Function: Word_Exists |
| 2626 |
|
| * |
| 2627 |
|
| * Purpose: A silly brute-force searcher for the reply string. |
| 2628 |
|
| */ |
| 2629 |
2567 |
| bool word_exists(DICTIONARY *dictionary, STRING word) |
| 2630 |
|
| { |
| 2631 |
|
| register int i; |
| 2632 |
|
| |
| 2633 |
19799 |
▶ | for(i=0; i<dictionary->size; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<dictionary->size; ++i) |
17465 |
2334 |
19799 |
|
| 2634 |
17465 |
▶ | if(wordcmp(dictionary->entry[i], word)==0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(wordcmp(dictionary->entry[i], word)==0) |
233 |
17232 |
17465 |
|
| 2635 |
233 |
| return(TRUE); |
| 2636 |
2334 |
| return(FALSE); |
| 2637 |
|
| } |
| 2638 |
|
| |
| 2639 |
|
| /*---------------------------------------------------------------------------*/ |
| 2640 |
|
| |
| 2641 |
|
| /* |
| 2642 |
|
| * Function: Seed |
| 2643 |
|
| * |
| 2644 |
|
| * Purpose: Seed the reply by guaranteeing that it contains a |
| 2645 |
|
| * keyword, if one exists. |
| 2646 |
|
| */ |
| 2647 |
742 |
| int seed(MODEL *model, DICTIONARY *keys) |
| 2648 |
|
| { |
| 2649 |
|
| register int i; |
| 2650 |
|
| int symbol; |
| 2651 |
|
| int stop; |
| 2652 |
|
| |
| 2653 |
|
| /* |
| 2654 |
|
| * Fix, thanks to Mark Tarrabain |
| 2655 |
|
| */ |
| 2656 |
742 |
▶ | if(model->context[0]->branch==0) symbol=0; |
Branch: 1 branch
| Branch | True | False | Total |
| if(model->context[0]->branch==0) symbol=0; |
0 |
742 |
742 |
|
| 2657 |
742 |
| else symbol=model->context[0]->tree[rnd(model->context[0]->branch)]->symbol; |
| 2658 |
|
| |
| 2659 |
742 |
▶ | if(keys->size>0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(keys->size>0) { |
741 |
1 |
742 |
|
| 2660 |
741 |
| i=rnd(keys->size); |
| 2661 |
741 |
| stop=i; |
| 2662 |
|
| while(TRUE) { |
| 2663 |
741 |
▶ | if( |
Branch: 1 branch
| Branch | True | False | Total |
| if( |
741 |
0 |
741 |
|
| 2664 |
1482 |
▶ | (find_word(model->dictionary, keys->entry[i])!=0)&& |
Branch: 1 branch
| Branch | True | False | Total |
| (find_word(model->dictionary, keys->entry[i])!=0)&& |
741 |
0 |
741 |
|
| 2665 |
741 |
| (find_word(aux, keys->entry[i])==0) |
| 2666 |
|
| ) { |
| 2667 |
741 |
| symbol=find_word(model->dictionary, keys->entry[i]); |
| 2668 |
741 |
| return(symbol); |
| 2669 |
|
| } |
| 2670 |
0 |
| ++i; |
| 2671 |
0 |
▶ | if(i==keys->size) i=0; |
Branch: 1 branch
| Branch | True | False | Total |
| if(i==keys->size) i=0; |
0 |
0 |
0 |
|
| 2672 |
0 |
▶ | if(i==stop) return(symbol); |
Branch: 1 branch
| Branch | True | False | Total |
| if(i==stop) return(symbol); |
0 |
0 |
0 |
|
| 2673 |
|
| } |
| 2674 |
|
| } |
| 2675 |
|
| |
| 2676 |
1 |
| return(symbol); |
| 2677 |
|
| } |
| 2678 |
|
| |
| 2679 |
|
| /*---------------------------------------------------------------------------*/ |
| 2680 |
|
| |
| 2681 |
|
| /* |
| 2682 |
|
| * Function: New_Swap |
| 2683 |
|
| * |
| 2684 |
|
| * Purpose: Allocate a new swap structure. |
| 2685 |
|
| */ |
| 2686 |
1 |
| SWAP *new_swap(void) |
| 2687 |
|
| { |
| 2688 |
|
| SWAP *list; |
| 2689 |
|
| |
| 2690 |
1 |
| list=(SWAP *)malloc(sizeof(SWAP)); |
| 2691 |
1 |
▶ | if(list==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(list==NULL) { |
0 |
1 |
1 |
|
| 2692 |
0 |
| error("new_swap", "Unable to allocate swap"); |
| 2693 |
0 |
| return(NULL); |
| 2694 |
|
| } |
| 2695 |
1 |
| list->size=0; |
| 2696 |
1 |
| list->from=NULL; |
| 2697 |
1 |
| list->to=NULL; |
| 2698 |
|
| |
| 2699 |
1 |
| return(list); |
| 2700 |
|
| } |
| 2701 |
|
| |
| 2702 |
|
| /*---------------------------------------------------------------------------*/ |
| 2703 |
|
| |
| 2704 |
|
| /* |
| 2705 |
|
| * Function: Add_Swap |
| 2706 |
|
| * |
| 2707 |
|
| * Purpose: Add a new entry to the swap structure. |
| 2708 |
|
| */ |
| 2709 |
0 |
| void add_swap(SWAP *list, char *s, char *d) |
| 2710 |
|
| { |
| 2711 |
0 |
| list->size+=1; |
| 2712 |
|
| |
| 2713 |
0 |
▶ | if(list->from==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(list->from==NULL) { |
0 |
0 |
0 |
|
| 2714 |
0 |
| list->from=(STRING *)malloc(sizeof(STRING)); |
| 2715 |
0 |
▶ | if(list->from==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(list->from==NULL) { |
0 |
0 |
0 |
|
| 2716 |
0 |
| error("add_swap", "Unable to allocate list->from"); |
| 2717 |
0 |
| return; |
| 2718 |
|
| } |
| 2719 |
|
| } |
| 2720 |
|
| |
| 2721 |
0 |
▶ | if(list->to==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(list->to==NULL) { |
0 |
0 |
0 |
|
| 2722 |
0 |
| list->to=(STRING *)malloc(sizeof(STRING)); |
| 2723 |
0 |
▶ | if(list->to==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(list->to==NULL) { |
0 |
0 |
0 |
|
| 2724 |
0 |
| error("add_swap", "Unable to allocate list->to"); |
| 2725 |
0 |
| return; |
| 2726 |
|
| } |
| 2727 |
|
| } |
| 2728 |
|
| |
| 2729 |
0 |
| list->from=(STRING *)realloc(list->from, sizeof(STRING)*(list->size)); |
| 2730 |
0 |
▶ | if(list->from==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(list->from==NULL) { |
0 |
0 |
0 |
|
| 2731 |
0 |
| error("add_swap", "Unable to reallocate from"); |
| 2732 |
0 |
| return; |
| 2733 |
|
| } |
| 2734 |
|
| |
| 2735 |
0 |
| list->to=(STRING *)realloc(list->to, sizeof(STRING)*(list->size)); |
| 2736 |
0 |
▶ | if(list->to==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(list->to==NULL) { |
0 |
0 |
0 |
|
| 2737 |
0 |
| error("add_swap", "Unable to reallocate to"); |
| 2738 |
0 |
| return; |
| 2739 |
|
| } |
| 2740 |
|
| |
| 2741 |
0 |
| list->from[list->size-1].length=strlen(s); |
| 2742 |
0 |
| list->from[list->size-1].word=strdup(s); |
| 2743 |
0 |
| list->to[list->size-1].length=strlen(d); |
| 2744 |
0 |
| list->to[list->size-1].word=strdup(d); |
| 2745 |
|
| } |
| 2746 |
|
| |
| 2747 |
|
| /*---------------------------------------------------------------------------*/ |
| 2748 |
|
| |
| 2749 |
|
| /* |
| 2750 |
|
| * Function: Initialize_Swap |
| 2751 |
|
| * |
| 2752 |
|
| * Purpose: Read a swap structure from a file. |
| 2753 |
|
| */ |
| 2754 |
1 |
| SWAP *initialize_swap(char *filename) |
| 2755 |
|
| { |
| 2756 |
|
| SWAP *list; |
| 2757 |
1 |
| FILE *file=NULL; |
| 2758 |
|
| char buffer[1024]; |
| 2759 |
|
| char *from; |
| 2760 |
|
| char *to; |
| 2761 |
|
| |
| 2762 |
1 |
| list=new_swap(); |
| 2763 |
|
| |
| 2764 |
1 |
▶ | if(filename==NULL) return(list); |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) return(list); |
0 |
1 |
1 |
|
| 2765 |
|
| |
| 2766 |
1 |
| file=fopen(filename, "r"); |
| 2767 |
1 |
▶ | if(file==NULL) return(list); |
Branch: 1 branch
| Branch | True | False | Total |
| if(file==NULL) return(list); |
1 |
0 |
1 |
|
| 2768 |
|
| |
| 2769 |
0 |
▶ | while(!feof(file)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(!feof(file)) { |
0 |
0 |
0 |
|
| 2770 |
|
| |
| 2771 |
0 |
▶ | if(fgets(buffer, 1024, file)==NULL) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if(fgets(buffer, 1024, file)==NULL) break; |
0 |
0 |
0 |
|
| 2772 |
0 |
▶ | if(buffer[0]=='#') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if(buffer[0]=='#') continue; |
0 |
0 |
0 |
|
| 2773 |
0 |
| from=strtok(buffer, "\t "); |
| 2774 |
0 |
| to=strtok(NULL, "\t \n#"); |
| 2775 |
|
| |
| 2776 |
0 |
| add_swap(list, from, to); |
| 2777 |
|
| } |
| 2778 |
|
| |
| 2779 |
0 |
| fclose(file); |
| 2780 |
0 |
| return(list); |
| 2781 |
|
| } |
| 2782 |
|
| |
| 2783 |
|
| /*---------------------------------------------------------------------------*/ |
| 2784 |
|
| |
| 2785 |
1 |
| void free_swap(SWAP *swap) |
| 2786 |
|
| { |
| 2787 |
|
| register int i; |
| 2788 |
|
| |
| 2789 |
1 |
▶ | if(swap==NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(swap==NULL) return; |
1 |
0 |
1 |
|
| 2790 |
|
| |
| 2791 |
0 |
▶ | for(i=0; i<swap->size; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<swap->size; ++i) { |
0 |
0 |
0 |
|
| 2792 |
0 |
| free_word(swap->from[i]); |
| 2793 |
0 |
| free_word(swap->to[i]); |
| 2794 |
|
| } |
| 2795 |
0 |
| free(swap->from); |
| 2796 |
0 |
| free(swap->to); |
| 2797 |
0 |
| free(swap); |
| 2798 |
|
| } |
| 2799 |
|
| |
| 2800 |
|
| /*---------------------------------------------------------------------------*/ |
| 2801 |
|
| |
| 2802 |
|
| /* |
| 2803 |
|
| * Function: Initialize_List |
| 2804 |
|
| * |
| 2805 |
|
| * Purpose: Read a dictionary from a file. |
| 2806 |
|
| */ |
| 2807 |
3 |
| DICTIONARY *initialize_list(char *filename) |
| 2808 |
|
| { |
| 2809 |
|
| DICTIONARY *list; |
| 2810 |
3 |
| FILE *file=NULL; |
| 2811 |
|
| STRING word; |
| 2812 |
|
| char *string; |
| 2813 |
|
| char buffer[1024]; |
| 2814 |
|
| |
| 2815 |
3 |
| list=new_dictionary(); |
| 2816 |
|
| |
| 2817 |
3 |
▶ | if(filename==NULL) return(list); |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) return(list); |
0 |
3 |
3 |
|
| 2818 |
|
| |
| 2819 |
3 |
| file=fopen(filename, "r"); |
| 2820 |
3 |
▶ | if(file==NULL) return(list); |
Branch: 1 branch
| Branch | True | False | Total |
| if(file==NULL) return(list); |
3 |
0 |
3 |
|
| 2821 |
|
| |
| 2822 |
0 |
▶ | while(!feof(file)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(!feof(file)) { |
0 |
0 |
0 |
|
| 2823 |
|
| |
| 2824 |
0 |
▶ | if(fgets(buffer, 1024, file)==NULL) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if(fgets(buffer, 1024, file)==NULL) break; |
0 |
0 |
0 |
|
| 2825 |
0 |
▶ | if(buffer[0]=='#') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if(buffer[0]=='#') continue; |
0 |
0 |
0 |
|
| 2826 |
0 |
| string=strtok(buffer, "\t \n#"); |
| 2827 |
|
| |
| 2828 |
0 |
▶ | if((string!=NULL)&&(strlen(string)>0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if((string!=NULL)&&(strlen(string)>0)) { |
0 |
0 |
0 |
| if((string!=NULL)&&(strlen(string)>0)) { |
0 |
0 |
0 |
|
| 2829 |
0 |
| word.length=strlen(string); |
| 2830 |
0 |
| word.word=strdup(buffer); |
| 2831 |
0 |
| add_word(list, word); |
| 2832 |
|
| } |
| 2833 |
|
| } |
| 2834 |
|
| |
| 2835 |
0 |
| fclose(file); |
| 2836 |
0 |
| return(list); |
| 2837 |
|
| } |
| 2838 |
|
| |
| 2839 |
|
| /*---------------------------------------------------------------------------*/ |
| 2840 |
|
| |
| 2841 |
|
| /* |
| 2842 |
|
| * Function: Delay |
| 2843 |
|
| * |
| 2844 |
|
| * Purpose: Display the string to stdout as if it was typed by a human. |
| 2845 |
|
| */ |
| 2846 |
0 |
| void delay(char *string) |
| 2847 |
|
| { |
| 2848 |
|
| register int i; |
| 2849 |
|
| |
| 2850 |
|
| /* |
| 2851 |
|
| * Don't simulate typing if the feature is turned off |
| 2852 |
|
| */ |
| 2853 |
0 |
▶ | if(typing_delay==FALSE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(typing_delay==FALSE) { |
0 |
0 |
0 |
|
| 2854 |
0 |
| fprintf(stdout, string); |
| 2855 |
0 |
| return; |
| 2856 |
|
| } |
| 2857 |
|
| |
| 2858 |
|
| /* |
| 2859 |
|
| * Display the entire string, one character at a time |
| 2860 |
|
| */ |
| 2861 |
0 |
▶ | for(i=0; i<(int)strlen(string)-1; ++i) typein(string[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<(int)strlen(string)-1; ++i) typein(string[i]); |
0 |
0 |
0 |
|
| 2862 |
0 |
| usleep((D_THINK+rnd(V_THINK)-rnd(V_THINK))/2); |
| 2863 |
0 |
| typein(string[i]); |
| 2864 |
|
| } |
| 2865 |
|
| |
| 2866 |
|
| /*---------------------------------------------------------------------------*/ |
| 2867 |
|
| |
| 2868 |
|
| /* |
| 2869 |
|
| * Function: Typein |
| 2870 |
|
| * |
| 2871 |
|
| * Purpose: Display a character to stdout as if it was typed by a human. |
| 2872 |
|
| */ |
| 2873 |
0 |
| void typein(char c) |
| 2874 |
|
| { |
| 2875 |
|
| /* |
| 2876 |
|
| * Standard keyboard delay |
| 2877 |
|
| */ |
| 2878 |
0 |
| usleep(D_KEY+rnd(V_KEY)-rnd(V_KEY)); |
| 2879 |
0 |
| fprintf(stdout, "%c", c); |
| 2880 |
0 |
| fflush(stdout); |
| 2881 |
|
| |
| 2882 |
|
| /* |
| 2883 |
|
| * A random thinking delay |
| 2884 |
|
| */ |
| 2885 |
0 |
▶ | if((!isalnum(c))&&((rnd(100))<P_THINK)) |
Branch: 2 branches
| Branch | True | False | Total |
| if((!isalnum(c))&&((rnd(100))<P_THINK)) |
0 |
0 |
0 |
| if((!isalnum(c))&&((rnd(100))<P_THINK)) |
0 |
0 |
0 |
|
| 2886 |
0 |
| usleep(D_THINK+rnd(V_THINK)-rnd(V_THINK)); |
| 2887 |
0 |
| } |
| 2888 |
|
| |
| 2889 |
|
| /*---------------------------------------------------------------------------*/ |
| 2890 |
|
| |
| 2891 |
|
| /* |
| 2892 |
|
| * Function: Ignore |
| 2893 |
|
| * |
| 2894 |
|
| * Purpose: Log the occurrence of a signal, but ignore it. |
| 2895 |
|
| */ |
| 2896 |
1 |
| void ignore(int sig) |
| 2897 |
|
| { |
| 2898 |
1 |
▶ | if(sig!=0) warn("ignore", "MegaHAL received signal %d", sig); |
Branch: 1 branch
| Branch | True | False | Total |
| if(sig!=0) warn("ignore", "MegaHAL received signal %d", sig); |
0 |
1 |
1 |
|
| 2899 |
|
| |
| 2900 |
|
| #if !defined(DOS) |
| 2901 |
|
| // signal(SIGINT, saveandexit); |
| 2902 |
|
| // signal(SIGILL, die); |
| 2903 |
|
| // signal(SIGSEGV, die); |
| 2904 |
|
| #endif |
| 2905 |
|
| // signal(SIGFPE, die); |
| 2906 |
1 |
| } |
| 2907 |
|
| |
| 2908 |
|
| |
| 2909 |
|
| /*---------------------------------------------------------------------------*/ |
| 2910 |
|
| |
| 2911 |
|
| /* |
| 2912 |
|
| * Function: Die |
| 2913 |
|
| * |
| 2914 |
|
| * Purpose: Log the occurrence of a signal, and exit. |
| 2915 |
|
| */ |
| 2916 |
0 |
| void die(int sig) |
| 2917 |
|
| { |
| 2918 |
0 |
| error("die", "MegaHAL received signal %d", sig); |
| 2919 |
0 |
| exithal(); |
| 2920 |
0 |
| } |
| 2921 |
|
| |
| 2922 |
|
| /*---------------------------------------------------------------------------*/ |
| 2923 |
|
| |
| 2924 |
|
| /* |
| 2925 |
|
| * Function: Rnd |
| 2926 |
|
| * |
| 2927 |
|
| * Purpose: Return a random integer between 0 and range-1. |
| 2928 |
|
| */ |
| 2929 |
33075 |
| int rnd(int range) |
| 2930 |
|
| { |
| 2931 |
|
| static bool flag=FALSE; |
| 2932 |
|
| |
| 2933 |
33075 |
▶ | if(flag==FALSE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(flag==FALSE) { |
1 |
33074 |
33075 |
|
| 2934 |
|
| #if defined(DOS) || defined(__mac_os) || defined(_MSC_VER) || defined(__MINGW32_VERSION) |
| 2935 |
|
| srand(time(NULL)); |
| 2936 |
|
| #else |
| 2937 |
1 |
| srand48(time(NULL)); |
| 2938 |
|
| #endif |
| 2939 |
|
| } |
| 2940 |
33075 |
| flag=TRUE; |
| 2941 |
|
| #if defined(DOS) || defined(__mac_os) || defined(_MSC_VER) || defined(__MINGW32_VERSION) |
| 2942 |
|
| return(rand()%range); |
| 2943 |
|
| #else |
| 2944 |
33075 |
| return(floor(drand48()*(double)(range))); |
| 2945 |
|
| #endif |
| 2946 |
|
| } |
| 2947 |
|
| |
| 2948 |
|
| /*---------------------------------------------------------------------------*/ |
| 2949 |
|
| |
| 2950 |
|
| /* |
| 2951 |
|
| * Function: Usleep |
| 2952 |
|
| * |
| 2953 |
|
| * Purpose: Simulate the Un*x function usleep. Necessary because |
| 2954 |
|
| * Microsoft provide no similar function. Performed via |
| 2955 |
|
| * a busy loop, which unnecessarily chews up the CPU. |
| 2956 |
|
| * But Windows '95 isn't properly multitasking anyway, so |
| 2957 |
|
| * no-one will notice. Modified from a real Microsoft |
| 2958 |
|
| * example, believe it or not! |
| 2959 |
|
| */ |
| 2960 |
|
| #if defined(DOS) || defined(__mac_os) |
| 2961 |
|
| void usleep(int period) |
| 2962 |
|
| { |
| 2963 |
|
| clock_t goal; |
| 2964 |
|
| |
| 2965 |
|
| goal=(clock_t)(period*CLOCKS_PER_SEC)/(clock_t)1000000+clock(); |
| 2966 |
|
| while(goal>clock()); |
| 2967 |
|
| } |
| 2968 |
|
| #endif |
| 2969 |
|
| |
| 2970 |
|
| /*---------------------------------------------------------------------------*/ |
| 2971 |
|
| |
| 2972 |
|
| /* |
| 2973 |
|
| * Function: Strdup |
| 2974 |
|
| * |
| 2975 |
|
| * Purpose: Provide the strdup() function for Macintosh. |
| 2976 |
|
| */ |
| 2977 |
|
| #ifdef __mac_os |
| 2978 |
|
| char *strdup(const char *str) |
| 2979 |
|
| { |
| 2980 |
|
| char *rval=(char *)malloc(strlen(str)+1); |
| 2981 |
|
| |
| 2982 |
|
| if(rval!=NULL) strcpy(rval, str); |
| 2983 |
|
| |
| 2984 |
|
| return(rval); |
| 2985 |
|
| } |
| 2986 |
|
| #endif |
| 2987 |
|
| |
| 2988 |
|
| /*---------------------------------------------------------------------------*/ |
| 2989 |
|
| |
| 2990 |
|
| /* |
| 2991 |
|
| * Function: Initialize_Speech |
| 2992 |
|
| * |
| 2993 |
|
| * Purpose: Initialize speech output. |
| 2994 |
|
| */ |
| 2995 |
|
| #ifdef __mac_os |
| 2996 |
|
| bool initialize_speech(void) |
| 2997 |
|
| { |
| 2998 |
|
| bool speechExists = false; |
| 2999 |
|
| long response; |
| 3000 |
|
| OSErr err; |
| 3001 |
|
| |
| 3002 |
|
| err = Gestalt(gestaltSpeechAttr, &response); |
| 3003 |
|
| |
| 3004 |
|
| if(!err) { |
| 3005 |
|
| if(response & (1L << gestaltSpeechMgrPresent)) { |
| 3006 |
|
| speechExists = true; |
| 3007 |
|
| } |
| 3008 |
|
| } |
| 3009 |
|
| return speechExists; |
| 3010 |
|
| } |
| 3011 |
|
| #endif |
| 3012 |
|
| |
| 3013 |
|
| /*---------------------------------------------------------------------------*/ |
| 3014 |
|
| |
| 3015 |
|
| /* |
| 3016 |
|
| * Function: changevoice |
| 3017 |
|
| * |
| 3018 |
|
| * Purpose: change voice of speech output. |
| 3019 |
|
| */ |
| 3020 |
0 |
| void changevoice(DICTIONARY* words, int position) |
| 3021 |
|
| { |
| 3022 |
|
| #ifdef __mac_os |
| 3023 |
|
| register int i, index; |
| 3024 |
|
| STRING word={ 1, "#" }; |
| 3025 |
|
| char buffer[80]; |
| 3026 |
|
| VoiceSpec voiceSpec; |
| 3027 |
|
| VoiceDescription info; |
| 3028 |
|
| short count, voiceCount; |
| 3029 |
|
| unsigned char* temp; |
| 3030 |
|
| OSErr err; |
| 3031 |
|
| /* |
| 3032 |
|
| * If there is less than 4 words, no voice specified. |
| 3033 |
|
| */ |
| 3034 |
|
| if(words->size<=4) return; |
| 3035 |
|
| |
| 3036 |
|
| for(i=0; i<words->size-4; ++i) |
| 3037 |
|
| if(wordcmp(word, words->entry[i])==0) { |
| 3038 |
|
| |
| 3039 |
|
| err = CountVoices(&voiceCount); |
| 3040 |
|
| if (!err && voiceCount) { |
| 3041 |
|
| for (count = 1; count <= voiceCount; count++) { |
| 3042 |
|
| err = GetIndVoice(count, &voiceSpec); |
| 3043 |
|
| if (err) continue; |
| 3044 |
|
| err = GetVoiceDescription(&voiceSpec, &info, |
| 3045 |
|
| sizeof(VoiceDescription)); |
| 3046 |
|
| if (err) continue; |
| 3047 |
|
| |
| 3048 |
|
| |
| 3049 |
|
| for (temp= info.name; *temp; temp++) { |
| 3050 |
|
| if (*temp == ' ') |
| 3051 |
|
| *temp = '_'; |
| 3052 |
|
| } |
| 3053 |
|
| |
| 3054 |
|
| /* |
| 3055 |
|
| * skip command and get voice name |
| 3056 |
|
| */ |
| 3057 |
|
| index = i + 3; |
| 3058 |
|
| strcpy(buffer, words->entry[index].word); |
| 3059 |
|
| c2pstr(buffer); |
| 3060 |
|
| // compare ignoring case |
| 3061 |
|
| if (EqualString((StringPtr)buffer, info.name, false, false)) { |
| 3062 |
|
| if (gSpeechChannel) { |
| 3063 |
|
| StopSpeech(gSpeechChannel); |
| 3064 |
|
| DisposeSpeechChannel(gSpeechChannel); |
| 3065 |
|
| gSpeechChannel = nil; |
| 3066 |
|
| } |
| 3067 |
|
| err = NewSpeechChannel(&voiceSpec, &gSpeechChannel); |
| 3068 |
|
| if (!err) { |
| 3069 |
|
| p2cstr((StringPtr)buffer); |
| 3070 |
|
| printf("Now using %s voice\n", buffer); |
| 3071 |
|
| c2pstr(buffer); |
| 3072 |
|
| err = SpeakText(gSpeechChannel, &buffer[1], buffer[0]); |
| 3073 |
|
| } |
| 3074 |
|
| } |
| 3075 |
|
| } |
| 3076 |
|
| } |
| 3077 |
|
| } |
| 3078 |
|
| #endif |
| 3079 |
0 |
| } |
| 3080 |
|
| |
| 3081 |
|
| /*---------------------------------------------------------------------------*/ |
| 3082 |
|
| |
| 3083 |
|
| /* |
| 3084 |
|
| * Function: listvoices |
| 3085 |
|
| * |
| 3086 |
|
| * Purpose: Display the names of voices for speech output. |
| 3087 |
|
| */ |
| 3088 |
0 |
| void listvoices(void) |
| 3089 |
|
| { |
| 3090 |
|
| #ifdef __mac_os |
| 3091 |
|
| VoiceSpec voiceSpec; |
| 3092 |
|
| VoiceDescription info; |
| 3093 |
|
| short count, voiceCount; |
| 3094 |
|
| unsigned char* temp; |
| 3095 |
|
| OSErr err; |
| 3096 |
|
| |
| 3097 |
|
| if(gSpeechExists) { |
| 3098 |
|
| err = CountVoices(&voiceCount); |
| 3099 |
|
| if (!err && voiceCount) { |
| 3100 |
|
| for (count = 1; count <= voiceCount; count++) { |
| 3101 |
|
| err = GetIndVoice(count, &voiceSpec); |
| 3102 |
|
| if (err) continue; |
| 3103 |
|
| |
| 3104 |
|
| err = GetVoiceDescription(&voiceSpec, &info, |
| 3105 |
|
| sizeof(VoiceDescription)); |
| 3106 |
|
| if (err) continue; |
| 3107 |
|
| |
| 3108 |
|
| p2cstr(info.name); |
| 3109 |
|
| for (temp= info.name; *temp; temp++) |
| 3110 |
|
| if (*temp == ' ') |
| 3111 |
|
| *temp = '_'; |
| 3112 |
|
| printf("%s\n",info.name); |
| 3113 |
|
| } |
| 3114 |
|
| } |
| 3115 |
|
| } |
| 3116 |
|
| #endif |
| 3117 |
0 |
| } |
| 3118 |
|
| |
| 3119 |
|
| /*---------------------------------------------------------------------------*/ |
| 3120 |
|
| |
| 3121 |
|
| /* |
| 3122 |
|
| * Function: Speak |
| 3123 |
|
| */ |
| 3124 |
0 |
| void speak(char *output) |
| 3125 |
|
| { |
| 3126 |
0 |
▶ | if(speech==FALSE) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(speech==FALSE) return; |
0 |
0 |
0 |
|
| 3127 |
|
| #ifdef __mac_os |
| 3128 |
|
| if(gSpeechExists) { |
| 3129 |
|
| OSErr err; |
| 3130 |
|
| |
| 3131 |
|
| if (gSpeechChannel) |
| 3132 |
|
| err = SpeakText(gSpeechChannel, output, strlen(output)); |
| 3133 |
|
| else { |
| 3134 |
|
| c2pstr(output); |
| 3135 |
|
| SpeakString((StringPtr)output); |
| 3136 |
|
| p2cstr((StringPtr)output); |
| 3137 |
|
| } |
| 3138 |
|
| } |
| 3139 |
|
| #endif |
| 3140 |
|
| } |
| 3141 |
|
| |
| 3142 |
|
| /*---------------------------------------------------------------------------*/ |
| 3143 |
|
| |
| 3144 |
|
| /* |
| 3145 |
|
| * Function: Progress |
| 3146 |
|
| * |
| 3147 |
|
| * Purpose: Display a progress indicator as a percentage. |
| 3148 |
|
| */ |
| 3149 |
4310 |
| bool progress(char *message, int done, int total) |
| 3150 |
|
| { |
| 3151 |
|
| static int last=0; |
| 3152 |
|
| static bool first=FALSE; |
| 3153 |
|
| |
| 3154 |
|
| /* |
| 3155 |
|
| * We have already hit 100%, and a newline has been printed, so nothing |
| 3156 |
|
| * needs to be done. |
| 3157 |
|
| */ |
| 3158 |
4310 |
▶ | if((done*100/total==100)&&(first==FALSE)) return(TRUE); |
Branch: 2 branches
| Branch | True | False | Total |
| if((done*100/total==100)&&(first==FALSE)) return(TRUE); |
6 |
4304 |
4310 |
| if((done*100/total==100)&&(first==FALSE)) return(TRUE); |
2 |
4 |
6 |
|
| 3159 |
|
| |
| 3160 |
|
| /* |
| 3161 |
|
| * Nothing has changed since the last time this function was called, |
| 3162 |
|
| * so do nothing, unless it's the first time! |
| 3163 |
|
| */ |
| 3164 |
4308 |
▶ | if(done*100/total==last) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(done*100/total==last) { |
3908 |
400 |
4308 |
|
| 3165 |
3908 |
▶ | if((done==0)&&(first==FALSE)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if((done==0)&&(first==FALSE)) { |
7 |
3901 |
3908 |
| if((done==0)&&(first==FALSE)) { |
4 |
3 |
7 |
|
| 3166 |
|
| // fprintf(stderr, "%s: %3d%%", message, done*100/total); |
| 3167 |
4 |
| first=TRUE; |
| 3168 |
|
| } |
| 3169 |
3908 |
| return(TRUE); |
| 3170 |
|
| } |
| 3171 |
|
| |
| 3172 |
|
| /* |
| 3173 |
|
| * Erase what we printed last time, and print the new percentage. |
| 3174 |
|
| */ |
| 3175 |
400 |
| last=done*100/total; |
| 3176 |
|
| |
| 3177 |
|
| //if(done>0) fprintf(stderr, "%c%c%c%c", 8, 8, 8, 8); |
| 3178 |
|
| //fprintf(stderr, "%3d%%", done*100/total); |
| 3179 |
|
| |
| 3180 |
|
| /* |
| 3181 |
|
| * We have hit 100%, so reset static variables and print a newline. |
| 3182 |
|
| */ |
| 3183 |
400 |
▶ | if(last==100) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(last==100) { |
4 |
396 |
400 |
|
| 3184 |
4 |
| first=FALSE; |
| 3185 |
4 |
| last=0; |
| 3186 |
|
| //fprintf(stderr, "\n"); |
| 3187 |
|
| } |
| 3188 |
|
| |
| 3189 |
400 |
| return(TRUE); |
| 3190 |
|
| } |
| 3191 |
|
| |
| 3192 |
|
| /*---------------------------------------------------------------------------*/ |
| 3193 |
|
| |
| 3194 |
0 |
| void help(void) |
| 3195 |
|
| { |
| 3196 |
|
| int j; |
| 3197 |
|
| |
| 3198 |
0 |
▶ | for(j=0; j<COMMAND_SIZE; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<COMMAND_SIZE; ++j) { |
0 |
0 |
0 |
|
| 3199 |
0 |
| printf("#%-7s: %s\n", command[j].word.word, command[j].helpstring); |
| 3200 |
|
| } |
| 3201 |
0 |
| } |
| 3202 |
|
| |
| 3203 |
|
| /*---------------------------------------------------------------------------*/ |
| 3204 |
|
| |
| 3205 |
1 |
| void load_personality(MODEL **model) |
| 3206 |
|
| { |
| 3207 |
|
| FILE *file; |
| 3208 |
|
| static char *filename=NULL; |
| 3209 |
|
| |
| 3210 |
1 |
▶ | if(filename==NULL) filename=(char *)malloc(sizeof(char)*1); |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) filename=(char *)malloc(sizeof(char)*1); |
1 |
0 |
1 |
|
| 3211 |
|
| |
| 3212 |
|
| /* |
| 3213 |
|
| * Allocate memory for the filename |
| 3214 |
|
| */ |
| 3215 |
1 |
| filename=(char *)realloc(filename, |
| 3216 |
1 |
| sizeof(char)*(strlen(directory)+strlen(SEP)+12)); |
| 3217 |
1 |
▶ | if(filename==NULL) error("load_personality","Unable to allocate filename"); |
Branch: 1 branch
| Branch | True | False | Total |
| if(filename==NULL) error("load_personality","Unable to allocate filename"); |
0 |
1 |
1 |
|
| 3218 |
|
| |
| 3219 |
|
| /* |
| 3220 |
|
| * Check to see if the brain exists |
| 3221 |
|
| */ |
| 3222 |
|
| |
| 3223 |
1 |
▶ | if(strcmp(directory, DEFAULT)!=0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(strcmp(directory, DEFAULT)!=0) { |
0 |
1 |
1 |
|
| 3224 |
0 |
| sprintf(filename, "%s%smegahal.brn", directory, SEP); |
| 3225 |
0 |
| file=fopen(filename, "r"); |
| 3226 |
0 |
▶ | if(file==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(file==NULL) { |
0 |
0 |
0 |
|
| 3227 |
0 |
| sprintf(filename, "%s%smegahal.trn", directory, SEP); |
| 3228 |
0 |
| file=fopen(filename, "r"); |
| 3229 |
0 |
▶ | if(file==NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(file==NULL) { |
0 |
0 |
0 |
|
| 3230 |
0 |
| fprintf(stdout, "Unable to change MegaHAL personality to \"%s\".\n" |
| 3231 |
|
| "Reverting to MegaHAL personality \"%s\".\n", directory, last); |
| 3232 |
0 |
| free(directory); |
| 3233 |
0 |
| directory=strdup(last); |
| 3234 |
0 |
| return; |
| 3235 |
|
| } |
| 3236 |
|
| } |
| 3237 |
0 |
| fclose(file); |
| 3238 |
0 |
| fprintf(stdout, "Changing to MegaHAL personality \"%s\".\n", directory); |
| 3239 |
|
| } |
| 3240 |
|
| |
| 3241 |
|
| /* |
| 3242 |
|
| * Free the current personality |
| 3243 |
|
| */ |
| 3244 |
1 |
| free_model(*model); |
| 3245 |
1 |
| free_words(ban); |
| 3246 |
1 |
| free_dictionary(ban); |
| 3247 |
1 |
| free_words(aux); |
| 3248 |
1 |
| free_dictionary(aux); |
| 3249 |
1 |
| free_words(grt); |
| 3250 |
1 |
| free_dictionary(grt); |
| 3251 |
1 |
| free_swap(swp); |
| 3252 |
|
| |
| 3253 |
|
| /* |
| 3254 |
|
| * Create a language model. |
| 3255 |
|
| */ |
| 3256 |
1 |
| *model=new_model(order); |
| 3257 |
|
| |
| 3258 |
|
| /* |
| 3259 |
|
| * Train the model on a text if one exists |
| 3260 |
|
| */ |
| 3261 |
|
| |
| 3262 |
1 |
| sprintf(filename, "%s%smegahal.brn", directory, SEP); |
| 3263 |
1 |
▶ | if(load_model(filename, *model)==FALSE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(load_model(filename, *model)==FALSE) { |
1 |
0 |
1 |
|
| 3264 |
1 |
| sprintf(filename, "%s%smegahal.trn", directory, SEP); |
| 3265 |
1 |
| train(*model, filename); |
| 3266 |
|
| } |
| 3267 |
|
| |
| 3268 |
|
| /* |
| 3269 |
|
| * Read a dictionary containing banned keywords, auxiliary keywords, |
| 3270 |
|
| * greeting keywords and swap keywords |
| 3271 |
|
| */ |
| 3272 |
1 |
| sprintf(filename, "%s%smegahal.ban", directory, SEP); |
| 3273 |
1 |
| ban=initialize_list(filename); |
| 3274 |
1 |
| sprintf(filename, "%s%smegahal.aux", directory, SEP); |
| 3275 |
1 |
| aux=initialize_list(filename); |
| 3276 |
1 |
| sprintf(filename, "%s%smegahal.grt", directory, SEP); |
| 3277 |
1 |
| grt=initialize_list(filename); |
| 3278 |
1 |
| sprintf(filename, "%s%smegahal.swp", directory, SEP); |
| 3279 |
1 |
| swp=initialize_swap(filename); |
| 3280 |
|
| } |
| 3281 |
|
| |
| 3282 |
|
| /*---------------------------------------------------------------------------*/ |
| 3283 |
|
| |
| 3284 |
1 |
| void change_personality(DICTIONARY *command, int position, MODEL **model) |
| 3285 |
|
| { |
| 3286 |
|
| |
| 3287 |
1 |
▶ | if(directory == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(directory == NULL) { |
1 |
0 |
1 |
|
| 3288 |
1 |
| directory = (char *)malloc(sizeof(char)*(strlen(DEFAULT)+1)); |
| 3289 |
1 |
▶ | if(directory == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(directory == NULL) { |
0 |
1 |
1 |
|
| 3290 |
0 |
| error("change_personality", "Unable to allocate directory"); |
| 3291 |
|
| } else { |
| 3292 |
1 |
| strcpy(directory, DEFAULT); |
| 3293 |
|
| } |
| 3294 |
|
| } |
| 3295 |
|
| |
| 3296 |
1 |
▶ | if(last == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(last == NULL) { |
1 |
0 |
1 |
|
| 3297 |
1 |
| last = strdup(directory); |
| 3298 |
|
| } |
| 3299 |
|
| |
| 3300 |
1 |
▶ | if((command == NULL)||((position+2)>=command->size)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if((command == NULL)||((position+2)>=command->size)) { |
0 |
1 |
1 |
| if((command == NULL)||((position+2)>=command->size)) { |
0 |
0 |
0 |
|
| 3301 |
|
| /* no dir set, so we leave it to whatever was set above */ |
| 3302 |
|
| } else { |
| 3303 |
0 |
| directory=(char *)realloc(directory, |
| 3304 |
0 |
| sizeof(char)*(command->entry[position+2].length+1)); |
| 3305 |
0 |
▶ | if(directory == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(directory == NULL) |
0 |
0 |
0 |
|
| 3306 |
0 |
| error("change_personality", "Unable to allocate directory"); |
| 3307 |
0 |
| strncpy(directory, command->entry[position+2].word, |
| 3308 |
0 |
| command->entry[position+2].length); |
| 3309 |
0 |
| directory[command->entry[position+2].length]='\0'; |
| 3310 |
|
| } |
| 3311 |
|
| |
| 3312 |
1 |
| load_personality(model); |
| 3313 |
1 |
| } |
| 3314 |
|
| |
| 3315 |
|
| /*---------------------------------------------------------------------------*/ |
| 3316 |
|
| |
| 3317 |
3 |
| void free_words(DICTIONARY *words) |
| 3318 |
|
| { |
| 3319 |
|
| register int i; |
| 3320 |
|
| |
| 3321 |
3 |
▶ | if(words == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(words == NULL) return; |
3 |
0 |
3 |
|
| 3322 |
|
| |
| 3323 |
0 |
▶ | if(words->entry != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(words->entry != NULL) |
0 |
0 |
0 |
|
| 3324 |
0 |
▶ | for(i=0; i<words->size; ++i) free_word(words->entry[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<words->size; ++i) free_word(words->entry[i]); |
0 |
0 |
0 |
|
| 3325 |
|
| } |
| 3326 |
|
| |
| 3327 |
|
| /*---------------------------------------------------------------------------*/ |
| 3328 |
|
| |
| 3329 |
0 |
| void free_word(STRING word) |
| 3330 |
|
| { |
| 3331 |
0 |
| free(word.word); |
| 3332 |
0 |
| } |
| 3333 |
|
| |
| 3334 |
|
| /*===========================================================================*/ |
| 3335 |
|
| |
| 3336 |
|
| /* |
| 3337 |
|
| * $Log: megahal.c,v $ |
| 3338 |
|
| * Revision 1.6 2002/10/16 04:32:53 davidw |
| 3339 |
|
| * * megahal.c (change_personality): [ 541667 ] Added patch from Andrew |
| 3340 |
|
| * Burke to rework logic in change_personality. |
| 3341 |
|
| * |
| 3342 |
|
| * * megahal.c: Trailing white space cleanup. |
| 3343 |
|
| * |
| 3344 |
|
| * * python-interface.c: [ 546397 ] Change python include path to a more |
| 3345 |
|
| * recent version. This should really be fixed by using some of |
| 3346 |
|
| * Python's build automation tools. |
| 3347 |
|
| * |
| 3348 |
|
| * Revision 1.5 2000/11/08 11:07:11 davidw |
| 3349 |
|
| * Moved README to docs directory. |
| 3350 |
|
| * |
| 3351 |
|
| * Changes to debian files. |
| 3352 |
|
| * |
| 3353 |
|
| * Revision 1.4 2000/09/07 21:51:12 davidw |
| 3354 |
|
| * Created some library functions that I think are workable, and moved |
| 3355 |
|
| * everything else into megahal.c as static variables/functions. |
| 3356 |
|
| * |
| 3357 |
|
| * Revision 1.3 2000/09/07 11:43:43 davidw |
| 3358 |
|
| * Started hacking: |
| 3359 |
|
| * |
| 3360 |
|
| * Reduced makefile targets, eliminating non-Linux OS's. There should be |
| 3361 |
|
| * a cleaner way to do this. |
| 3362 |
|
| * |
| 3363 |
|
| * Added Tcl and Python C level interfaces. |
| 3364 |
|
| * |
| 3365 |
|
| * Revision 1.23 1998/05/19 03:02:02 hutch |
| 3366 |
|
| * Removed a small malloc() bug, and added a progress display for |
| 3367 |
|
| * generate_reply(). |
| 3368 |
|
| * |
| 3369 |
|
| * Revision 1.22 1998/04/24 03:47:03 hutch |
| 3370 |
|
| * Quick bug fix to get sunos version to work. |
| 3371 |
|
| * |
| 3372 |
|
| * Revision 1.21 1998/04/24 03:39:51 hutch |
| 3373 |
|
| * Added the BRAIN command, to allow user to change MegaHAL personalities |
| 3374 |
|
| * on the fly. |
| 3375 |
|
| * |
| 3376 |
|
| * Revision 1.20 1998/04/22 07:12:37 hutch |
| 3377 |
|
| * A few small changes to get the DOS version to compile. |
| 3378 |
|
| * |
| 3379 |
|
| * Revision 1.19 1998/04/21 10:10:56 hutch |
| 3380 |
|
| * Fixed a few little errors. |
| 3381 |
|
| * |
| 3382 |
|
| * Revision 1.18 1998/04/06 08:02:01 hutch |
| 3383 |
|
| * Added debugging stuff, courtesy of Paul Baxter. |
| 3384 |
|
| * |
| 3385 |
|
| * Revision 1.17 1998/04/02 01:34:20 hutch |
| 3386 |
|
| * Added the help function and fixed a few errors. |
| 3387 |
|
| * |
| 3388 |
|
| * Revision 1.16 1998/04/01 05:42:57 hutch |
| 3389 |
|
| * Incorporated Mac code, including speech synthesis, and attempted |
| 3390 |
|
| * to tidy up the code for multi-platform support. |
| 3391 |
|
| * |
| 3392 |
|
| * Revision 1.15 1998/03/27 03:43:15 hutch |
| 3393 |
|
| * Added AMIGA specific changes, thanks to Dag Agren. |
| 3394 |
|
| * |
| 3395 |
|
| * Revision 1.14 1998/02/20 06:40:13 hutch |
| 3396 |
|
| * Tidied up transcript file format. |
| 3397 |
|
| * |
| 3398 |
|
| * Revision 1.13 1998/02/20 06:26:19 hutch |
| 3399 |
|
| * Fixed random number generator and Seed() function (thanks to Mark |
| 3400 |
|
| * Tarrabain), removed redundant code left over from the Loebner entry, |
| 3401 |
|
| * prettied things up a little and destroyed several causes of memory |
| 3402 |
|
| * leakage (although probably not all). |
| 3403 |
|
| * |
| 3404 |
|
| * Revision 1.12 1998/02/04 02:55:11 hutch |
| 3405 |
|
| * Fixed up memory allocation error which caused SunOS versions to crash. |
| 3406 |
|
| * |
| 3407 |
|
| * Revision 1.11 1998/01/22 03:16:30 hutch |
| 3408 |
|
| * Fixed several memory leaks, and the frustrating bug in the |
| 3409 |
|
| * Write_Input routine. |
| 3410 |
|
| * |
| 3411 |
|
| * Revision 1.10 1998/01/19 06:44:36 hutch |
| 3412 |
|
| * Fixed MegaHAL to compile under Linux with a small patch credited |
| 3413 |
|
| * to Joey Hess (joey@kitenet.net). MegaHAL may now be included as |
| 3414 |
|
| * part of the Debian Linux distribution. |
| 3415 |
|
| * |
| 3416 |
|
| * Revision 1.9 1998/01/19 06:37:32 hutch |
| 3417 |
|
| * Fixed a minor bug with end-of-sentence punctuation. |
| 3418 |
|
| * |
| 3419 |
|
| * Revision 1.8 1997/12/24 03:17:01 hutch |
| 3420 |
|
| * More bug fixes, and hopefully the final contest version! |
| 3421 |
|
| * |
| 3422 |
|
| * Revision 1.7 1997/12/22 13:18:09 hutch |
| 3423 |
|
| * A few more bug fixes, and non-repeating implemented. |
| 3424 |
|
| * |
| 3425 |
|
| * Revision 1.6 1997/12/22 04:27:04 hutch |
| 3426 |
|
| * A few minor bug fixes. |
| 3427 |
|
| * |
| 3428 |
|
| * Revision 1.5 1997/12/15 04:35:59 hutch |
| 3429 |
|
| * Final Loebner version! |
| 3430 |
|
| * |
| 3431 |
|
| * Revision 1.4 1997/12/11 05:45:29 hutch |
| 3432 |
|
| * The almost finished version. |
| 3433 |
|
| * |
| 3434 |
|
| * Revision 1.3 1997/12/10 09:08:09 hutch |
| 3435 |
|
| * Now Loebner complient (tm). |
| 3436 |
|
| * |
| 3437 |
|
| * Revision 1.2 1997/12/08 06:22:32 hutch |
| 3438 |
|
| * Tidied up. |
| 3439 |
|
| * |
| 3440 |
|
| * Revision 1.1 1997/12/05 07:11:44 hutch |
| 3441 |
|
| * Initial revision (lots of files were merged into one, RCS re-started) |
| 3442 |
|
| * |
| 3443 |
|
| * Revision 1.7 1997/12/04 07:07:13 hutch |
| 3444 |
|
| * Added load and save functions, and tidied up some code. |
| 3445 |
|
| * |
| 3446 |
|
| * Revision 1.6 1997/12/02 08:34:47 hutch |
| 3447 |
|
| * Added the ban, aux and swp functions. |
| 3448 |
|
| * |
| 3449 |
|
| * Revision 1.5 1997/12/02 06:03:04 hutch |
| 3450 |
|
| * Updated to use a special terminating symbol, and to store only |
| 3451 |
|
| * branches of maximum depth, as they are the only ones used in |
| 3452 |
|
| * the reply. |
| 3453 |
|
| * |
| 3454 |
|
| * Revision 1.4 1997/10/28 09:23:12 hutch |
| 3455 |
|
| * MegaHAL is babbling nicely, but without keywords. |
| 3456 |
|
| * |
| 3457 |
|
| * Revision 1.3 1997/10/15 09:04:03 hutch |
| 3458 |
|
| * MegaHAL can parrot back whatever the user says. |
| 3459 |
|
| * |
| 3460 |
|
| * Revision 1.2 1997/07/21 04:03:28 hutch |
| 3461 |
|
| * Fully working. |
| 3462 |
|
| * |
| 3463 |
|
| * Revision 1.1 1997/07/15 01:55:25 hutch |
| 3464 |
|
| * Initial revision. |
| 3465 |
|
| */ |
| 3466 |
|
| |
| 3467 |
|
| /*===========================================================================*/ |
| 3468 |
|
| |