summaryrefslogtreecommitdiff
path: root/decoder/sentences.h
blob: 842072b9301b17827a8cf44ff82db97e38a09ac1 (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
#ifndef CDEC_SENTENCES_H
#define CDEC_SENTENCES_H

#include <algorithm>
#include <vector>
#include <iostream>
#include "filelib.h"
#include "tdict.h"
#include "stringlib.h"
typedef std::vector<WordID> Sentence;

inline void StringToSentence(std::string const& str,Sentence &s) {
  using namespace std;
  vector<string> ss=SplitOnWhitespace(str);
  s.clear();
  transform(ss.begin(),ss.end(),back_inserter(s),ToTD());
}

inline Sentence StringToSentence(std::string const& str) {
  Sentence s;
  StringToSentence(str,s);
  return s;
}

inline std::istream& operator >> (std::istream &in,Sentence &s) {
  using namespace std;
  string str;
  if (getline(in,str)) {
    StringToSentence(str,s);
  }
  return in;
}


class Sentences : public std::vector<Sentence> {
  typedef std::vector<Sentence> VS;
public:
  Sentences() {  }
  Sentences(unsigned n,Sentence const& sentence) : VS(n,sentence) {  }
  Sentences(unsigned n,std::string const& sentence) : VS(n,StringToSentence(sentence)) {  }
  void Load(std::string file) {
    ReadFile r(file);
    Load(*r.stream());
  }
  void Load(std::istream &in) {
    this->push_back(Sentence());
    while(in>>this->back()) ;
    this->pop_back();
  }
};


#endif