/*   -*- coding: utf-8 -*-  */

#include "StrDict.h"
#include "Utf8Text.h"
#include <string.h>
#include <stdlib.h>

StrDict *Utf8LangJoinProfile(char *f1, char *f2)
{
  StrDict *d1;
  StrDict *d2;
  char    *k1;
  char    *k2;
  char    *v1;
  char    *v2;
  int      maxLen;
  char    *newStr;
  maxLen=0;
  d1=StrDictFromFile(f1);
  d2=StrDictFromFile(f2);
  StrDictIteratorReset(d1);
  while ((k1=StrDictNext(d1)))
    {
      int l1;
      v1=StrDictValue(d1);
      l1=strlen(k1);
      if (maxLen < l1) maxLen=l1;
      if (!(k2=StrDictSearch(d2, k1)))
	{
	  newStr=malloc(10);
	  sprintf(newStr, "%d", 2 * atoi(v1));  /* multiply by 2 in order to enforce */
	  free(v1);
	  StrDictSet(d1, k1, newStr);
	}
    }
  StrDictIteratorReset(d2);
  while ((k2=StrDictNext(d2)))
    {
      int newFreq;
      newStr=malloc(10);
      v2=StrDictValue(d2);
      if ((v1=StrDictSearch(d1, k2)))
	{
	  sprintf(newStr, "%d", atoi(v1)-atoi(v2));
	  free(v1);
	  StrDictSet(d1, k2, newStr);
	}
      else
	{
	  sprintf(newStr, "%d", -(2 * atoi(v2)));
	  StrDictAddNewDatum(d1, strdup(k2), newStr);
	}
    }
  StrDictFreeStr(d2);
  newStr=malloc(10);
  sprintf(newStr, "%d", maxLen);
  StrDictAddNewDatum(d1, "<maxLen>", newStr); /* <maxLen> could not appear in an SxmlText */
  return d1;
}

StrDict *Utf8LangEngFreDict=NULL;
int      Utf8LangEngFreMaxLen;
Buffer  *Utf8LangEngFreBuf;

int Utf8LangEvalEngFre(char *text)
{
  int maxLen;
  int cText;
  char *pos;
  int score;
  score=0;
  if (!Utf8LangEngFreDict)
    {
      Buffer *bufFre;
      Buffer *bufEng;
      bufFre=NewBuffer();
      bufEng=NewBuffer();
      Utf8LangEngFreBuf=NewBuffer();
      BufferStrcpy(bufFre,getenv("DILIB"));
      BufferStrcpy(bufEng,getenv("DILIB"));
      BufferStrcat(bufFre, "/Data/Fre/Utf8Profile.dict");
      BufferStrcat(bufEng, "/Data/Eng/Utf8Profile.dict");
      Utf8LangEngFreDict=Utf8LangJoinProfile(BufferString(bufEng),BufferString(bufFre));
      Utf8LangEngFreMaxLen=atoi(StrDictSearch(Utf8LangEngFreDict, "<maxLen>"));
      BufferFree(bufFre);
      BufferFree(bufEng);
    }
  pos=text;
  while ((cText=*pos))
    {
      int toDo;
      int localScore;
      int localLen;
      char *v1;
      toDo=Utf8LangEngFreMaxLen;
      localScore=0;
      localLen=0;
      while (toDo)
	{
	  if(strlen(pos)<toDo) 
	    {
	      toDo--;
	      continue;
	    }
	  BufferStrncpy(Utf8LangEngFreBuf, pos, toDo);
	  if ((v1=StrDictSearch(Utf8LangEngFreDict, BufferString(Utf8LangEngFreBuf))))
	    {
	      localScore=atoi(v1);
	      score+=localScore;
	      pos+=toDo;
	      break;
	    }
	  toDo--;
	}
      if (toDo==0)pos++;
    }
  return score;
}
