/**********************************************************************
*
*  module   : Tei
*  commande : TeiMiniVocEngRules
*  fichier  : TeiMiniVocEngRules.c
*  Source   : Dilib v0.2 MiniVocEngRules.c
*  Auteur   : Jacques DUCLOY & Joelle KLEIN
*  Date     : Janvier 97 -> septembre 2016
*
***********************************************************************/

#include "StrDict.h"
#include "Buffer.h"
#include "string.h"

StrDict *vocabulary;
Buffer        *bufWord;
Buffer        *bufWo2;
extern char *optarg;
extern int   optind;
int getopt();

void analWord(str)
     char *str;
{
  int l;
  char *wo1;

  l=strlen(str);
  switch(str[l-1])
    {
    case 's':
      if (str[l-2]=='e'&&str[l-3]=='i')
	{                                        /* ...ies */
	  wo1=BufferStrncpy(bufWord, str, strlen(str)-2);
	  wo1[strlen(str)-3]='y';
	  if (StrDictSearch(vocabulary, wo1))
	    {printf("%s\t%s\n", str, wo1);};
	}
      else                                        /* ...s */
	{
	  wo1=BufferStrncpy(bufWord, str, strlen(str)-1);
	  if (StrDictSearch(vocabulary, wo1))
	    {printf("%s\t%s\n", str, wo1);};
	}
      break;
    case 'd':
      if (str[l-2]=='e')
	{                                            /* ...ed */
	  wo1=BufferStrncpy(bufWord, str, strlen(str)-1);
	  if (StrDictSearch(vocabulary, wo1))
	    {printf("%s\t%s\n", str, wo1);};
	}
      break;
    case 'g':
      if (str[l-2]=='n'&&str[l-3]=='i')
	{                                               /* ...ing */
	  /* print -> printing ? */
	  wo1=BufferStrncpy(bufWord, str, strlen(str)-3);
	  if (StrDictSearch(vocabulary, wo1))
	    {printf("%s\t%s\n", str, wo1);}
	  else
	    {          /*  realize -> realizing */
	      wo1=BufferStrncpy(bufWord, str, strlen(str)-2);
	      wo1[strlen(str)-3]='e';
	      if (StrDictSearch(vocabulary, wo1))
		{printf("%s\t%s\n", str, wo1);}
	      else
		{
		  /* prints -> printing */
		  wo1[strlen(str)-3]='s';
		  if (StrDictSearch(vocabulary, wo1))
		    {
		      wo1[strlen(str)-3]='\0';
		      printf("%ss\t%s\n", wo1, wo1);
		      printf("%s\t%s\n", str, wo1);
		    }
		}
	    }
	}
      break;
    default:
      break;
    }
}

int main(argc,argv)
      int argc;
      char **argv;
{
   int cOption;
   int modeTable;
   char *w1; 
   Buffer *bw1;

   modeTable=0;
   bufWord=BufferCreate(10,10);
   bufWo2 =BufferCreate(10,10);
   bw1=NewBuffer();
   while((cOption=getopt(argc,argv,"t:"))!=EOF)
	 
     {switch (cOption)
      {

      case 't':
	 vocabulary=StrDictFromFile(optarg);
         modeTable=1;
	 break;

      }
    }

  if(!modeTable)
    {
      vocabulary=NewStrDict();
      while (BufferGets(bw1))
	{
	  StrDictAddNewDatum(vocabulary, strdup(BufferString(bw1)), "empty");
	}
      StrDictIteratorReset(vocabulary);
      while((w1=StrDictNext(vocabulary)))
	{
	  analWord(w1);
	}
    }
         
   else 
      {

          while (BufferGets(bw1))
              {
                   analWord(BufferString(bw1));
              }

      }
   

  return 0;
}




