summaryrefslogtreecommitdiff
path: root/decoder/lattice.cc
blob: 956e12b417216722e9f4bd6728cb7b67e3ca7e3b (plain)
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
#include "lattice.h"

#include "tdict.h"
#include "hg_io.h"

using namespace std;

static const int kUNREACHABLE = 99999999;

void Lattice::ComputeDistances() {
  const int n = this->size() + 1;
  dist_.resize(n, n, kUNREACHABLE);
  for (int i = 0; i < this->size(); ++i) {
    const vector<LatticeArc>& alts = (*this)[i];
    for (int j = 0; j < alts.size(); ++j)
      dist_(i, i + alts[j].dist2next) = 1;
  }
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        const int dp = dist_(i,k) + dist_(k,j);
        if (dist_(i,j) > dp)
          dist_(i,j) = dp;
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    int latest = kUNREACHABLE;
    for (int j = n-1; j >= 0; --j) {
      const int c = dist_(i,j);
      if (c < kUNREACHABLE)
        latest = c;
      else
        dist_(i,j) = latest;
    }
  }
  // cerr << dist_ << endl;
}

bool LatticeTools::LooksLikePLF(const string &line) {
  return (line.size() > 5) && (line.substr(0,4) == "((('");
}

void LatticeTools::ConvertTextToLattice(const string& text, Lattice* pl) {
  Lattice& l = *pl;
  vector<WordID> ids;
  TD::ConvertSentence(text, &ids);
  l.resize(ids.size());
  for (int i = 0; i < l.size(); ++i)
    l[i].push_back(LatticeArc(ids[i], 0.0, 1));
}

void LatticeTools::ConvertTextOrPLF(const string& text_or_plf, Lattice* pl) {
  if (LooksLikePLF(text_or_plf))
    HypergraphIO::PLFtoLattice(text_or_plf, pl);
  else {
    ConvertTextToLattice(text_or_plf, pl);
    pl->is_sentence_ = true;
  }
  pl->ComputeDistances();
}