Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PhraseDictionaryTransliteration.cpp « TranslationModel « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2ffe880c726b277396d0af53755389aed75310f5 (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
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
// vim:tabstop=2
#include <cstdlib>

#include "PhraseDictionaryTransliteration.h"
#include "moses/DecodeGraph.h"
#include "moses/DecodeStep.h"
#include "util/tempfile.hh"

using namespace std;

namespace Moses
{
PhraseDictionaryTransliteration::PhraseDictionaryTransliteration(const std::string &line)
  : PhraseDictionary(line, true)
{
  ReadParameters();
  UTIL_THROW_IF2(m_mosesDir.empty() ||
                 m_scriptDir.empty() ||
                 m_externalDir.empty() ||
                 m_inputLang.empty() ||
                 m_outputLang.empty(), "Must specify all arguments");
}

void PhraseDictionaryTransliteration::Load(AllOptions::ptr const& opts)
{
  m_options = opts;
  SetFeaturesToApply();
}

void PhraseDictionaryTransliteration::CleanUpAfterSentenceProcessing(const InputType& source)
{
  ReduceCache();
}

void PhraseDictionaryTransliteration::GetTargetPhraseCollectionBatch(const InputPathList &inputPathQueue) const
{

  InputPathList::const_iterator iter;
  for (iter = inputPathQueue.begin(); iter != inputPathQueue.end(); ++iter) {
    InputPath &inputPath = **iter;

    if (!SatisfyBackoff(inputPath)) {
      continue;
    }

    const Phrase &sourcePhrase = inputPath.GetPhrase();

    if (sourcePhrase.GetSize() != 1) {
      // only translit single words. A limitation of the translit script
      continue;
    }

    GetTargetPhraseCollection(inputPath);
  }
}

void
PhraseDictionaryTransliteration::
GetTargetPhraseCollection(InputPath &inputPath) const
{
  const Phrase &sourcePhrase = inputPath.GetPhrase();
  size_t hash = hash_value(sourcePhrase);

  CacheColl &cache = GetCache();

  CacheColl::iterator iter;
  iter = cache.find(hash);

  if (iter != cache.end()) {
    // already in cache
    TargetPhraseCollection::shared_ptr tpColl = iter->second.first;
    inputPath.SetTargetPhrases(*this, tpColl, NULL);
  } else {
    // TRANSLITERATE
    const util::temp_file inFile;
    const util::temp_dir outDir;

    ofstream inStream(inFile.path().c_str());
    inStream << sourcePhrase.ToString() << endl;
    inStream.close();

    string cmd = m_scriptDir + "/Transliteration/prepare-transliteration-phrase-table.pl" +
                 " --transliteration-model-dir " + m_filePath +
                 " --moses-src-dir " + m_mosesDir +
                 " --external-bin-dir " + m_externalDir +
                 " --input-extension " + m_inputLang +
                 " --output-extension " + m_outputLang +
                 " --oov-file " + inFile.path() +
                 " --out-dir " + outDir.path();

    int ret = system(cmd.c_str());
    UTIL_THROW_IF2(ret != 0, "Transliteration script error");

    TargetPhraseCollection::shared_ptr tpColl(new TargetPhraseCollection);
    vector<TargetPhrase*> targetPhrases
    = CreateTargetPhrases(sourcePhrase, outDir.path());
    vector<TargetPhrase*>::const_iterator iter;
    for (iter = targetPhrases.begin(); iter != targetPhrases.end(); ++iter) {
      TargetPhrase *tp = *iter;
      tpColl->Add(tp);
    }
    cache[hash] = CacheCollEntry(tpColl, clock());
    inputPath.SetTargetPhrases(*this, tpColl, NULL);
  }
}

std::vector<TargetPhrase*> PhraseDictionaryTransliteration::CreateTargetPhrases(const Phrase &sourcePhrase, const string &outDir) const
{
  std::vector<TargetPhrase*> ret;

  string outPath = outDir + "/out.txt";
  ifstream outStream(outPath.c_str());

  string line;
  while (getline(outStream, line)) {
    vector<string> toks;
    Tokenize(toks, line, "\t");
    UTIL_THROW_IF2(toks.size() != 2, "Error in transliteration output file. Expecting word\tscore");

    TargetPhrase *tp = new TargetPhrase(this);
    Word &word = tp->AddWord();
    word.CreateFromString(Output, m_output, toks[0], false);

    float score = Scan<float>(toks[1]);
    tp->GetScoreBreakdown().PlusEquals(this, score);

    // score of all other ff when this rule is being loaded
    tp->EvaluateInIsolation(sourcePhrase, GetFeaturesToApply());

    ret.push_back(tp);
  }

  outStream.close();

  return ret;
}

ChartRuleLookupManager* PhraseDictionaryTransliteration::CreateRuleLookupManager(const ChartParser &parser,
    const ChartCellCollectionBase &cellCollection,
    std::size_t /*maxChartSpan*/)
{
  return NULL;
  //return new ChartRuleLookupManagerSkeleton(parser, cellCollection, *this);
}

void
PhraseDictionaryTransliteration::
SetParameter(const std::string& key, const std::string& value)
{
  if (key == "moses-dir") {
    m_mosesDir = value;
  } else if (key == "script-dir") {
    m_scriptDir = value;
  } else if (key == "external-dir") {
    m_externalDir = value;
  } else if (key == "input-lang") {
    m_inputLang = value;
  } else if (key == "output-lang") {
    m_outputLang = value;
  } else {
    PhraseDictionary::SetParameter(key, value);
  }
}

TO_STRING_BODY(PhraseDictionaryTransliteration);

// friend
ostream& operator<<(ostream& out, const PhraseDictionaryTransliteration& phraseDict)
{
  return out;
}

}