1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
// Copyright (C) 2003--2004 Darren Moore (moore@idiap.ch)
//
// This file is part of Torch 3.1.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Allocator.h"
#include "LMNGram.h"
namespace Torch {
LMNGram::LMNGram( int n_ , Vocabulary *vocab_ )
{
#ifdef DEBUG
if ( n_ < 1 )
error("LMNGram::LMNGram - n cannot be < 1\n") ;
#endif
n = n_ ;
vocab = vocab_ ;
// Create a unigram entry for every word in our vocabulary.
unigrams = (real *)Allocator::sysAlloc( vocab->n_words * sizeof(real) ) ;
for ( int i=0 ; i<vocab->n_words ; i++ )
unigrams[i] = LOG_ZERO ;
if ( n > 1 )
next_level = new LMInteriorLevelWordList( n-1 ) ;
else
next_level = NULL ;
// Configure the cache - TODO make the cache size configurable (?)
cache = new LMCache( 10 , n , vocab->n_words ) ;
}
LMNGram::~LMNGram()
{
if ( unigrams != NULL )
free(unigrams) ;
if ( next_level != NULL )
delete next_level ;
if ( cache != NULL )
delete cache ;
}
void LMNGram::addEntry( int order , int *words , real prob , real bo_wt )
{
// We assume that the 'words' array is in oldest-word-first order.
// ie. [w1 w2 w3] for trigram entry.
int r_words_1[30] ; // rearranged words [w4,w3,w2,w1]
int r_words_2[30] ; // rearranged words [w3,w2,w1,w4]
#ifdef DEBUG
if ( (order < 1) || (order > n) )
error("LMNGram::addEntry - order out of range\n") ;
#endif
if ( order == 1 )
{
if ( unigrams[*words] > LOG_ZERO )
error("LMNGram::addEntry - duplicate unigram entry encountered\n") ;
unigrams[*words] = prob ;
if ( n > 1 )
{
// We are adding a unigram entry into a language model
// of higher order, so the backoff weight is important.
// Add the backoff weight.
next_level->addBackoffEntry( order , words , bo_wt ) ;
}
}
else
{
// The first thing we want to do is to rearrange the words in the entry
// so that the order is straight forward and matches the architecture
// of the language model, for adding both probs and backoffs.
for ( int i=0 ; i<(order-1) ; i++ )
{
r_words_1[i] = words[order-1-i] ; // backoff ordering
r_words_2[i] = words[order-2-i] ; // prob ordering
}
r_words_1[order-1] = words[0] ;
r_words_2[order-1] = words[order-1] ;
if ( prob > LOG_ZERO )
next_level->addProbEntry( order , r_words_2 , prob ) ;
if ( order < n )
{
// Add the backoff - we don't have/need, eg, trigram backoffs in our
// trigram language model.
next_level->addBackoffEntry( order , r_words_1 , bo_wt ) ;
}
}
}
real LMNGram::getLogProbBackoff( int order , int *words )
{
// There are 'order' entries in 'words'.
// The ordering in words is W3,W2,W1,W4 for a 4-gram query.
// ie. for the query : what is P(W4|W1,W2,W3) ?
real temp , prob ;
#ifdef DEBUG
if ( order < 1 )
error("LMNGram::getNextWordList - order out of range\n") ;
bool output_debug=false ;
if ( output_debug == true )
{
printf( "P( %s | " , vocab->words[words[order-1]] ) ;
for ( int i=0 ; i<(order-1) ; i++ )
printf( "%s " , vocab->words[words[i]] ) ;
printf(") = ") ;
}
#endif
if ( order > n )
order = n ;
if ( order == 1 )
{
// Just return the unigrams prob - no need to backoff or to use the cache.
#ifdef DEBUG
if ( output_debug == true )
printf("%f\n",unigrams[*words]);
#endif
return unigrams[*words] ;
}
else
{
// look in the cache ...
prob = cache->getProb( order , words ) ;
if ( prob >= (-LOG_ZERO) )
{
// The n-gram entry is not in the cache, so we need to search for it
// and then add it to the cache.
if ( next_level->getProbWithBackoff( order , words , &prob ) == false )
{
// No bigram, trigram, etc entries so backoff to unigram
temp = unigrams[words[order-1]] ;
if ( temp <= LOG_ZERO )
prob = LOG_ZERO ;
else if ( prob <= LOG_ZERO )
prob = temp ;
else
prob += temp ;
}
cache->addEntry( order , words , prob ) ;
#ifdef DEBUG
if ( output_debug == true )
printf("%f\n",prob) ;
#endif
return prob ;
}
else
{
// The entry was in the cache. Just return it
#ifdef DEBUG
if ( output_debug == true )
printf("(c)%f\n",prob);
#endif
return prob ;
}
}
}
#ifdef DEBUG
void LMNGram::outputText()
{
int words[30] ;
// Print the unigrams
printf("\\1-gram\\\n") ;
for ( int i=0 ; i<vocab->n_words ; i++ )
printf("%s %f\n",vocab->words[i],unigrams[i]) ;
// Print the rest
if ( next_level != NULL )
next_level->outputText( vocab , words , 0 ) ;
}
#endif
}
|