File: LMInteriorLevelWordList.cc

package info (click to toggle)
torch3 3.1-0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,940 kB
  • ctags: 2,744
  • sloc: cpp: 24,245; python: 299; makefile: 153
file content (225 lines) | stat: -rw-r--r-- 7,616 bytes parent folder | download | duplicates (5)
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
216
217
218
219
220
221
222
223
224
225
// 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 "LMInteriorLevelWordList.h"
#include "log_add.h"


namespace Torch {


LMInteriorLevelWordList::LMInteriorLevelWordList( int level_ )
{
    if ( level_ < 1 )
        error("LMInteriorLevelWordList::LMInteriorLevelWordList - cannot have level_ < 1\n") ;
    level = level_ ;
    n_entries = 0 ;
    entries = NULL ;
}


LMInteriorLevelWordList::~LMInteriorLevelWordList()
{
    if ( entries != NULL )
    {
        for ( int i=0 ; i<n_entries ; i++ )
            delete entries[i] ;
        free( entries ) ;
    }
}


void LMInteriorLevelWordList::addProbEntry( int order , int *words , real prob )
{
    // 'words' is an array of word indices.  There should be 'order' elements
    //    in this array and the ordering should be, eg, [w3 w2 w1 w4] for 4-gram.

    LMInteriorLevelWordEntry *new_word=NULL ;
    
#ifdef DEBUG
    if ( (order-1) > level )
        error("LMInteriorLevelWordList - addEntry - order of new entry must be <= level\n") ;
#endif
    
    // Do we have an entry for the word at this level ?
    if ( (new_word = findWord( *words )) == NULL )
    { 
        // No we don't so we need to create an entry and add it to our 'entries' array.
        entries = (LMInteriorLevelWordEntry **)Allocator::sysRealloc( entries , 
                             (n_entries+1)*sizeof(LMInteriorLevelWordEntry *) ) ;
        new_word = new LMInteriorLevelWordEntry( *words ) ;

        // If the list is empty, just insert it straight off.  Also
        //   do an initial check to see if the word belongs at the end of the array
        if ( (n_entries == 0) || (new_word->word > entries[n_entries-1]->word) )
        {
            entries[n_entries] = new_word ;
        }
        else
        {
            // Find the place in the list of words where we want to insert the new word
            for ( int i=0 ; i<n_entries ; i++ )
            {
                if ( (*words) < entries[i]->word ) 
                {
                    // Shuffle down all words from i onwards and place the
                    //   new word in position i.
                    memmove( entries+i+1 , entries+i , 
                             (n_entries-i)*sizeof(LMInteriorLevelWordEntry *) ) ;
                    entries[i] = new_word ;
                    break ;
                }
            }
        }

        n_entries++ ;
    }

    // 'new_word' now points to the entry for the word corresponding to this level.
    // Continue adding our entry.
    new_word->addProbEntry( level , order-1 , words+1 , prob ) ;
}


void LMInteriorLevelWordList::addBackoffEntry( int order , int *words , real bo_wt )
{
    LMInteriorLevelWordEntry *new_word ;
    
    // There should be 'order' entries in 'words' and the order should be
    //   most-recent word first. eg. [w4 w3 w2 w1] for 4-gram
#ifdef DEBUG
    if ( (order > level) || (order < 1) )
        error("LMInteriorLevelWordList::addBackoffEntry - order out of range\n") ;
#endif

    if ( (new_word = findWord( *words )) == NULL )
    {
        // No we don't so we need to create an entry and add it to our 'entries' array.
        entries = (LMInteriorLevelWordEntry **)Allocator::sysRealloc( entries , 
                                         (n_entries+1)*sizeof(LMInteriorLevelWordEntry *) ) ;
        new_word = new LMInteriorLevelWordEntry( *words ) ;

        // If the list is empty, just insert it straight off.  Also
        //   do an initial check to see if the word belongs at the end of the array
        if ( (n_entries == 0) || (new_word->word > entries[n_entries-1]->word) )
        {
            entries[n_entries] = new_word ;
        }
        else
        {
            // Find the place in the list of words where we want to insert the new word
            for ( int i=0 ; i<n_entries ; i++ )
            {
                if ( entries[i]->word > new_word->word )
                {
                    // Shuffle down all words from i onwards and place the
                    //   new word in position i.
                    memmove( entries+i+1 , entries+i , 
                             (n_entries-i)*sizeof(LMInteriorLevelWordEntry *) ) ;
                    entries[i] = new_word ;
                    break ;
                }
            }
        }
        n_entries++ ;
    }

    new_word->addBackoffEntry( level , order-1 , words+1 , bo_wt ) ;
}


bool LMInteriorLevelWordList::getProbWithBackoff( int order , int *words , real *prob )
{
    LMInteriorLevelWordEntry *word_entry ;
    
    // There should be 'order' entries in 'words'.  The ordering should be eg.
    //   W3,W2,W1,W4 when order=4
    if ( (word_entry = findWord( *words )) != NULL )
        return word_entry->getProbWithBackoff( order-1 , words+1 , prob ) ;
    else
    {
        *prob = 0.0 ;
        return false ;
    }
}


LMInteriorLevelWordEntry *LMInteriorLevelWordList::findWord( int word_ )
{
    // We assume that the list of words is in ascending order so 
    //   that we can do a binary search.
    int min=0 , max=(n_entries-1) , curr_pos=0 ;
    
    if ( n_entries == 0 )
        return NULL ;

    if ( n_entries <= 10 )
    {
        // just do a linear search
        for ( int i=0 ; i<n_entries ; i++ )
        {
            if ( word_ < entries[i]->word )
                return NULL ;
            else if ( word_ == entries[i]->word )
                return entries[i] ;
        }
    }
    else
    {
        // do a binary search
        while (1)
        {
            curr_pos = min+(max-min)/2 ;
            if ( word_ < entries[curr_pos]->word )
                max = curr_pos-1 ;
            else if ( word_ > entries[curr_pos]->word )
                min = curr_pos+1 ;
            else
                return entries[curr_pos] ;

            if ( min > max )
                return NULL ;
        }
    }
    return NULL ;
}


#ifdef DEBUG
void LMInteriorLevelWordList::outputText( Vocabulary *vocab , int *words , int n_words )
{
    for ( int i=0 ; i<n_entries ; i++ )
        entries[i]->outputText( vocab , words , n_words ) ;
}
#endif


}