00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00025 #include <stdlib.h>
00026 #include "evallm.h"
00027 #include "idngram2lm.h"
00028
00029 double calc_prob_of(id__t sought_word,
00030 id__t *context,
00031 int context_length,
00032 ng_t *ng,
00033 arpa_lm_t *arpa_ng,
00034 fb_info *fb_list,
00035 int *bo_case,
00036 int *acl,
00037 flag arpa_lm) {
00038
00039 int i;
00040 flag exc_back_off;
00041 int most_recent_fb;
00042 int actual_context_length;
00043 id__t *sought_ngram;
00044 double prob;
00045
00046 exc_back_off = 0;
00047
00048 if (arpa_lm) {
00049 if (sought_word == 0 && arpa_ng->vocab_type == CLOSED_VOCAB) {
00050 quit(-1,"Error : Cannot generate probability for <UNK> since this is a closed \nvocabulary model.\n");
00051 }
00052 }
00053 else {
00054 if (sought_word == 0 && ng->vocab_type == CLOSED_VOCAB) {
00055 quit(-1,"Error : Cannot generate probability for <UNK> since this is a closed \nvocabulary model.\n");
00056 }
00057 }
00058
00059 most_recent_fb = -1;
00060
00061
00062
00063 for (i=context_length-1;i>=0;i--) {
00064
00065 if (fb_list[context[i]].backed_off) {
00066 most_recent_fb = i;
00067 if (fb_list[context[i]].inclusive) {
00068 exc_back_off = 0;
00069 }
00070 else {
00071 exc_back_off = 1;
00072 }
00073 i = -2;
00074 }
00075
00076 }
00077
00078 actual_context_length = context_length - most_recent_fb -1;
00079
00080 if (!exc_back_off && most_recent_fb != -1) {
00081 actual_context_length++;
00082 }
00083
00084 sought_ngram = (id__t *) rr_malloc(sizeof(id__t)*(actual_context_length+1));
00085
00086 for (i=0;i<=actual_context_length-1;i++) {
00087 if (exc_back_off) {
00088 sought_ngram[i] = context[i+most_recent_fb+1];
00089 }
00090 else {
00091 if (most_recent_fb == -1) {
00092 sought_ngram[i] = context[i+most_recent_fb+1];
00093 }
00094 else {
00095 sought_ngram[i] = context[i+most_recent_fb];
00096 }
00097 }
00098 }
00099 sought_ngram[actual_context_length] = sought_word;
00100
00101
00102 if (arpa_lm) {
00103 arpa_bo_ng_prob(actual_context_length,
00104 sought_ngram,
00105 arpa_ng,
00106 2,
00107 &prob,
00108 bo_case);
00109 }
00110 else {
00111 bo_ng_prob(actual_context_length,
00112 sought_ngram,
00113 ng,
00114 2,
00115 &prob,
00116 bo_case);
00117 }
00118
00119 *acl = actual_context_length;
00120
00121 free(sought_ngram);
00122
00123 return(prob);
00124
00125 }
00126