PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/mythes-1.2.3/example.cxx

#
C++ | 119 lines | 89 code | 18 blank | 12 comment | 22 complexity | b44c322c56b48d814658907e77ad2c19 MD5 | raw file
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "mythes.hxx"
  5. #include <hunspell.hxx>
  6. extern char * mystrdup(const char * s);
  7. using namespace std;
  8. void myfreelist(char *** list, int n)
  9. {
  10. if (list && (n > 0)) {
  11. for (int i = 0; i < n; i++) if ((*list)[i]) free((*list)[i]);
  12. free(*list);
  13. *list = NULL;
  14. }
  15. }
  16. int main(int argc, char** argv)
  17. {
  18. FILE* wtclst;
  19. /* first parse the command line options
  20. * arg1 - index file, arg2 thesaurus data file, arg3 - file of words to check,
  21. * arg4, arg5 - opt. Hunspell affix and dic file for stemming and
  22. * morphological generation
  23. */
  24. if (argc < 3) {
  25. fprintf(stderr,"correct syntax is:\n");
  26. fprintf(stderr,"example index_file thesaurus_file file_of_words_to_check [affix_file dic_file]\n");
  27. exit(1);
  28. }
  29. /* open the words to check list */
  30. wtclst = fopen(argv[3], "r");
  31. if (!wtclst) {
  32. fprintf(stderr,"Error - could not open file of words to check\n");
  33. exit(1);
  34. }
  35. // Hunspell for stemming and morphological generation of affixes synonyms
  36. Hunspell * pH = NULL;
  37. if (argc >= 5) pH = new Hunspell(argv[4], argv[5], (const char *) NULL);
  38. // open a new thesaurus object
  39. MyThes * pMT = new MyThes(argv[1], argv[2]);
  40. // get the encoding used for the thesaurus data
  41. char * encoding = pMT->get_th_encoding();
  42. printf("Thesaurus uses encoding %s\n\n", encoding);
  43. int k;
  44. char buf[101];
  45. char oldbuf[101];
  46. mentry * pmean;
  47. while(fgets(buf,100,wtclst)) {
  48. oldbuf[0] = '\0';
  49. k = strlen(buf);
  50. *(buf + k - 1) = '\0';
  51. int len = strlen(buf);
  52. int count = pMT->Lookup(buf,len,&pmean);
  53. // don't change value of pmean
  54. // or count since needed for CleanUpAfterLookup routine
  55. if (!count) {
  56. int stemcount = 0;
  57. char **stem;
  58. if (pH) stemcount = pH->stem(&stem, buf); else stemcount = 0;
  59. if (stemcount) {
  60. printf("stem: %s\n", stem[0]);
  61. strncpy(oldbuf,buf, sizeof(oldbuf)-1);
  62. oldbuf[sizeof(oldbuf)-1] = 0;
  63. strncpy(buf, stem[0], sizeof(buf)-1);
  64. buf[sizeof(buf)-1] = 0;
  65. len = strlen(buf);
  66. count = pMT->Lookup(buf, len, &pmean);
  67. myfreelist(&stem, stemcount);
  68. } else oldbuf[0] = '\0';
  69. }
  70. mentry* pm = pmean;
  71. if (count) {
  72. printf("%s has %d meanings\n",buf,count);
  73. for (int i=0; i < count; i++) {
  74. printf(" meaning %d: %s\n",i,pm->defn);
  75. for (int j=0; j < pm->count; j++) {
  76. char ** gen;
  77. int l = 0;
  78. if (pH && oldbuf[0]) l = pH->generate(&gen, pm->psyns[j], oldbuf);
  79. if (l) {
  80. int k;
  81. printf(" %s",gen[0]);
  82. for (k = 1; k < l; k++) printf(", %s",gen[k]);
  83. printf("\n");
  84. myfreelist(&gen, l);
  85. } else printf(" %s\n",pm->psyns[j]);
  86. }
  87. printf("\n");
  88. pm++;
  89. }
  90. printf("\n\n");
  91. // now clean up all allocated memory
  92. pMT->CleanUpAfterLookup(&pmean,count);
  93. } else {
  94. printf("\"%s\" is not in thesaurus!\n",buf);
  95. }
  96. }
  97. fclose(wtclst);
  98. delete pMT;
  99. if (pH) delete pH;
  100. return 0;
  101. }