1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
|
/*===========================================================================*/
/*
* Copyright (C) 1997 Jason Hutchens
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the license or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the Gnu Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*===========================================================================*/
/*
* $Id: megahal.c,v 1.8 1997/12/24 03:17:01 hutch Exp hutch $
*
* File: megahal.c
*
* Program: MegaHAL v8
*
* Purpose: To simulate a natural language conversation with a psychotic
* computer. This is achieved by learning from the user's
* input using a third-order Markov model on the word level.
* Words are considered to be sequences of characters separated
* by whitespace and punctuation. Replies are generated
* randomly based on a keyword, and they are scored using
* measures of surprise.
*
* Author: Mr. Jason L. Hutchens
*
* WWW: http://ciips.ee.uwa.edu.au/~hutch/hal/
*
* E-Mail: hutch@ciips.ee.uwa.edu.au
*
* Contact: The Centre for Intelligent Information Processing Systems
* Department of Electrical and Electronic Engineering
* The University of Western Australia
* AUSTRALIA 6907
*
* Phone: +61-8-9380-3856
*
* Facsimile: +61-8-9380-1168
*
* Notes: This file is best viewed with tabstops set to three spaces.
*/
/*===========================================================================*/
#include "hx_types.h"
#include "xmalloc.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
extern void colourz (char *);
extern void curscr_printf (const char *fmt, ...);
/*===========================================================================*/
#define P_THINK 40
#define D_KEY 100000
#define V_KEY 50000
#define D_THINK 500000
#define V_THINK 250000
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define COOKIE "MegaHALv8"
/*===========================================================================*/
typedef enum {FALSE, TRUE} bool;
typedef struct {
u_int8_t length;
char *word;
} STRING;
typedef struct {
u_int32_t size;
STRING *entry;
u_int16_t *index;
} DICTIONARY;
typedef struct {
u_int16_t size;
STRING *from;
STRING *to;
} SWAP;
typedef struct NODE {
u_int16_t symbol;
u_int32_t usage;
u_int16_t count;
u_int16_t branch;
struct NODE **tree;
} TREE;
typedef struct {
u_int8_t order;
TREE *forward;
TREE *backward;
TREE **context;
DICTIONARY *dictionary;
} MODEL;
/*===========================================================================*/
void add_aux (MODEL *, DICTIONARY *, STRING);
void add_key (MODEL *, DICTIONARY *, STRING);
void add_node (TREE *, TREE *, int);
void add_swap (SWAP *, char *, char *);
TREE *add_symbol (TREE *, u_int16_t);
u_int16_t add_word (DICTIONARY *, STRING);
int babble (MODEL *, DICTIONARY *, DICTIONARY *);
bool boundary (char *, unsigned int);
void capitalize (char *);
void delay (char *);
bool dissimilar (DICTIONARY *, DICTIONARY *);
void error (const char *, const char *, ...);
float evaluate_reply (MODEL *, DICTIONARY *, DICTIONARY *);
void exithal (void);
TREE *find_symbol (TREE *, int);
TREE *find_symbol_add (TREE *, int);
u_int16_t find_word (DICTIONARY *, STRING);
void free_dictionary (DICTIONARY *);
char *generate_reply (MODEL *, DICTIONARY *);
void initialize_context (MODEL *);
void initialize_dictionary (DICTIONARY *);
void initialize_error (const char *);
DICTIONARY *initialize_list (const char *);
void initialize_status (const char *);
SWAP *initialize_swap (const char *);
void learn (MODEL *, DICTIONARY *);
void load_dictionary (FILE *, DICTIONARY *);
bool load_model (const char *, MODEL *);
void load_tree (FILE *, TREE *);
void load_word (FILE *, DICTIONARY *);
void make_greeting (DICTIONARY *);
DICTIONARY *make_keywords (MODEL *, DICTIONARY *);
char *make_output (DICTIONARY *);
void make_words (char *, DICTIONARY *);
DICTIONARY *new_dictionary (void);
MODEL *new_model (int);
TREE *new_node (void);
SWAP *new_swap (void);
void print_header (FILE *);
DICTIONARY *reply (MODEL *, DICTIONARY *);
void save_dictionary (FILE *, DICTIONARY *);
void save_model (const char *, MODEL *);
void save_tree (FILE *, TREE *);
void save_word (FILE *, STRING);
int search_dictionary (DICTIONARY *, STRING, bool *);
int search_node (TREE *, int, bool *);
int seed (MODEL *, DICTIONARY *);
void show_dictionary (DICTIONARY *);
void status (const char *, ...);
void train (MODEL *, const char *);
void update_context (MODEL *, int);
void update_model (MODEL *, int);
void upper (char *);
int wordcmp (STRING, STRING);
bool word_exists (DICTIONARY *, STRING);
void write_input (char *, char *);
void write_output (char *);
/*===========================================================================*/
#define ORDER 5
#define TIMEOUT 2
int hal_active = 0;
bool used_key, typing_delay = TRUE;
DICTIONARY *g_words = 0, *g_ban = 0, *g_aux = 0, *g_grt = 0;
MODEL *g_model = 0;
SWAP *g_swp = 0;
FILE *errorfp, *statusfp;
int cmd_hal (int, char *const *);
void hal_rcv (char *, char *);
#include <getopt.h>
static struct option hal_opts[] = {
{"delay", 0, 0, 'd'},
{"quit", 0, 0, 'q'},
{"restart", 0, 0, 'r'},
{"save", 0, 0, 's'},
{0, 0, 0, 0}
};
int
cmd_hal (int argc, char *const *argv)
{
int longind, o;
if (argc < 2) {
usage:
curscr_printf("\nusage: %s [--save] [--delay] [--restart] [--quit]", argv[0]);
return 0;
}
optind = 0;
while ((o = getopt_long(argc, argv, "dqrs", hal_opts, &longind)) != EOF) {
if (o == 0)
o = hal_opts[longind].val;
switch (o) {
case 's':
if (g_model) {
save_model("megahal.brn", g_model);
make_greeting(g_words);
}
break;
case 'd':
typing_delay = typing_delay == TRUE ? FALSE : TRUE;
break;
case 'r':
hal_active = 1;
break;
case 'q':
if (g_model)
save_model("megahal.brn", g_model);
exithal();
break;
default:
goto usage;
}
}
return 0;
}
void
hal_rcv (char *input_p, char *user)
{
char *output, *input = xstrdup(input_p);
if (!g_words) {
errorfp = stderr;
statusfp = stdout;
/*
* Create a dictionary which will be used to hold the segmented
* version of the user's input.
*/
g_words = new_dictionary();
/*
* Do some initialisation
*/
initialize_error("megahal.log");
srandom(time(0) + clock() + getpid());
/*
* Create a language model.
*/
g_model = new_model(ORDER);
/*
* Train the model on a text if one exists
*/
if (load_model("megahal.brn", g_model) == FALSE)
train(g_model, "megahal.trn");
/*
* Read a dictionary containing banned keywords, auxiliary keywords,
* greeting keywords and swap keywords
*/
g_ban = initialize_list("megahal.ban");
g_aux = initialize_list("megahal.aux");
g_grt = initialize_list("megahal.grt");
g_swp = initialize_swap("megahal.swp");
initialize_status("megahal.txt");
}
write_input(input, user);
upper(input);
make_words(input, g_words);
learn(g_model, g_words);
output = generate_reply(g_model, g_words);
write_output(output);
xfree(output);
xfree(input);
}
/*---------------------------------------------------------------------------*/
/*
* Function: exithal
*
* Purpose: Terminate the program.
*/
void
exithal (void)
{
hal_active = 0;
}
/*---------------------------------------------------------------------------*/
/*
* Function: initialize_error
*
* Purpose: Close the current error file pointer, and open a new one.
*/
void
initialize_error (const char *filename)
{
if (errorfp != stderr)
fclose(errorfp);
if (!filename || !(errorfp = fopen(filename, "a"))) {
errorfp = stderr;
return;
}
print_header(errorfp);
}
/*---------------------------------------------------------------------------*/
/*
* Function: error
*
* Purpose: Print the specified message to the error file.
*/
void
error (const char *title, const char *fmt, ...)
{
va_list argp;
fprintf(errorfp, "%s: ", title);
va_start(argp, fmt);
vfprintf(errorfp, fmt, argp);
va_end(argp);
fprintf(errorfp, ".\n");
fflush(errorfp);
}
/*---------------------------------------------------------------------------*/
/*
* Function: initialize_status
*
* Purpose: Close the current status file pointer, and open a new one.
*/
void
initialize_status (const char *filename)
{
if (statusfp != stdout)
fclose(statusfp);
if (!filename || !(statusfp = fopen(filename, "a"))) {
statusfp = stdout;
return;
}
print_header(statusfp);
}
/*---------------------------------------------------------------------------*/
/*
* Function: status
*
* Purpose Print the specified message to the status file.
*/
void
status (const char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
vfprintf(statusfp, fmt, argp);
va_end(argp);
fflush(statusfp);
}
/*---------------------------------------------------------------------------*/
/*
* Function: print_header
*
* Purpose: Display a copyright message and timestamp.
*/
void
print_header (FILE *fp)
{
time_t t;
char timestamp[1024];
struct tm *local;
t = time(0);
local = localtime(&t);
strftime(timestamp, 1024, "Start at: [%Y/%m/%d %H:%M:%S]", local);
fprintf(fp, "(c)1998 Cambridge Center For Behavioral Studies all rights reserved\n"
"[MegaHALv8][Jason Hutchens]\n"
"%s\n", timestamp);
fflush(fp);
}
/*---------------------------------------------------------------------------*/
/*
* Function: write_output
*
* Purpose: Display the output string.
*/
void
write_output (char *output)
{
time_t t;
char timestamp[1024];
struct tm *local;
capitalize(output);
t = time(0);
local = localtime(&t);
strftime(timestamp, 1024, "HAL[%H:%M:%S]", local);
delay(output);
status("%s %s\n", timestamp, output);
}
/*---------------------------------------------------------------------------*/
/*
* Function: capitalize
*
* Purpose: Convert a string to look nice.
*/
void
capitalize (char *str)
{
register unsigned int i;
bool start = TRUE;
for (i = 0; i < 3 && str[i]; i++)
if (isalpha(str[i])) {
if (start == TRUE)
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
start = FALSE;
}
for (i = 3; str[i]; i++) {
if (isalpha(str[i])) {
if (start == TRUE)
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
start = FALSE;
}
if (isspace(str[i])) {
switch (str[i - 1]) {
case '?':
case '.':
case '!':
start = TRUE;
}
} else {
switch (str[i]) {
case '"':
start = TRUE;
}
}
}
}
/*---------------------------------------------------------------------------*/
/*
* Function: upper
*
* Purpose: Convert a string to its uppercase representation.
*/
void
upper (char *str)
{
register unsigned int i;
for (i = 0; str[i]; i++)
str[i] = toupper(str[i]);
}
/*---------------------------------------------------------------------------*/
/*
* Function: write_input
*
* Purpose: Log the user's input
*/
void
write_input (char *input, char *user)
{
time_t t;
char timestamp[1024], tmp[1024];
struct tm *local;
t = time(0);
local = localtime(&t);
strftime(tmp, 1024, "[%H:%M:%S]", local);
sprintf(timestamp, "%s%s", user, tmp);
status("%s %s\n", timestamp, input);
}
/*---------------------------------------------------------------------------*/
/*
* Function: add_word
*
* Purpose: Add a word to a dictionary, and return the identifier
* assigned to the word. If the word already exists in
* the dictionary, then return its current identifier
* without adding it again.
*/
u_int16_t
add_word (DICTIONARY *dictionary, STRING word)
{
register int i;
int position;
bool found;
/*
* If the word's already in the dictionary, there is no need to add it
*/
position = search_dictionary(dictionary, word, &found);
if (found == TRUE)
goto succeed;
/*
* Increase the number of words in the dictionary
*/
dictionary->size++;
/*
* Allocate one more entry for the word index
*/
dictionary->index = xrealloc(dictionary->index, 2 * dictionary->size);
/*
* Allocate one more entry for the word array
*/
dictionary->entry = xrealloc(dictionary->entry, sizeof(STRING) * dictionary->size);
/*
* Copy the new word into the word array
*/
dictionary->entry[dictionary->size - 1].length = word.length;
dictionary->entry[dictionary->size - 1].word = xmalloc(word.length);
memcpy(dictionary->entry[dictionary->size - 1].word, word.word, word.length);
/*
* Shuffle the word index to keep it sorted alphabetically
*/
for (i = dictionary->size - 1; i > position; i--)
dictionary->index[i] = dictionary->index[i - 1];
/*
* Copy the new symbol identifier into the word index
*/
dictionary->index[position] = dictionary->size - 1;
succeed:
return dictionary->index[position];
}
/*---------------------------------------------------------------------------*/
/*
* Function: search_dictionary
*
* Purpose: Search the dictionary for the specified word, returning its
* position in the index if found, or the position where it
* should be inserted otherwise.
*/
int
search_dictionary (DICTIONARY *dictionary, STRING word, bool *find)
{
int position, min, max, middle, compar;
/*
* If the dictionary is empty, then obviously the word won't be found
*/
if (!dictionary->size) {
position = 0;
goto notfound;
}
/*
* Initialize the lower and upper bounds of the search
*/
min = 0;
max = dictionary->size - 1;
/*
* Search repeatedly, halving the search space each time, until either
* the entry is found, or the search space becomes empty
*/
while (TRUE) {
/*
* See whether the middle element of the search space is greater
* than, equal to, or less than the element being searched for.
*/
middle = (min + max) / 2;
compar = wordcmp(word, dictionary->entry[dictionary->index[middle]]);
/*
* If it is equal then we have found the element. Otherwise we
* can halve the search space accordingly.
*/
if (!compar) {
position = middle;
goto found;
} else if (compar > 0) {
if (max == middle) {
position = middle + 1;
goto notfound;
}
min = middle + 1;
} else {
if (min == middle) {
position = middle;
goto notfound;
}
max = middle - 1;
}
}
found:
*find = TRUE;
return position;
notfound:
*find = FALSE;
return position;
}
/*---------------------------------------------------------------------------*/
/*
* Function: find_word
*
* Purpose: Return the symbol corresponding to the word specified.
* We assume that the word with index zero is equal to a
* NULL word, indicating an error condition.
*/
u_int16_t
find_word (DICTIONARY *dictionary, STRING word)
{
int position;
bool found;
position = search_dictionary(dictionary, word, &found);
if (found == TRUE)
return dictionary->index[position];
else
return 0;
}
/*---------------------------------------------------------------------------*/
/*
* Function: wordcmp
*
* Purpose: Compare two words, and return an integer indicating whether
* the first word is less than, equal to or greater than the
* second word.
*/
int
wordcmp (STRING word1, STRING word2)
{
register int i;
int bound;
bound = MIN(word1.length,word2.length);
for (i = 0; i < bound; i++)
if (word1.word[i] != word2.word[i])
return (int)(word1.word[i] - word2.word[i]);
if (word1.length < word2.length)
return -1;
if (word1.length > word2.length)
return 1;
return 0;
}
/*---------------------------------------------------------------------------*/
/*
* Function: free_dictionary
*
* Purpose: Release the memory consumed by the dictionary.
*/
void
free_dictionary (DICTIONARY *dictionary)
{
register u_int32_t i;
if (!dictionary->size)
return;
for (i = 0; i < dictionary->size; i++)
xfree(dictionary->entry[i].word);
xfree(dictionary->entry);
dictionary->entry = 0;
xfree(dictionary->index);
dictionary->index = 0;
dictionary->size = 0;
initialize_dictionary(dictionary);
}
/*---------------------------------------------------------------------------*/
/*
* Function: initialize_dictionary
*
* Purpose: Add dummy words to the dictionary.
*/
void
initialize_dictionary (DICTIONARY *dictionary)
{
char err[] = "<ERROR>", fin[] = "<FIN>";
STRING word, end;
word.word = err;
word.length = 7;
end.word = fin;
end.length = 5;
add_word(dictionary, word);
add_word(dictionary, end);
}
/*---------------------------------------------------------------------------*/
/*
* Function: new_dictionary
*
* Purpose: Allocate room for a new dictionary.
*/
DICTIONARY *
new_dictionary (void)
{
DICTIONARY *dictionary;
dictionary = xmalloc(sizeof *dictionary);
dictionary->size = 0;
dictionary->index = 0;
dictionary->entry = 0;
initialize_dictionary(dictionary);
return dictionary;
}
/*---------------------------------------------------------------------------*/
/*
* Function: save_dictionary
*
* Purpose: Save a dictionary to the specified file.
*/
void
save_dictionary (FILE *fp, DICTIONARY *dictionary)
{
register u_int32_t i;
fwrite(&(dictionary->size), sizeof dictionary->size, 1, fp);
for (i = 0; i < dictionary->size; i++)
save_word(fp, dictionary->entry[i]);
}
/*---------------------------------------------------------------------------*/
/*
* Function: load_dictionary
*
* Purpose: Load a dictionary from the specified file.
*/
void
load_dictionary (FILE *fp, DICTIONARY *dictionary)
{
register u_int32_t i;
u_int32_t size;
fread(&size, sizeof size, 1, fp);
for (i = 0; i < size; i++)
load_word(fp, dictionary);
}
/*---------------------------------------------------------------------------*/
/*
* Function: save_word
*
* Purpose: Save a dictionary word to a file.
*/
void
save_word (FILE *fp, STRING word)
{
fwrite(&(word.length), sizeof word.length, 1, fp);
fwrite(word.word, sizeof *(word.word), word.length, fp);
}
/*---------------------------------------------------------------------------*/
/*
* Function: load_word
*
* Purpose: Load a dictionary word from a file.
*/
void
load_word (FILE *fp, DICTIONARY *dictionary)
{
char buf[0xff];
STRING word;
fread(&(word.length), sizeof word.length, 1, fp);
word.word = buf;
fread(word.word, sizeof *(word.word), word.length, fp);
add_word(dictionary, word);
}
/*---------------------------------------------------------------------------*/
/*
* Function: new_node
*
* Purpose: Allocate a new node for the n-gram tree, and initialise
* its contents to sensible values.
*/
TREE *
new_node (void)
{
TREE *node;
/*
* Allocate memory for the new node
*/
node = xmalloc(sizeof *node);
/*
* Initialise the contents of the node
*/
node->symbol = 0;
node->usage = 0;
node->count = 0;
node->branch = 0;
node->tree = 0;
return node;
}
/*---------------------------------------------------------------------------*/
/*
* Function: new_model
*
* Purpose: Create and initialise a new ngram model.
*/
MODEL *
new_model (int order)
{
MODEL *model;
model = xmalloc(sizeof *model);
model->order = order;
model->forward = new_node();
model->backward = new_node();
model->context = xmalloc(sizeof(TREE *) * (order + 2));
initialize_context(model);
model->dictionary = new_dictionary();
return model;
}
/*---------------------------------------------------------------------------*/
/*
* Function: update_model
*
* Purpose: Update the model with the specified symbol.
*/
void
update_model (MODEL *model, int symbol)
{
register u_int16_t i;
/*
* Update all of the models in the current context with the specified
* symbol.
*/
for (i = model->order + 1; i > 0; i--)
if (model->context[i - 1])
model->context[i] = add_symbol(model->context[i - 1], (u_int16_t)symbol);
}
/*---------------------------------------------------------------------------*/
/*
* Function: update_context
*
* Purpose: Update the context of the model without adding the symbol.
*/
void
update_context (MODEL *model, int symbol)
{
register u_int16_t i;
for (i = model->order + 1; i > 0; i--)
if (model->context[i - 1])
model->context[i] = find_symbol(model->context[i - 1], symbol);
}
/*---------------------------------------------------------------------------*/
/*
* Function: add_symbol
*
* Purpose: Update the statistics of the specified tree with the
* specified symbol, which may mean growing the tree if the
* symbol hasn't been seen in this context before.
*/
TREE *
add_symbol (TREE *tree, u_int16_t symbol)
{
TREE *node = 0;
/*
* Search for the symbol in the subtree of the tree node.
*/
node = find_symbol_add(tree, symbol);
/*
*Increment the symbol counts
*/
if (node->count < 65535) {
node->count++;
tree->usage++;
}
return node;
}
/*---------------------------------------------------------------------------*/
/*
* Function: find_symbol
*
* Purpose: Return a pointer to the child node, if one exists, which
* contains the specified symbol.
*/
TREE *
find_symbol (TREE *node, int symbol)
{
register int i;
TREE *found = 0;
bool found_symbol = FALSE;
/*
* Perform a binary search for the symbol.
*/
i = search_node(node, symbol, &found_symbol);
if (found_symbol == TRUE)
found = node->tree[i];
return found;
}
/*---------------------------------------------------------------------------*/
/*
* Function: find_symbol_add
*
* Purpose: This function is conceptually similar to find_symbol,
* apart from the fact that if the symbol is not found,
* a new node is automatically allocated and added to the
* tree.
*/
TREE *
find_symbol_add (TREE *node, int symbol)
{
register int i;
TREE *found = 0;
bool found_symbol = FALSE;
/*
* Perform a binary search for the symbol. If the symbol isn't found,
* attach a new sub-node to the tree node so that it remains sorted.
*/
i = search_node(node, symbol, &found_symbol);
if (found_symbol == TRUE) {
found = node->tree[i];
} else {
found = new_node();
found->symbol = symbol;
add_node(node, found, i);
}
return found;
}
/*---------------------------------------------------------------------------*/
/*
* Function: add_node
*
* Purpose: Attach a new child node to the sub-tree of the tree
* specified.
*/
void
add_node (TREE *tree, TREE *node, int position)
{
register int i;
/*
* Allocate room for one more child node, which may mean allocating
* the sub-tree from scratch.
*/
tree->tree = xrealloc(tree->tree, sizeof(TREE *) * (tree->branch + 1));
/*
* Shuffle the nodes down so that we can insert the new node at the
* subtree index given by position.
*/
for (i = tree->branch; i > position; i--)
tree->tree[i] = tree->tree[i - 1];
/*
* Add the new node to the sub-tree.
*/
tree->tree[position] = node;
tree->branch++;
}
/*---------------------------------------------------------------------------*/
/*
* Function: search_node
*
* Purpose: Perform a binary search for the specified symbol on the
* subtree of the given node. Return the position of the
* child node in the subtree if the symbol was found, or the
* position where it should be inserted to keep the subtree
* sorted if it wasn't.
*/
int
search_node (TREE *node, int symbol, bool *found_symbol)
{
register int position;
int min, max, middle, compar;
/*
* Handle the special case where the subtree is empty.
*/
if (!node->branch) {
position = 0;
goto notfound;
}
/*
* Perform a binary search on the subtree.
*/
min = 0;
max = node->branch - 1;
while (TRUE) {
middle = (min + max) / 2;
compar = symbol-node->tree[middle]->symbol;
if (!compar) {
position = middle;
goto found;
} else if (compar > 0) {
if (max == middle) {
position = middle + 1;
goto notfound;
}
min = middle + 1;
} else {
if (min == middle) {
position = middle;
goto notfound;
}
max = middle - 1;
}
}
found:
*found_symbol = TRUE;
return position;
notfound:
*found_symbol = FALSE;
return position;
}
/*---------------------------------------------------------------------------*/
/*
* Function: initialize_context
*
* Purpose: Set the context of the model to a default value.
*/
void
initialize_context (MODEL *model)
{
register u_int16_t i;
for (i = 0; i <= model->order; i++)
model->context[i] = 0;
}
/*---------------------------------------------------------------------------*/
/*
* Function: learn
*
* Purpose: Learn from the user's input.
*/
void
learn (MODEL *model, DICTIONARY *words)
{
register int i;
u_int16_t symbol;
/*
* We only learn from inputs which are long enough
*/
if (words->size <= model->order)
return;
/*
* Train the model in the forwards direction. Start by initializing
* the context of the model.
*/
initialize_context(model);
model->context[0] = model->forward;
for (i = 0; i < (signed)words->size; i++) {
/*
* Add the symbol to the model's dictionary if necessary, and then
* update the forward model accordingly.
*/
symbol = add_word(model->dictionary, words->entry[i]);
update_model(model, symbol);
}
/*
* Add the sentence-terminating symbol.
*/
update_model(model, 1);
/*
* Train the model in the backwards direction. Start by initializing
* the context of the model.
*/
initialize_context(model);
model->context[0] = model->backward;
for (i = words->size - 1; i >= 0; --i) {
/*
* Find the symbol in the model's dictionary, and then update
* the backward model accordingly.
*/
symbol = find_word(model->dictionary, words->entry[i]);
update_model(model, symbol);
}
/*
* Add the sentence-terminating symbol.
*/
update_model(model, 1);
return;
}
/*---------------------------------------------------------------------------*/
/*
* Function: train
*
* Purpose: Infer a MegaHAL brain from the contents of a text file.
*/
void
train (MODEL *model, const char *filename)
{
FILE *fp;
char buffer[1024];
DICTIONARY *words = 0;
if (!filename || !(fp = fopen(filename, "r")))
return;
words = new_dictionary();
while (fgets(buffer, sizeof buffer, fp)) {
if (buffer[0] == '#')
continue;
buffer[strlen(buffer) - 1] = 0;
upper(buffer);
make_words(buffer, words);
learn(model, words);
}
fclose(fp);
xfree(words->entry);
xfree(words);
}
/*---------------------------------------------------------------------------*/
/*
* Function: show_dictionary
*
* Purpose: Display the dictionary for training purposes.
*/
void
show_dictionary (DICTIONARY *dictionary)
{
register u_int32_t i;
FILE *fp;
if (!(fp = fopen("megahal.dic", "w"))) {
error("show_dictionary", "Unable to open file");
return;
}
for (i = 0; i < dictionary->size; i++)
fprintf(fp, "%.*s\n", dictionary->entry[i].length, dictionary->entry[i].word);
fclose(fp);
}
/*---------------------------------------------------------------------------*/
/*
* Function: save_model
*
* Purpose: Save the current state to a MegaHAL brain file.
*/
void
save_model (const char *filename, MODEL *model)
{
FILE *fp;
show_dictionary(model->dictionary);
if (!filename || !(fp = fopen(filename, "w"))) {
error("save_model", "Unable to open file `%s'", filename);
return;
}
fwrite(COOKIE, sizeof(char), sizeof COOKIE - 1, fp);
fwrite(&(model->order), sizeof model->order, 1, fp);
save_tree(fp, model->forward);
save_tree(fp, model->backward);
save_dictionary(fp, model->dictionary);
fclose(fp);
}
/*---------------------------------------------------------------------------*/
/*
* Function: save_tree
*
* Purpose: Save a tree structure to the specified file.
*/
void
save_tree (FILE *fp, TREE *node)
{
register u_int16_t i;
fwrite(&(node->symbol), sizeof node->symbol, 1, fp);
fwrite(&(node->usage), sizeof node->usage, 1, fp);
fwrite(&(node->count), sizeof node->count, 1, fp);
fwrite(&(node->branch), sizeof node->branch, 1, fp);
for (i = 0; i < node->branch; i++)
save_tree(fp, node->tree[i]);
}
/*---------------------------------------------------------------------------*/
/*
* Function: load_tree
*
* Purpose: Load a tree structure from the specified file.
*/
void
load_tree (FILE *fp, TREE *node)
{
register u_int16_t i;
fread(&(node->symbol), sizeof node->symbol, 1, fp);
fread(&(node->usage), sizeof node->usage, 1, fp);
fread(&(node->count), sizeof node->count, 1, fp);
fread(&(node->branch), sizeof node->branch, 1, fp);
if (!node->branch)
return;
node->tree = xmalloc(sizeof(TREE *) * node->branch);
for (i = 0; i < node->branch; i++) {
node->tree[i] = new_node();
load_tree(fp, node->tree[i]);
}
}
/*---------------------------------------------------------------------------*/
/*
* Function: load_model
*
* Purpose: Load a model into memory.
*/
bool
load_model (const char *filename, MODEL *model)
{
FILE *fp;
char cookie[sizeof COOKIE];
if (!filename || !(fp = fopen(filename, "r"))) {
error("load_model", "Unable to open file `%s'", filename);
return FALSE;
}
fread(cookie, sizeof *cookie, sizeof COOKIE - 1, fp);
if (memcmp(cookie, COOKIE, sizeof COOKIE - 1)) {
error("load_model", "File `%s' is not a MegaHAL brain (bad cookie %.*s)",
filename, sizeof COOKIE - 1, cookie);
fclose(fp);
return FALSE;
}
fread(&(model->order), sizeof model->order, 1, fp);
load_tree(fp, model->forward);
load_tree(fp, model->backward);
load_dictionary(fp, model->dictionary);
fclose(fp);
return TRUE;
}
static char period_str[] = ".";
/*---------------------------------------------------------------------------*/
/*
* Function: make_words
*
* Purpose: Break a string into an array of words.
*/
void
make_words (char *input, DICTIONARY *words)
{
unsigned int offset = 0;
/*
* Clear the entries in the dictionary
*/
words->size = 0;
/*
* If the string is empty then do nothing, for it contains no words.
*/
if (!input[0])
return;
/*
* Loop forever.
*/
for (;;) {
/*
* If the current character is of the same type as the previous
* character, then include it in the word. Otherwise, terminate
* the current word.
*/
if (boundary(input, offset)) {
/*
* Add the word to the dictionary
*/
words->entry = xrealloc(words->entry, (words->size + 1) * sizeof(STRING));
words->entry[words->size].length = offset;
words->entry[words->size].word = input;
words->size++;
if (offset == strlen(input))
break;
input += offset;
offset = 0;
} else {
offset++;
}
}
/*
* If the last word isn't punctuation, then replace it with a
* full-stop character.
*/
if (isalnum(words->entry[words->size - 1].word[0])) {
words->entry = xrealloc(words->entry, (words->size + 1) * sizeof(STRING));
words->entry[words->size].length = 1;
words->entry[words->size].word = period_str;
words->size++;
} else if (!strchr("!.?", words->entry[words->size - 1].word[0])) {
words->entry[words->size - 1].length = 1;
words->entry[words->size - 1].word = period_str;
}
}
/*---------------------------------------------------------------------------*/
/*
* Function: boundary
*
* Purpose: Return whether or not a word boundary exists in a string
* at the specified location.
*/
bool
boundary (char *str, unsigned int position)
{
if (!position)
return FALSE;
if (position == strlen(str))
return TRUE;
if (
str[position] == '\'' &&
isalpha(str[position - 1]) &&
isalpha(str[position + 1])
)
return FALSE;
if (
position > 1 &&
str[position - 1] == '\'' &&
isalpha(str[position - 2]) &&
isalpha(str[position])
)
return FALSE;
if (
isalpha(str[position]) &&
!isalpha(str[position - 1])
)
return TRUE;
if (
!isalpha(str[position]) &&
isalpha(str[position - 1])
)
return TRUE;
if (isdigit(str[position]) != isdigit(str[position - 1]))
return TRUE;
return FALSE;
}
/*---------------------------------------------------------------------------*/
/*
* Function: make_greeting
*
* Purpose: Put some special words into the dictionary so that the
* program will respond as if to a new judge.
*/
void
make_greeting (DICTIONARY *words)
{
words->size = 0;
if (g_grt->size > 2)
add_word(words, g_grt->entry[random() % (g_grt->size - 2) + 2]);
}
/*---------------------------------------------------------------------------*/
/*
* Function: generate_reply
*
* Purpose: Take a string of user input and return a string of output
* which may vaguely be construed as containing a reply to
* whatever is in the input string.
*/
char *
generate_reply (MODEL *model, DICTIONARY *words)
{
static DICTIONARY *dummy = 0;
DICTIONARY *replywords, *keywords;
float surprise, max_surprise;
char *output = 0;
int count, basetime;
/*
* Create an array of keywords from the words in the user's input
*/
keywords = make_keywords(model, words);
/*
* Make sure some sort of reply exists
*/
if (!dummy)
dummy = new_dictionary();
replywords = reply(model, dummy);
if (dissimilar(words, replywords) == TRUE)
output = make_output(replywords);
/*
* Loop for the specified waiting period, generating and evaluating
* replies
*/
max_surprise = (float)-1.0;
count = 0;
basetime = time(0);
do {
replywords = reply(model, keywords);
surprise = evaluate_reply(model, keywords, replywords);
++count;
if ((surprise > max_surprise) && (dissimilar(words, replywords) == TRUE)) {
max_surprise = surprise;
output = make_output(replywords);
}
} while ((time(0) - basetime) < TIMEOUT);
/*
* Return the best answer we generated
*/
return output ? output : xstrdup("I forgot what i was gonna say!");
}
/*---------------------------------------------------------------------------*/
/*
* Function: dissimilar
*
* Purpose: Return TRUE or FALSE depending on whether the dictionaries
* are the same or not.
*/
bool
dissimilar (DICTIONARY *words1, DICTIONARY *words2)
{
register unsigned int i;
if (words1->size != words2->size)
return TRUE;
for (i = 0; i < words1->size; i++)
if (wordcmp(words1->entry[i], words2->entry[i]))
return TRUE;
return FALSE;
}
/*---------------------------------------------------------------------------*/
/*
* Function: make_keywords
*
* Purpose: Put all the interesting words from the user's input into
* a keywords dictionary, which will be used when generating
* a reply.
*/
DICTIONARY *
make_keywords (MODEL *model, DICTIONARY *words)
{
static DICTIONARY *keys = 0;
register unsigned int i, j;
int c;
if (!keys)
keys = new_dictionary();
else
free_dictionary(keys);
for (i = 0; i < words->size; i++) {
/*
* Find the symbol ID of the word. If it doesn't exist in
* the model, or if it begins with a non-alphanumeric
* character, or if it is in the exclusion array, then
* skip over it.
*/
c = 0;
for (j = 0; j < g_swp->size; j++)
if (!wordcmp(g_swp->from[j], words->entry[i])) {
add_key(model, keys, g_swp->to[j]);
c = 1;
}
if (!c)
add_key(model, keys, words->entry[i]);
}
if (keys->size > 2)
for (i = 0; i < words->size; i++) {
c = 0;
for (j = 0; j < g_swp->size; j++)
if (!wordcmp(g_swp->from[j], words->entry[i])) {
add_aux(model, keys, g_swp->to[j]);
c = 1;
}
if (!c)
add_aux(model, keys, words->entry[i]);
}
return keys;
}
/*---------------------------------------------------------------------------*/
/*
* Function: add_key
*
* Purpose: Add a word to the keyword dictionary.
*/
void
add_key (MODEL *model, DICTIONARY *keys, STRING word)
{
int symbol;
symbol = find_word(model->dictionary, word);
if (!symbol)
return;
if (!isalnum(word.word[0]))
return;
symbol = find_word(g_ban, word);
if (symbol)
return;
symbol = find_word(g_aux, word);
if (symbol)
return;
add_word(keys, word);
}
/*---------------------------------------------------------------------------*/
/*
* Function: add_aux
*
* Purpose: Add an auxilliary keyword to the keyword dictionary.
*/
void
add_aux (MODEL *model, DICTIONARY *keys, STRING word)
{
int symbol;
symbol = find_word(model->dictionary, word);
if (!symbol)
return;
if (!isalnum(word.word[0]))
return;
symbol = find_word(g_aux, word);
if (!symbol)
return;
add_word(keys, word);
}
/*---------------------------------------------------------------------------*/
/*
* Function: reply
*
* Purpose: Generate a dictionary of reply words appropriate to the
* given dictionary of keywords.
*/
DICTIONARY *
reply (MODEL *model, DICTIONARY *keys)
{
DICTIONARY *replies;
register int i;
int symbol;
bool start = TRUE;
replies = new_dictionary();
replies->size = 0;
/*
* Start off by making sure that the model's context is empty.
*/
initialize_context(model);
model->context[0] = model->forward;
used_key = FALSE;
/*
* Generate the reply in the forward direction.
*/
while (TRUE) {
/*
* Get a random symbol from the current context.
*/
if (start == TRUE)
symbol = seed(model, keys);
else
symbol = babble(model, keys, replies);
if (!symbol || symbol == 1)
break;
start = FALSE;
/*
* Append the symbol to the reply dictionary.
*/
replies->entry = xrealloc(replies->entry, (replies->size + 1) * sizeof(STRING));
replies->entry[replies->size].length =
model->dictionary->entry[symbol].length;
replies->entry[replies->size].word =
model->dictionary->entry[symbol].word;
replies->size++;
/*
* Extend the current context of the model with the current symbol.
*/
update_context(model, symbol);
}
/*
* Start off by making sure that the model's context is empty.
*/
initialize_context(model);
model->context[0] = model->backward;
/*
* Re-create the context of the model from the current reply
* dictionary so that we can generate backwards to reach the
* beginning of the string.
*/
if (replies->size > 0)
for (i = MIN(replies->size - 1, model->order); i >= 0; --i) {
symbol = find_word(model->dictionary, replies->entry[i]);
update_context(model, symbol);
}
/*
* Generate the reply in the backward direction.
*/
while (TRUE) {
/*
* Get a random symbol from the current context.
*/
symbol = babble(model, keys, replies);
if (!symbol || symbol == 1)
break;
/*
* Prepend the symbol to the reply dictionary.
*/
replies->entry = xrealloc(replies->entry, (replies->size + 1) * sizeof(STRING));
/*
* Shuffle everything up for the prepend.
*/
for (i = replies->size; i > 0; --i) {
replies->entry[i].length = replies->entry[i - 1].length;
replies->entry[i].word = replies->entry[i - 1].word;
}
replies->entry[0].length = model->dictionary->entry[symbol].length;
replies->entry[0].word = model->dictionary->entry[symbol].word;
replies->size++;
/*
* Extend the current context of the model with the current symbol.
*/
update_context(model, symbol);
}
return replies;
}
/*---------------------------------------------------------------------------*/
/*
* Function: evaluate_reply
*
* Purpose: Measure the average surprise of keywords relative to the
* language model.
*/
float
evaluate_reply (MODEL *model, DICTIONARY *keys, DICTIONARY *words)
{
register unsigned int j;
register int i;
int symbol, count, num = 0;
float probability, entropy = (float)0.0;
TREE *node;
if (words->size <= 0)
return (float)0.0;
initialize_context(model);
model->context[0] = model->forward;
for (i = 0; i < (signed)words->size; i++) {
symbol = find_word(model->dictionary, words->entry[i]);
if (find_word(keys, words->entry[i])) {
probability = (float)0.0;
count = 0;
++num;
for (j = 0; j < model->order; j++)
if (model->context[j]) {
node = find_symbol(model->context[j], symbol);
probability += (float)(node->count) /
(float)(model->context[j]->usage);
++count;
}
if (count > 0.0)
entropy -= (float)log(probability / (float)count);
}
update_context(model, symbol);
}
initialize_context(model);
model->context[0] = model->backward;
for (i = words->size - 1; i >= 0; --i) {
symbol = find_word(model->dictionary, words->entry[i]);
if (find_word(keys, words->entry[i])) {
probability = (float)0.0;
count = 0;
++num;
for (j = 0; j < model->order; j++)
if (model->context[j]) {
node = find_symbol(model->context[j], symbol);
probability += (float)(node->count) /
(float)(model->context[j]->usage);
++count;
}
if (count > 0.0)
entropy -= (float)log(probability / (float)count);
}
update_context(model, symbol);
}
if (num >= 8)
entropy /= (float)sqrt(num - 1);
if (num >= 16)
entropy /= (float)num;
return entropy;
}
/*---------------------------------------------------------------------------*/
/*
* Function: make_output
*
* Purpose: Generate a string from the dictionary of reply words.
*/
char *
make_output (DICTIONARY *words)
{
char *output = 0;
register unsigned int i, len;
if (!words->size)
return xstrdup("I am utterly speechless!");
len = 1;
for (i = 0; i < words->size; i++)
len += words->entry[i].length;
output = xmalloc(len + 1);
len = 0;
for (i = 0; i < words->size; i++) {
memcpy(&(output[len]), words->entry[i].word, words->entry[i].length);
len += words->entry[i].length;
}
output[len] = 0;
return output;
}
/*---------------------------------------------------------------------------*/
/*
* Function: babble
*
* Purpose: Return a random symbol from the current context, or a
* zero symbol identifier if we've reached either the
* start or end of the sentence. Select the symbol based
* on probabilities, favouring keywords. In all cases,
* use the longest available context to choose the symbol.
*/
int
babble (MODEL *model, DICTIONARY *keys, DICTIONARY *words)
{
TREE *node = 0;
register int i;
int count, symbol = 0;
/*
* Select the longest available context.
*/
for (i = 0; i <= model->order; i++)
if (model->context[i])
node = model->context[i];
if (!node || !node->branch)
return 0;
/*
* Choose a symbol at random from this context.
*/
i = random() % node->branch;
count = random() % node->usage;
while (count >= 0) {
/*
* If the symbol occurs as a keyword, then use it. Only use an
* auxilliary keyword if a normal keyword has already been used.
*/
symbol = node->tree[i]->symbol;
if (
(find_word(keys, model->dictionary->entry[symbol])) &&
(used_key == TRUE ||
(!find_word(g_aux, model->dictionary->entry[symbol]))) &&
(word_exists(words, model->dictionary->entry[symbol]) == FALSE)
) {
used_key = TRUE;
break;
}
count -= node->tree[i]->count;
i = i >= (node->branch - 1) ? 0 : i + 1;
}
return symbol;
}
/*---------------------------------------------------------------------------*/
/*
* Function: word_exists
*
* Purpose: A silly brute-force searcher for the reply string.
*/
bool
word_exists (DICTIONARY *dictionary, STRING word)
{
register u_int32_t i;
for (i = 0; i < dictionary->size; i++)
if (!wordcmp(dictionary->entry[i], word))
return TRUE;
return FALSE;
}
/*---------------------------------------------------------------------------*/
/*
* Function: seed
*
* Purpose: Seed the reply by guaranteeing that it contains a
* keyword, if one exists.
*/
int
seed (MODEL *model, DICTIONARY *keys)
{
register unsigned int i, stop;
int symbol;
if (!model->context[0]->branch)
symbol = 0;
else
symbol = random() % model->context[0]->branch;
if (keys->size > 2) {
do {
i = random() % keys->size;
} while (i < 2);
stop = i;
while (TRUE) {
if(
(find_word(model->dictionary, keys->entry[i])) &&
(!find_word(g_aux, keys->entry[i]))
) {
symbol = find_word(model->dictionary, keys->entry[i]);
return symbol;
}
i++;
if (i == keys->size)
i = 2;
if (i == stop)
return symbol;
}
}
return symbol;
}
/*---------------------------------------------------------------------------*/
/*
* Function: new_swap
*
* Purpose: Allocate a new swap structure.
*/
SWAP *
new_swap (void)
{
SWAP *list;
list = xmalloc(sizeof *list);
list->size = 0;
list->from = 0;
list->to = 0;
return list;
}
/*---------------------------------------------------------------------------*/
/*
* Function: add_swap
*
* Purpose: Add a new entry to the swap structure.
*/
void
add_swap (SWAP *list, char *s, char *d)
{
list->size++;
list->from = xrealloc(list->from, sizeof(STRING) * (list->size));
list->to = xrealloc(list->to, sizeof(STRING) * (list->size));
list->from[list->size - 1].length = strlen(s);
list->from[list->size - 1].word = xstrdup(s);
list->to[list->size - 1].length = strlen(d);
list->to[list->size - 1].word = xstrdup(d);
}
/*---------------------------------------------------------------------------*/
/*
* Function: initialize_swap
*
* Purpose: Read a swap structure from a file.
*/
SWAP *
initialize_swap (const char *filename)
{
SWAP *list;
FILE *fp;
char buffer[1024], *from, *to;
list = new_swap();
if (!filename || !(fp = fopen(filename, "r")))
return list;
while (fgets(buffer, sizeof buffer, fp)) {
if (buffer[0] == '#')
continue;
from = strtok(buffer, "\t ");
to = strtok(0, "\t \n#");
add_swap(list, from, to);
}
fclose(fp);
return list;
}
/*---------------------------------------------------------------------------*/
/*
* Function: initialize_list
*
* Purpose: Read a dictionary from a file.
*/
DICTIONARY
*initialize_list (const char *filename)
{
DICTIONARY *list;
FILE *fp;
STRING word;
char *string, buffer[1024];
list = new_dictionary();
if (!filename || !(fp = fopen(filename, "r")))
return list;
while (fgets(buffer, sizeof buffer, fp)) {
if (buffer[0] == '#')
continue;
string = strtok(buffer, "\t \n#");
if (string && string[0]) {
word.length = strlen(string);
word.word = xstrdup(buffer);
add_word(list, word);
}
}
fclose(fp);
return list;
}
#ifdef __BEOS__
extern int snooze();
#define usleep(ms) snooze(ms)
#endif
/*---------------------------------------------------------------------------*/
/*
* Function: delay
*
* Purpose: Display the string to stdout as if it was typed by a human.
*/
void
delay (char *str)
{
register int i;
register char *out;
/*
* Don't simulate typing if the feature is turned off
*/
if (typing_delay == FALSE) {
colourz(str);
return;
}
out = xmalloc(strlen(str) + 1);
for (i = 0; str[i]; i++) {
/*
* Standard keyboard delay
*/
usleep(D_KEY + random() % V_KEY - random() % V_KEY);
out[i] = str[i];
/*
* A random thinking delay
*/
if ((!isalnum(str[i])) && ((random() % 100) < P_THINK))
usleep(D_THINK + random() % V_THINK - random() % V_THINK);
}
out[i] = 0;
colourz(out);
xfree(out);
}
/*===========================================================================*/
/*
* $Log: megahal.c,v $
* Revision 1.8 1997/12/24 03:17:01 hutch
* More bug fixes, and hopefully the final contest version!
*
* Revision 1.7 1997/12/22 13:18:09 hutch
* A few more bug fixes, and non-repeating implemented.
*
* Revision 1.6 1997/12/22 04:27:04 hutch
* A few minor bug fixes.
*
* Revision 1.5 1997/12/15 04:35:59 hutch
* Final Loebner version!
*
* Revision 1.4 1997/12/11 05:45:29 hutch
* the almost finished version.
*
* Revision 1.3 1997/12/10 09:08:09 hutch
* Now Loebner complient (tm)
*
* Revision 1.2 1997/12/08 06:22:32 hutch
* Tidied up.
*
* Revision 1.1 1997/12/05 07:11:44 hutch
* Initial revision
*
* Revision 1.7 1997/12/04 07:07:13 hutch
* Added load and save functions, and tidied up some code/
*
* Revision 1.6 1997/12/02 08:34:47 hutch
* Added the ban, aux and swp functions.
*
* Revision 1.5 1997/12/02 06:03:04 hutch
* Updated to use a special terminating symbol, and to store only
* branches of maximum depth, as they are the only ones used in
* the reply.
*
* Revision 1.4 1997/10/28 09:23:12 hutch
* MegaHAL is babbling nicely, but without keywords.
*
* Revision 1.3 1997/10/15 09:04:03 hutch
* MegaHAL can parrot back whatever the user says.
*
* Revision 1.2 1997/07/21 04:03:28 hutch
* Fully working.
*
* Revision 1.1 1997/07/15 01:55:25 hutch
* Initial revision
*
* Revision 1.1 1997/07/15 01:54:21 hutch
* Initial revision
*/
/*===========================================================================*/
|