Language transliterator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

221 lines
5.1 KiB

  1. /*
  2. * open jisho file, and set the size of this jisho etc
  3. * default personal jisho: $home/lib/ktrans-jisho
  4. *
  5. * Kenji Okamoto August 4, 2000
  6. * Osaka Prefecture Univ.
  7. * okamoto@granite.cias.osakafu-u.ac.jp
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include "jisho.h"
  13. Dictionary *openQDIC(char *);
  14. void freeQDIC(Dictionary*);
  15. KouhoList *getKouhoHash(Dictionary*, char *);
  16. KouhoList *getKouhoFile(DicList*, char *);
  17. void selectKouho(KouhoList **, KouhoList*);
  18. int hashVal(char *);
  19. void addHash(Hash **, DicList*);
  20. /*
  21. * Open QuickDIC (hashed personal dictionary)
  22. * open skk styled ktrans dictinary file, and make its hash table
  23. * based on individual header kana strings
  24. *
  25. * KouhoList
  26. * |---------|
  27. * Hash |---->kouho---->kouhotop
  28. * |-------| |
  29. * dic---->dhash---->dicindex---->kanahead
  30. * |--------| |--------|
  31. * Dictionary DicList
  32. *
  33. */
  34. Dictionary *
  35. openQDIC(char *dicname)
  36. {
  37. Biobuf *f;
  38. void *Bbuf;
  39. Dictionary *dic;
  40. DicList *dicitem; /* for a future extension */
  41. char buf[1024], *startstr, *endstr;
  42. int i;
  43. SET(dicitem); /* yes, I know I'm wrong, but... */
  44. dic = (Dictionary*)malloc(sizeof(Dictionary));
  45. /* make room for pointer array (size=HASHSIZE) of hash table */
  46. for(i=0; i< HASHSIZE; i++) dic->dhash[i] = 0;
  47. dic->dlist = 0; /* for a future extension (more than one dics ^_^ */
  48. if ((f = Bopen(dicname, OREAD)) == 0)
  49. return dic;
  50. /* make hash table by the dic's header word */
  51. while(Bbuf = Brdline(f, '\n')) {
  52. strncpy(buf, (char *)Bbuf, Blinelen(f));
  53. if (buf[0] == ';') /* comment line */
  54. continue;
  55. else {
  56. /* get header word from jisho */
  57. startstr = buf;
  58. if(!(endstr = utfutf(startstr, "\t"))) break;
  59. *endstr = '\0';
  60. /* dicitem includes each header word from the jisho */
  61. dicitem = (DicList*)malloc(sizeof(DicList)+(endstr-startstr+1));
  62. dicitem->nextitem = 0; /* for a future extension */
  63. strcpy(dicitem->kanahead, startstr);
  64. dicitem->kouho = getKouhoFile(dicitem, endstr); /* read kouho from jisho */
  65. addHash(dic->dhash, dicitem);
  66. }
  67. continue;
  68. }
  69. dic->dlist = dicitem;
  70. Bterm(f);
  71. return dic;
  72. }
  73. /*
  74. * free dynamically allocated memory
  75. */
  76. void
  77. freeQDIC(Dictionary *dic)
  78. {
  79. Hash *hash1, *hash2;
  80. DicList *dlist, *dlist2;
  81. int l;
  82. for (dlist = dic->dlist;
  83. dlist != 0;
  84. dlist2 = dlist, dlist = dlist->nextitem, free((void *)dlist2));
  85. for (l = 0; l < HASHSIZE; l++) {
  86. for (hash1 = dic->dhash[l]; hash1; hash1 = hash2) {
  87. if (hash1->next !=0) {
  88. hash2 = hash1->next;
  89. free((void *)hash1);
  90. }else
  91. break;
  92. }
  93. }
  94. free((void *)dic);
  95. }
  96. /*
  97. * well known method to make a hash table
  98. * I don't know another better method. ^_^
  99. */
  100. int
  101. hashVal(char *s)
  102. {
  103. int n = 0;
  104. while (*s) {
  105. n += (*s)*(*s);
  106. s++;
  107. }
  108. return n%HASHSIZE;
  109. }
  110. void
  111. addHash(Hash **hash, DicList *ditem)
  112. {
  113. Hash *h;
  114. int v;
  115. v = hashVal(ditem->kanahead);
  116. h = (Hash*)malloc(sizeof(Hash));
  117. h->dicindex = ditem;
  118. h->length = strlen(ditem->kanahead);
  119. h->next = hash[v];
  120. hash[v] = h;
  121. }
  122. /*
  123. * read Kouho list from the jisho file defined by Biobuf descriptor f
  124. *
  125. * revised for Plan 9 by K.Okamoto
  126. */
  127. KouhoList *
  128. getKouhoFile(DicList *dicitem, char * endstr)
  129. {
  130. char *kouhostart, *kouhoend;
  131. KouhoList *kouhoitem, *currntkouhoitem=0, *prevkouhoitem;
  132. prevkouhoitem = 0;
  133. kouhostart = endstr + 1;
  134. while((kouhoend = utfutf(kouhostart, " ")) ||
  135. (kouhoend = utfutf(kouhostart, "\n"))) {
  136. *kouhoend = '\0';
  137. kouhoitem = (KouhoList*)malloc(sizeof(KouhoList)+(kouhoend-kouhostart+1));
  138. kouhoitem->nextkouho = 0;
  139. kouhoitem->prevkouho = prevkouhoitem;
  140. kouhoitem->dicitem = dicitem;
  141. strcpy(kouhoitem->kouhotop, kouhostart);
  142. if (prevkouhoitem)
  143. prevkouhoitem->nextkouho = kouhoitem;
  144. else
  145. currntkouhoitem = kouhoitem;
  146. prevkouhoitem = kouhoitem;
  147. kouhostart = kouhoend + 1;
  148. }
  149. return currntkouhoitem;
  150. }
  151. /*
  152. * get matched kouho from the hash table of header word of the dic
  153. * if found, returns pointer to the first candidate in the hash table.
  154. * if not found, returns 0.
  155. *
  156. * from getCand() in skklib.c by Akinori Ito et al.,(aito@ei5sun.yz.yamagata-u.ac.jp)
  157. */
  158. KouhoList *
  159. getKouhoHash(Dictionary *dic, char *s)
  160. {
  161. int l, v;
  162. Hash *h;
  163. l = strlen(s);
  164. v = hashVal(s);
  165. for (h = dic->dhash[v]; h != 0; h = h->next) {
  166. if (h->length != l ||
  167. strcmp(h->dicindex->kanahead, s)) continue;
  168. return h->dicindex->kouho; /* return matched kouho */
  169. }
  170. return 0;
  171. }
  172. /*
  173. *from skklib.c by Akinori Ito et al.,(aito@ei5sun.yz.yamagata-u.ac.jp)
  174. * just modified to read easier for present purpose
  175. */
  176. void
  177. selectKouho(KouhoList **first, KouhoList *current)
  178. {
  179. /* take off currentkouho from the kouholist table */
  180. if (current->prevkouho) {
  181. current->prevkouho->nextkouho = current->nextkouho;
  182. if (current->nextkouho)
  183. current->nextkouho->prevkouho = current->prevkouho;
  184. current->prevkouho = 0;
  185. }
  186. /* take place of firstkouho by currentkouho */
  187. if (*first != current) {
  188. (*first)->prevkouho = current;
  189. current->nextkouho = *first;
  190. *first = current;
  191. }
  192. }