summaryrefslogtreecommitdiff
path: root/decoder/sentences.h
blob: 622a6f43ddfc85cd21a492797090351333241852 (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
#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 std::ostream & operator<<(std::ostream &out,Sentence const& s) {
  return out<<TD::GetString(s);
}

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)) {  }
  std::string filename;
  void Load(std::string file) {
    ReadFile r(file);
    Load(r.get(),file);
  }
  void Load(std::istream &in,std::string filen="-") {
    filename=filen;
    do {
      this->push_back(Sentence());
    } while(in>>this->back());
    this->pop_back();
  }
  void Print(std::ostream &out,int headn=0) const {
    out << "[" << size()<< " sentences from "<<filename<<"]";
    if (headn!=0) {
      int i=0,e=this->size();
      if (headn>0&&headn<e) {
        e=headn;
        out << " (first "<<headn<<")";
      }
      out << " :\n";
      for (;i<e;++i)
        out<<(*this)[i] << "\n";
    }
  }
  friend inline std::ostream& operator<<(std::ostream &out,Sentences const& s) {
    s.Print(out);
    return out;
  }
};


#endif