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

WordClasses.h « GIZA++-v2 - github.com/moses-smt/giza-pp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 369390664cf2b0fac35e6556ec8df8cbccb01ddb (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
/*

Copyright (C) 2000,2001  Franz Josef Och (RWTH Aachen - Lehrstuhl fuer Informatik VI)

This file is part of GIZA++ ( extension of GIZA ).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
USA.

*/
#ifndef WordClasses_h_DEFINED
#define WordClasses_h_DEFINED
#include <map>
#include <string>
#include <set>

class WordClasses
{
 private:
  map<string,string> Sw2c;
  map<string,int> Sc2int;
  Vector<string> Sint2c;
  Vector<int> w2c;
  unsigned int classes;
 public:
  WordClasses() 
    : classes(1) 
    {
      Sint2c.push_back("0");
      Sc2int["0"]=0;
    }
  template<class MAPPER> bool read(istream&in,const MAPPER&m)
    {
      string sline;
      int maxword=0;
      while(getline(in,sline))
	{
	  string word,wclass;
	  //istringstream iline(sline.c_str());
	  istringstream iline(sline);
	  iline>>word>>wclass;
	  maxword=max(m(word),maxword);
	  assert(Sw2c.count(word)==0);
	  Sw2c[word]=wclass;
	  if( !Sc2int.count(wclass) )
	    {
	      Sc2int[wclass]=classes++;
	      Sint2c.push_back(wclass);
	      assert(classes==Sint2c.size());
	    }
	}
      w2c=Vector<int>(maxword+1,0);
      for(map<string,string>::const_iterator i=Sw2c.begin();i!=Sw2c.end();++i)
	w2c[m(i->first)]=Sc2int[i->second];
      cout << "Read classes: #words: " << maxword << " " << " #classes: "<< classes <<endl;
      return 1;
    }
  int getClass(int w)const
    {
      if(w>=0&&int(w)<int(w2c.size()) )
	return w2c[w];
      else
	return 0;
    }
  int operator()(const string&x)const
    {
      if( Sc2int.count(x) )
	return Sc2int.find(x)->second;
      else
	{
	  cerr << "WARNING:  class " << x << " not found.\n";
	  return 0;
	}
    }
  string classString(unsigned int cnr)const
    {
      if( cnr<Sint2c.size())
	return Sint2c[cnr];
      else
	return string("0");
    }
};

#endif