#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string.h>
#include"Interface.h"
#include"glodef.h"
using namespace std;
#define CountCut 0
#define VocabSize 32531
#define StopWordsSize 571
int MapToID(char* p_acWord, char** p_aacVocab, int p_nVocabSize, char** p_aacStopWords, int p_nStopWordsSize);
void main(int argc, char* argv[])       //argv[1] :vocab 文件　　argv[2]: filelist 文件 argv[3] stopwords 文件

{  FILE_PATH fpInputFile,fpOutputFile;
   FILE* fpVocabFile,*fpStopWordsFile;
   char acWord[100];
   int nCountOfWord;
//   char acTempWord[100];
   int WordLen;
   int CurWordNo=0;
   char *Vocab[VocabSize],*StopWords[StopWordsSize];


   if(  (fpVocabFile=fopen(argv[1],"r"))==NULL)  { outputPrompt("open vocab file %s failed",argv[1]); return ;}
    if(  (fpStopWordsFile=fopen(argv[3],"r"))==NULL)  { outputPrompt("open stop_words file %s failed",argv[3]); return ;}

	while( fscanf(fpVocabFile,"%s%d",acWord,&nCountOfWord)!=EOF)
	{ 
		if(nCountOfWord>CountCut)
		{
			WordLen=strlen(acWord);
			Vocab[CurWordNo]=(char *)malloc( (WordLen+1)*sizeof(char));
			strcpy(Vocab[CurWordNo],acWord);		
		//	printf("%s",Vocab[CurWordNo]);
			CurWordNo++;
		}
	}
      CurWordNo=0;
		while( fscanf(fpStopWordsFile,"%s",acWord)!=EOF)
	{ 
		
		
			WordLen=strlen(acWord);
			StopWords[CurWordNo]=(char *)malloc( (WordLen+1)*sizeof(char));
			strcpy(StopWords[CurWordNo],acWord);		
		//	printf("%s",Vocab[CurWordNo]);
			CurWordNo++;
		
	}
    FILE* fpFileList,*fpFile,*fpFileOut;
	ifstream fInputFile;
	int nWordID;
	char acInputWord[100];
	bool bJustChangeLine=0;

 // nWordID=MapToID("zzzz",Vocab,VocabSize);


 if(  (fpFileList=fopen(argv[2],"r"))==NULL)  { outputPrompt("open  filelist %s failed",argv[2]); return ;}
         while(fgets(fpInputFile,Max_Path_Len,fpFileList)!=NULL)
		 {   
			 int p=strlen(fpInputFile)-1;
	         if(fpInputFile[p]==10) fpInputFile[p]='\0';  //抹掉回车    "\10" 不行？？？
		     fInputFile.open(fpInputFile);
			 
			 if(!fInputFile)
				{ outputPrompt("open  filelist %s failed",fpInputFile); return ;}
			 
			 sprintf(fpOutputFile,"%s.outns",fpInputFile);
			 if(  (fpFileOut=fopen(fpOutputFile,"w"))==NULL)  { outputPrompt("open  file %s failed",fpOutputFile); return ;}
			      
			 while(1)
					{
						fInputFile>>acInputWord;
						if(acInputWord[0]==0) break;
                        nWordID=MapToID(acInputWord,Vocab,VocabSize,StopWords,StopWordsSize);
						if(nWordID>0) 
						{fprintf(fpFileOut,"%d ",nWordID); bJustChangeLine=0;}
						else if(nWordID==-2)
							outputPrompt("invalid character %s in %s\n",acInputWord,fpInputFile);
						if(fInputFile.get()==10&&!bJustChangeLine) 
						{ fInputFile.seekg(-1L,ios_base::cur);
						  fprintf(fpFileOut,"\n");
						  bJustChangeLine=1;
						}
					}
					fInputFile.close();
					fInputFile.clear();
					fclose(fpFileOut);
					outputPrompt("%s done\n",fpInputFile);
			
		 }
		 fclose(fpVocabFile);
		 fclose(fpFileList);
	












}
int MapToID(char* p_acWord, char** p_aacVocab, int p_nVocabSize, char** p_aacStopWords, int p_nStopWordsSize)
{  int begin,cur,end, key=-1,key2=-1;
   int flag;
   begin=1;
   end=p_nVocabSize-1;
   
   while( end>=begin)
   {     cur=(begin+end)/2;
	    flag=strcmp(p_acWord,p_aacVocab[cur]);
        if (flag==0) {key=cur; break;}
		else if (flag<0) {end=cur-1;}
		else  {begin=cur+1;}
   }
   if(key==-1) return -2;  //返回-2查不到

   begin=0;
   end=p_nStopWordsSize-1;
   
   while( end>=begin)
   {     cur=(begin+end)/2;
	    flag=strcmp(p_acWord,p_aacStopWords[cur]);
        if (flag==0) {key2=cur; break;}
		else if (flag<0) {end=cur-1;}
		else  {begin=cur+1;}
   }
   if( key2!=-1) return -1;         //在stopword中，返回-1
   else return key;






}