Ejemplo n.º 1
0
Archivo: trie.c Proyecto: starsep/Trie
void prev(int number, int start, int end){
	if(!(number < numberOfWords && endOfWord[number] != NULL &&
	words[number].text != NULL &&
	start <= end && start >= 0 && end < words[number].size))
		ignore();
	if(checkBadLine())return;
	addPrevWord(number, start, end);
	addLastWord();
}
Ejemplo n.º 2
0
WordPairLM::WordPairLM( const char *fName , DecVocabulary *vocab_ )
{
   if ( (vocab = vocab_) == NULL )
      error("WordPairLM::WordPairLM - vocab_ is NULL") ;

   if ( vocab->sentStartIndex < 0 )
      error("WordPairLM::WordPairLM - vocab->sentStartIndex not defined") ;
   if ( vocab->sentEndIndex < 0 )
      error("WordPairLM::WordPairLM - vocab->sentEndIndex < not defined") ;

   // Allocate and initialise the words array
   words = new WordPairLMEntry[vocab->nWords] ;
   for ( int i=0 ; i<vocab->nWords ; i++ )
   {
      words[i].word = i ;
      words[i].nSucWords = 0 ;
      words[i].sucWords = NULL ;
   }

   currPrevWordInd = -1 ;

   // Open the input word pair lm file
   FILE *fd ;
   if ( (fd = fopen( fName , "rb" )) == NULL )
      error("WordPairLM::WordPairLM - error opening input word pair LM file") ;

   // Ignore comments section if it is present
   char *str ;
   char *line = new char[10000] ;
   int readState = WPLM_READ_STATE_START ;
   while ( fgets( line , 10000 , fd ) != NULL )
   {
      switch ( readState )
      {
      case WPLM_READ_STATE_START:
         // We can get either the start of comment section, or the first word pair entry.
         if ( strstr( line , "/*" ) != NULL )
         {
            readState = WPLM_READ_STATE_INCOMMENTS ;
         }
         else
         {
            // Isolate first word in line
            str = strtok( line , "\r\n\t " ) ;
            if ( (str != NULL) && (strcmp( str , "" ) != 0) )
            {
               if ( str[0] != '>' )
                  error("WordPairLM::WordPairLM - in STATE_START, 1st word does not have >") ;
               str++ ;

               // Add this new prev word
               addPrevWord( str ) ;
               readState = WPLM_READ_STATE_INWORDS ;
            }
            // else empty line - stay in same state and read next line
         }
         break ;
      case WPLM_READ_STATE_INCOMMENTS:
         // We stay in this state until we get a "*/"
         if ( strstr( line , "*/" ) != NULL )
         {
            readState = WPLM_READ_STATE_INWORDS ;
         }
         break ;
      case WPLM_READ_STATE_INWORDS:
         // Isolate first word in line
         str = strtok( line , "\r\n\t " ) ;
         if ( (str != NULL) && (strcmp( str , "" ) != 0) )
         {
            if ( str[0] == '>' )
            {
               // New prev word
               str++ ;
               addPrevWord( str ) ;
            }
            else
            {
               addSucWord( str ) ;
            }
         }
         break ;
      default:
         error("WordPairLM::WordPairLM - invalid readState") ;
         break ;
      }
   }  
   delete [] line ;

   if ( readState != WPLM_READ_STATE_INWORDS )
      error("WordPairLM::WordPairLM - end of file but no entries encountered") ;

   fclose( fd ) ;
}