00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <string.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <math.h>
00025 #include <assert.h>
00026 #include "mfstream.h"
00027 #include "mempool.h"
00028 #include "htable.h"
00029 #include "dictionary.h"
00030 #include "n_gram.h"
00031 #include "mempool.h"
00032 #include "ngramtable.h"
00033 #include "ngramcache.h"
00034 #include "normcache.h"
00035 #include "interplm.h"
00036 #include "mdiadapt.h"
00037 #include "linearlm.h"
00038
00039
00040
00041
00042
00043
00044 linearwb::linearwb(char* ngtfile,int depth,int prunefreq,TABLETYPE tt):
00045 mdiadaptlm(ngtfile,depth,tt)
00046 {
00047 prunethresh=prunefreq;
00048 cerr << "PruneThresh: " << prunethresh << "\n";
00049
00050 };
00051
00052
00053
00054
00055 int linearwb::train()
00056 {
00057
00058 trainunigr();
00059
00060
00061 gensuccstat();
00062
00063 return 1;
00064 }
00065
00066
00067 int linearwb::discount(ngram ng_,int size,double& fstar,double& lambda,int cv)
00068 {
00069
00070 ngram ng(dict);
00071 ng.trans(ng_);
00072
00073 if (size > 1) {
00074
00075 ngram history=ng;
00076
00077 if (ng.ckhisto(size) && get(history,size,size-1) && (history.freq>cv) &&
00078
00079 ((size < 3) || ((history.freq-cv) > prunethresh))) {
00080
00081
00082
00083
00084 if (get(ng,size,size) && (!prunesingletons() || ng.freq>1 || size<3)) {
00085
00086
00087
00088 cv=(cv>ng.freq)?ng.freq:cv;
00089
00090 if (ng.freq >cv) {
00091
00092 fstar=(double)(ng.freq-cv)/(double)(history.freq -cv + history.succ);
00093
00094 lambda=(double)history.succ/(double)(history.freq -cv + history.succ);
00095
00096 if (size>=3 && prunesingletons())
00097 lambda+=(double)succ1(history.link)/(double)(history.freq -cv + history.succ);
00098
00099
00100
00101 } else {
00102
00103 fstar=0.0;
00104
00105 lambda=(double)(history.succ-1)/
00106 (double)(history.freq - cv + history.succ - 1);
00107
00108 if (size>=3 && prunesingletons())
00109
00110 lambda+=(double)succ1(history.link)-(cv==1 && ng.freq==1?1:0)/(double)(history.freq -cv + history.succ -1);
00111
00112 }
00113 } else {
00114
00115 fstar=0.0;
00116
00117 lambda=(double)history.succ/(double)(history.freq + history.succ);
00118
00119 if (size>=3 && prunesingletons())
00120 lambda+=(double)succ1(history.link)/(double)(history.freq + history.succ);
00121 }
00122
00123
00124
00125
00126
00127 if (*ng.wordp(1)==dict->oovcode()) {
00128 lambda+=fstar;
00129 fstar=0.0;
00130 assert(lambda<=1 && lambda>0);
00131 } else {
00132 *ng.wordp(1)=dict->oovcode();
00133 if (get(ng,size,size) && (!prunesingletons() || ng.freq>1 || size<3))
00134 lambda+=(double)ng.freq/(double)(history.freq - cv + history.succ);
00135 }
00136 } else {
00137 fstar=0;
00138 lambda=1;
00139 }
00140 } else {
00141 fstar=unigr(ng);
00142 lambda=0;
00143 }
00144
00145 return 1;
00146 }
00147
00148