Source

bllip-parser / first-stage / PARSE / BchartSm.C

Full commit
  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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
 * Copyright 1999, 2005 Brown University, Providence, RI.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.  You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

#include "Bchart.h"
#include "Feature.h"
#include "FeatureTree.h"
#include "GotIter.h"
#include "utils.h"
#include "math.h"
#include "stdlib.h"
#include "string.h"
extern LeftRightGotIter globalGi[MAXNUMTHREADS];

void
Bchart::
makepUgT(ECString path)
{
  int i;
  int nm = Term::lastTagInt()+1;
  ECString pth(path);
  for(int f = 0 ; f < 20 ; f++)
    {
      ECString pUString(pth);
      pUString += "pUgT.txt";
      ifstream pUstream(pUString.c_str());
      if(!pUstream)
	{
	  cerr << "Could not find " << pUString << endl;
	  assert(pUstream);
	}
      for( i = 0 ; i <  nm ; i++ )
	{
	  int t;
	  pUstream >> t;
	  float p;
	  if(f == 0)
	    {
	      pUstream >> p;
	      pHugt(t) = p;
	      //cerr << "set pHugt " << t << " = " << p << endl;
	      pUstream >> p;
	      pHcapgt(t) = p;
	      pUstream >> p;
	      pHhypgt(t) = p;
	    }
	  else
	    {
	      pUstream >> p;
	      pHugt(t) += p;
	      //cerr << "set pHugt " << t << " = " << p << endl;
	      pUstream >> p;
	      pHcapgt(t) += p;
	      pUstream >> p;
	      pHhypgt(t) += p;
	    }
	}
    }
  for( i = 0 ; i <  nm ; i++ )
    {
      pHugt(i) /= 20.0;
      pHcapgt(i) /= 20.0;
      pHhypgt(i) /= 20.0;
    }
  ECString resultsString(path);
  resultsString += "pUgT.txt";
  ofstream     resultsStream(resultsString.c_str());
  assert(resultsStream);
  for(i = 0 ; i < nm ; i++)
    {
      resultsStream << i << "\t";
      resultsStream << pHugt(i) << "\t";
      resultsStream << pHcapgt(i) << "\t";
      resultsStream << pHhypgt(i) << "\n";
    }
}

void
Bchart::
readpUgT(ECString path)
{
  ECString pUstring(path);
  pUstring += "pUgT.txt";
  ifstream pUstream(pUstring.c_str());
  assert(pUstream);
  for( int i = 0 ; i <=  Term::lastNTInt() ; i++ )
    {
      int t;
      pUstream >> t;
      float p;
      pUstream >> p;
      pHugt(t) = p;
      //cerr << "set pHugt " << t << " = " << p << endl;
      pUstream >> p;
      if(p == 0) p = .00001;  //Anything might be capitalized;
      pHcapgt(t) = p;
      pUstream >> p;
      pHhypgt(t) = p;
    }
}

void
Bchart::
readTermProbs(ECString& path)
{
  int i;
  for(i = 0 ; i < MAXSENTLEN ; i++) stops[i] = new Item(Term::stopTerm, i, i);
  //makepUgT(path); //???;
  readpUgT(path);
    
  ECString pTstring(path);
  pTstring += "endings.txt";
  ECString ppTstring(path);
  ppTstring += "nttCounts.txt";
  ifstream pTstream(pTstring.c_str());
  assert(pTstream);
  ifstream ppTstream(ppTstring.c_str());
  assert(ppTstream);

  int numpT;
  pTstream >> numpT;
  pHegt_ = new Wwegt[numpT];
  egtSize_ = numpT;
  i = 0;
  while(pTstream)
    {
      int t;
      ECString  e;
      float p;
      pTstream >> t;
      if(!pTstream) break;
      assert(i < numpT);
      pTstream >> e;
      pTstream >> p;
      pHegt_[i].t = t;
      pHegt_[i].e = e;
      pHegt_[i].p = p;
      i++;
    }
  /* read in counts of each non-term to get p(non-term) in pT */
  int sumTag = 0;
  int sumNT = 0;
  double nums[MAXNUMNTTS];
  for(i = 0 ; i < MAXNUMNTTS ; i++) nums[i] = 0;
  while(ppTstream)
    {
      int t;
      ppTstream >> t;
      if(!ppTstream) break;
      
      assert(t < MAXNUMNTTS);
      ppTstream >> nums[t];
      if(t > Term::lastTagInt()) sumNT += nums[t];
      else sumTag += nums[t];
    }
  float sumTagf = (float)sumTag;
  float sumNTf = (float)sumNT;
  for(i = 0 ; i < MAXNUMNTTS ; i++)
    {
      if(nums[i] == 0) continue;
      float divisor = (i < Term::lastTagInt()+1) ? sumTagf : sumNTf;
      pT(i) = nums[i]/divisor;
    }
  ECString wlistString(path);
  //wlistString += "probSum.txt";
  wlistString += "pSgT.txt";
  ifstream wlistStream(wlistString.c_str());
  assert(wlistStream);
  int wnum = 0;
  ECString w;
  wlistStream >> w;  //first entry is number of entries
  lastKnownWord = atoi(w.c_str())-1;
  while(wlistStream)
    {
      wlistStream >> w;
      if (!wlistStream) break;
      ECString dummy;
      WordAndPresence wap;
      // if we see a vocabulary hole, we increment the word counter and move on
      if (w == "**VocabHole**") {
          wlistStream >> dummy; // the word that would fill this hole
          wap.second = false; // this is a hole
      }
      else {
          wap.second = true; // real word (not a hole)
          for( ; ; )
            {
              wlistStream >> dummy;
              if(dummy == "|") break;
              int trmInt = atoi(dummy.c_str());
              float prb;
              wlistStream >> prb;
              if(prb < .001) continue;
              const Term* trm = Term::fromInt(trmInt);
              assert(trm);
              if(trm->terminal_p() == COLON) Term::Colons.push_back(w);
              else if(trm->terminal_p() == FINAL) Term::Finals.push_back(w);
            }
          int cnt;
          wlistStream >> cnt;
      }

      /* TODO confirm that invWordMap is okay for holes */
      invWordMap[wnum] = w;
      wap.first = wnum;
      wordMap[w] = wap;
      wnum++;
    }
}

void
Bchart::
initDenom()
{
  static int startState = -1;

  int eosInt = Term::stopTerm->toInt();
  /* we compute p(w_0,i t^j) in parray[j][1],
     then move it to parray[j][0].
     each time we also compute the component of p(w_0,n) due to p(w_i)
     = p(w_0,i)/p(w_0,i-1) and put this in denomProbs[i] */
  float parray[MAXNUMNTS][2];
  int i, j;
  for(i = 0 ; i < MAXNUMNTS ; i++)
    for(j = 0 ; j < 2 ; j++)
      parray[i][j] = 0;
  for(i = 0 ; i < MAXSENTLEN ; i++)
    denomProbs[i] = 0;
  
  if(startState < 0)
    startState = eosInt; 
  parray[startState][0] = 1;
  assert(wrd_count_ < 1000);
  /* compute p(w_0,n t) for all n */
  for(i = 0 ; i < wrd_count_ ; i++)
    {
      float pw0n = 0; // = p(w_{0,i}) = sum_j p(w_{0,i}, j)
      /* for n = i, compute p(w_0,n t) for all t */

      list<float>& wpl = wordPlist(&(sentence_[i]), i);
      list<float>::iterator wpli = wpl.begin();
      for( ; wpli != wpl.end() ; wpli++)
	{	
	  float pw0nt = 0;
	  int trmInt = (int)(*wpli);
	  wpli++;
	  float prb = *wpli;
	  if(prb == 0) cerr << "Zero prob from wordPlist, "
	    << sentence_[i] << ", " << trmInt << endl;
	  assert(prb >= 0);
	  for(int k = 0 ; k < MAXNUMNTS ; k++)
	    {
	      float pk = parray[k][0];
	      if(pk == 0) continue;
	      float smb = computepTgT(k,trmInt);

	      // allow labels in terms.txt which do not appear in the training corpus
	      //assert(smb > 0);
	      if(smb==0) continue;

	      assert(pk > 0);
	      pw0nt += pk * prb * smb;
	    }
	  parray[trmInt][1] = pw0nt;
	  if(printDebug(1000))
	    cerr << "initD " << i << "\t" << trmInt << "\t" << pw0nt << endl;
	  pw0n += pw0nt;
	}

      if(pw0n == 0)
	{
	  cerr << "Zero at pos " << i << " word = "
	   << sentence_[i] << endl;
	  /*If we get here it means that all of the beta values are zero.
	    So assign them .00001 * P(word|tag) as a default to avoid
	    all zeros */
	  wpli = wpl.begin();
	  for( ; wpli != wpl.end() ; wpli++)
	    {
	      int trmInt = (int)(*wpli);
	      wpli++;
	      float prb = *wpli;
	      float pw0nt = prb * .00001;
	      cerr << "Assigning " << trmInt << " prob = " << pw0nt << endl;
	      parray[trmInt][1] = pw0nt;
	      pw0n += pw0nt;
	    }
	}
      assert(pw0n > 0);
      /* now compute the pwarray value we care about */
      denomProbs[i] = pw0n;
      if(printDebug(1000))
	cerr << "denomProb " << i << " = " << pw0n << endl;
      /* and now transfer the values from the [1] column to the [0] */
      for(j = 0 ; j < MAXNUMNTS ; j++)
	{
	  /* each time through we devide probs by p(wi-1|w_0,i-1), and thus
	     all of the figures are p(...|w_o,i) */
	  parray[j][0] = parray[j][1]/pw0n;
	  /* I hope this is something like the relative prob of the tag
	     being t^j at this point in the sent */
	  parray[j][1] = 0;
	}

      wpli = wpl.begin();
      for( ; wpli != wpl.end() ; wpli++) 
	{
	  int trmInt = (int)(*wpli);
	  wpli++;
	  float prb = *wpli;
	  prb /= denomProbs[i];

	  const Term*  possTerm = Term::fromInt(trmInt);
	  //Item *item =  new Item(possTerm, i, i + 1);
	  Item * item = addtochart(possTerm);
	  item->start() = i;
	  item->finish() = i+1;
	  item->word() = &sentence_[i];
	  item->prob() = prb; 
	  item->prob() *= 1.2;  // 1.1 factor to overcome bigram superiority;
	  Edge* nEdge = new Edge(*item);   
	  // this next is a hack so that that the merit of nEdge will come
	    // out right/
	  nEdge->leftMerit() = parray[trmInt][0]/item->prob(); 
	  //cerr << "plstop for " << *item << " = "
	  //<< parray[trmInt][0] << " / " << item->prob()
	  //<< " = " << nEdge->plstopGt() << endl;
	  nEdge->setmerit();
	  heap->insert(nEdge); 
	  ++ruleiCounts_;
	}
    }

  /* finally, compute the dummy p(dummy eos | prev)
     = sum_i p(w,t^i|prev) * p(eos | t^i) */
  float ans = 0;
  for(i = 0 ; i <= Term::lastTagInt() ; i++)
    {
      float pwti = parray[i][0];
      if(pwti == 0) continue;
      float sbg = computepTgT(i, eosInt);
      ans += sbg * pwti;
    }
  if(printDebug(1000))
    cerr << "initD " << wrd_count_ << "\tSTOP("<<eosInt<<")\t" << ans << endl;
  denomProbs[wrd_count_] = ans;
}

int
Bchart::
wtoInt(ECString& w)
{
  map<ECString, WordAndPresence, less<ECString> >::iterator wordMapIter = 
        wordMap.find(w);
  if (wordMapIter != wordMap.end()) {
        /* word is in wordMap, now check to see if it is a hole */
        WordAndPresence wap = (*wordMapIter).second;
        if (wap.second)
            return wap.first;
  }

  map<ECString, int, less<ECString> >::iterator newWordMapIter = 
        newWordMap[thrdid].find(w);
  if (newWordMapIter != newWordMap[thrdid].end()) return (*newWordMapIter).second;
  lastWord[thrdid]++;
  newWordMap[thrdid][w] = lastWord[thrdid];
  newWords[thrdid].push_back(w);
  return lastWord[thrdid];
}

ECString
Bchart::
intToW(int n)
{
  if(n <= lastKnownWord) return invWordMap[n];
  else return newWords[thrdid][n-lastKnownWord-1];
}

list<float>&
Bchart::
wordPlist(Wrd* word, int word_num)
{
  list<float>& ans = wordPlists[word_num];
  if(!ans.empty()) return ans;
  if(printDebug(500))
    cerr << "wordPlist " << *word << endl;
  char temp[1024];
  ECString head(word->lexeme());
  ECString headL(langAwareToLower(head.c_str(),temp));
  int wint = wtoInt(headL); 
  //cerr << "WTI " << headL << " " << wint << endl;
  word->toInt() = wint;
  if(word->lexeme() == Bchart::HEADWORD_S1)
    {
      ans.push_back(Term::stopTerm->toInt());
      ans.push_back(1.0);
      return ans;
    }
  if(!extraPos.empty()){
    assert(word_num<(int)extraPos.size());
    vector<const Term*>& vct=extraPos[word_num];
    float prb=1.0;
    if(!vct.empty()){
      vector<const Term*>::iterator vctI=vct.begin();
      for(;vctI!=vct.end();vctI++){
	const Term* trm=*vctI;
	int trmint=trm->toInt();
	prb/=2.0;
	ans.push_back((float)trmint);
	ans.push_back(prb);
      }
      return ans;
    }
  }
      
  if( wint <= lastKnownWord )
    {
      int i;
      for( i = 0 ; i <= Term::lastNTInt() ; i ++ )
	{
	  if(guided && !inGuide(word_num,word_num+1,i)) continue;
	  float pwgt = pHst(wint,i);
	  //cerr << "pwgt " << i << " " << pwgt << endl;
	  if(pwgt == 0) continue;
	  float prob = psktt(word,i); 
	  if(prob == 0) continue;
	  assert( prob > 0);
	  ans.push_back((float)i);
	  ans.push_back(prob);
	  //if(printDebug(7777))
	}
      if(!ans.empty())
	{
	  return ans;
	}
    }
  // in the case of a word that is only known as an NNPS, but we see it
  // uncapitalized, the above will assign 0 prob and ans will be empty.       
  // if this happens, we treat it like an unknown word.;
  for(int i = 0 ; i <= Term::lastTagInt() ; i++)
    {
      if(i == Term::stopTerm->toInt()) continue;
      float phut = pHugt(i);
      if(phut == 0) continue;
      float prob = psutt(word,i);
      if(prob == 0) continue;
      assert(prob > 0);
      if(printDebug(7777)) cerr << "Uk\t" << i << "\t" << prob << endl;
      ans.push_back((float)i);
      ans.push_back(prob);
      //cerr <<word_num<< "\t" << i << "\t" << prob << endl;
    }
  return ans;
}


double
Bchart::
psktt(Wrd* shU, int t)
{
  double ans = pHst(shU->toInt(), t); 
  double phcp = 1;
  phcp = pCapgt(shU,t);
  ans *= phcp;
  double put = pHugt(t);
  ans *= (1-put);
  if(ans < 0)
    {
      cerr << phcp << " " << put << endl;
      cerr << "psktt( " << *shU << " | " << t << " ) = " << ans << endl;
      assert(ans >= 0);
    }
  return ans;
}

double
Bchart::
pCapgt(const Wrd* shU, int t)
{
  if(Bchart::caseInsensitive) return 1.0;
  if (Term::Language == "Ch" || Term::Language == "Ar") 
    return 1.0;
  int word_num = shU->loc();
  const ECString& lex0 = sentence_[0].lexeme();
  if(word_num == 0) return 1;
  else if(word_num == 1 &&
	  (lex0 == "``" || lex0 == "-LCB-" || lex0 == "-LRB-"))
    return 1;
  //cerr << "pCapgt = " << pcap << endl;
  if(shU->lexeme().length() < 2) return 1;  //ignore words of length 1;
  char temp[1024];
  ECString sh(langAwareToLower(shU->lexeme().c_str(),temp));
  bool cap = false;
  /* if all caps, ignore capitalization evidence */
  if(shU->lexeme()[0] != sh[0] && shU->lexeme()[1] != sh[1]) return 1;
  if(shU->lexeme()[0] != sh[0] && shU->lexeme()[1] == sh[1]) cap = true;
  double pcap = pHcapgt(t);  
  return cap ? pcap : (1 - pcap);
}

float
Bchart::
pHst(int wordInt, int t)
{
  assert(wordInt >= 0);
  FeatureTree* strt = FeatureTree::roots(HCALC);
  assert(strt);
  FeatureTree* histPt = strt->follow(t, 0);
  if(!histPt) return 0;
  Feat* ft = histPt->feats.find(wordInt);
  if(!ft) return 0;
  else return ft->g();
}


double
Bchart::
psutt(const Wrd* shU, int t)
{
  //cerr << "Unknown word: " << shU << " for tag: " << t << endl; 
  double ans = pHugt(t);
  //cerr << "pHugt = " << ans << endl;
  assert(ans >= 0);
  if(ans == 0) return 0;
  double phyp = 1;
  if (Term::Language != "Ch" && Term::Language != "Ar") 
        phyp = pHypgt(shU->lexeme(), t);
  ans *= phyp;
  //cerr << "pHypgt = " << phyp << endl;
  double phcp = 1;
  if(Term::Language != "Ch" && Term::Language != "Ar") 
        phcp = pCapgt(shU,t);
  ans *= phcp;
  ans *= .0001;
  assert(ans >= 0);
  if(Term::fromInt(t)->openClass())
    {
      char temp[1024];
      ECString sh(langAwareToLower(shU->lexeme().c_str(),temp));
      float phegt = pegt(sh,t);
      if(phegt == 0) phegt = .00001;
      ans *= phegt;
    }
  else
    ans *= .0001;
  ans *= 600;
  assert(ans >= 0);
  //cerr << "psutt( " << shU << " | " << t << " ) = " << ans << endl;
  return ans;
}

int 
Bchart::
bucket(float val)
{
  for(int i = 0 ; i < 14 ; i++)
    if(val <= bucketLims[i])
      return i;
  return 14;
}

int 
Bchart::
bucket(float val, int whichInt, int whichFt)
{
  assert(whichInt < Feature::numCalcs);
  assert(whichFt < MAXNUMFS);
  float logFac = Feature::logFacs[whichInt][whichFt];
  float lval = logFac *log(val);
  int lvi = (int)lval;
  lvi++;
  if(lvi <= 14) return lvi;
  return 14;
}

double
Bchart::
pHypgt(const ECString& shU, int t)
{
  //return 1.0  //ADD to IGNORE hypenization for unknown words
  bool hyp = false;
  const char* hyppos = strpbrk(shU.c_str(), "-");
  if (hyppos) hyp = true;
  double phyp = pHhypgt(t);  
  return hyp ? phyp : (1 - phyp);
}

float
Bchart::
pegt(ECString& sh, int t)
{
  //return 1.0  //ADD to IGNORE endings for unknown words
  int len = sh.length();
  if(len < 3) return .01;
  ECString e(sh,len-2,2);
  float phegt = pHegt(e,t);
  //cerr << "pegt( " << sh <<", " << e << " | " << t << " ) = " << phegt << endl;
  return phegt;
}

float
Bchart::
pHegt(ECString& es, int t)
{
  int top = egtSize_;
  int bot = -1;
  for( ; ; )
    {
      if( top <= bot+1 )
	return 0.0;
      int mid = (top+bot)/2;
      Wwegt& midH = pHegt_[mid];
      
      int gt =  greaterThan(midH, es, t);
      if(gt  == 0) return midH.p;
      else if( gt == 1 ) top = mid;
      else bot = mid;
    }
}

int
Bchart::
greaterThan(Wwegt& wwegt, ECString e, int t)
{
  int ans = 0;
  if(wwegt.t < t) ans = -1;
  else if(wwegt.t > t) ans = 1;
  else if(wwegt.e < e) ans = -1;
  else if(wwegt.e > e) ans = 1;
  return ans;
}

float
Bchart::
computepTgT(int t1,int t2)
{
  FullHist fh;
  fh.preTerm = t1;
  fh.term = t2;
  fh.cb = this;
  return meFHProb(Term::fromInt(t2), fh, TTCALC);
}

float
Bchart::
computeMerit(Edge* edge, int whichDist)
{
  float ans = 0;  //accumulate the sum here;
  FullHist fh(edge);
  fh.cb = this;
  int denomPos = edge->loc();
  if(whichDist == LMCALC) denomPos = edge->start()-1;
  float denom = 0.1;  // ??? should be 1, but merit hs a problem with start.
  if(denomPos >= 0) denom = denomProbs[denomPos];
  if(denom <= 0)
    {
      /* attempted fix by dmcc */
      return 0.000001;
      cerr << denomPos << " " << *edge << endl;
      assert(denom > 0);
    }
  /* p(w|nt) = sum_t p(w|t)p(t|nt) where w is the word following item */
  if(denomPos == wrd_count_ || denomPos < 0)
    {
      /* if w is the word preceeding or
	 following the sentence, it is a pretend word
	 with only one part of speech, STOP */
      fh.term = Term::stopTerm->toInt();
      ans = meFHProb(Term::stopTerm, fh, whichDist);
    }
  else
    {
      Wrd& w = sentence_[denomPos];
      list<float>& wpl = wordPlist(&w, denomPos);
      list<float>::iterator wpli = wpl.begin();
      for( ; wpli != wpl.end() ; wpli++)
	{
	  int termInt = (int)(*wpli);
	  const Term* nxtTerm = Term::fromInt(termInt);
	  wpli++;
	  float  pwgt = *wpli;
	  float phtgnt = meFHProb(nxtTerm, fh, whichDist);
	  ans += pwgt * phtgnt;
	}
    }
  ans *= 1.5;
  ans /= denom;
  if(whichDist == LMCALC) ans *= pT(edge->lhs()->toInt());
  if(printDebug(150)) cerr << "cM" << whichDist << " = " << ans << endl;
  if(smallCorpus && ans == 0) ans = .000001;
  return ans;
}