~ubuntu-branches/ubuntu/oneiric/dasher/oneiric

« back to all changes in this revision

Viewing changes to Src/DasherCore/TrainingHelper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2010-04-23 07:56:20 UTC
  • mfrom: (1.2.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100423075620-qpr1ousiruyxs1fb
Tags: 4.11-1
* New upstream release:
  + debian/control.in:
    - Update build dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
// along with Dasher; if not, write to the Free Software 
19
19
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
20
 
21
 
#include "AlphabetManagerFactory.h"
22
21
#include "TrainingHelper.h"
23
22
 
24
23
#include <cstdio>
28
27
#include <ios>
29
28
#include <iostream>
30
29
#include <vector>
 
30
#include <sstream>
31
31
 
32
32
//using namespace Dasher;
33
33
 
35
35
static void XML_EndElement(void *pUserData, const XML_Char *szName);
36
36
static void XML_CharacterData(void *pUserData, const XML_Char *szS, int iLen);
37
37
 
 
38
Dasher::CTrainingHelper::CTrainingHelper(const Dasher::CAlphabet *pAlphabet) : m_pAlphabet(pAlphabet) {
 
39
}
 
40
 
38
41
void 
39
 
Dasher::CTrainingHelper::LoadFile(const std::string &strFileName, 
40
 
                                  Dasher::CTrainer *pTrainer, 
41
 
                                  const Dasher::CAlphabet *pAlphabet) {
 
42
Dasher::CTrainingHelper::LoadFile(const std::string &strFileName) {
42
43
   if(strFileName == "")
43
44
    return;
44
45
 
54
55
   fclose(pInputFile);
55
56
 
56
57
   if(!strcmp(szTestBuffer, "<?xml")) {
57
 
     LoadXML(strFileName, pTrainer, pAlphabet);
 
58
         LoadXML(strFileName);
58
59
   }
59
60
   else {
60
 
     LoadPlain(strFileName, pTrainer, pAlphabet);
 
61
        LoadPlain(strFileName);
61
62
   }
62
63
}
63
64
 
64
65
void
65
 
Dasher::CTrainingHelper::LoadPlain(const std::string &strFileName, 
66
 
                           Dasher::CTrainer *pTrainer, 
67
 
                           const Dasher::CAlphabet *pAlphabet) {
 
66
Dasher::CTrainingHelper::LoadPlain(const std::string &strFileName) {
68
67
  
69
68
  std::ifstream in(strFileName.c_str(), std::ios::binary);
70
69
  if (in.fail())
74
73
      return;
75
74
    }
76
75
 
77
 
  std::vector<Dasher::symbol> vSymbols;
78
 
  vSymbols.clear();
79
 
  pAlphabet->GetSymbols(vSymbols, in);
80
 
  pTrainer->Train(vSymbols);
 
76
  CAlphabet::SymbolStream syms(m_pAlphabet, in);
 
77
  Train(syms);
81
78
 
82
79
  in.close();
83
80
}
84
81
 
85
82
void 
86
 
Dasher::CTrainingHelper::LoadXML(const std::string &strFileName, 
87
 
                         Dasher::CTrainer *pTrainer, 
88
 
                         const Dasher::CAlphabet *pAlphabet) {
 
83
Dasher::CTrainingHelper::LoadXML(const std::string &strFileName) {
89
84
 
90
85
  m_bInSegment = false;
91
 
  m_pAlphabet = pAlphabet;
92
 
  m_pTrainer = pTrainer;
93
86
 
94
87
  FILE *pInput;
95
88
  if((pInput = fopen(strFileName.c_str(), "r")) == (FILE *) 0) {
134
127
void 
135
128
Dasher::CTrainingHelper::HandleEndElement(const XML_Char *szName) {
136
129
  if(!strcmp(szName, "segment")) {
137
 
    m_pTrainer->Reset();
138
 
 
139
 
    std::vector<Dasher::symbol> vSymbols;
140
 
    m_pAlphabet->GetSymbols(&vSymbols, &m_strCurrentText, false);
141
 
    m_pTrainer->Train(vSymbols);
 
130
    std::istringstream in(m_strCurrentText);
 
131
    CAlphabet::SymbolStream syms(m_pAlphabet,in);
 
132
    Train(syms);
142
133
    
143
134
    m_bInSegment = false;
144
135
  }