Changed the gerund rule to something that works for cases in Afrikaans.
[apertium.git] / apertium-tagger-training-tools-unicode / src / Utils.C
blob38c6d4c094e1897c80d3f639e8c43371426cc7c2
1 /*
2  * Copyright (C) 2004-2006 Felipe Sánchez-Martínez
3  * Copyright (C) 2006 Universitat d'Alacant
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 /**
21  * Utility functions. (source file)
22  *
23  *  @author   Felipe Sánchez-Martínez - fsanchez@dlsi.ua.es
24  */
26 #include <iostream>
27 #include <fstream>
29 #include <cstdio>
31 #include <sys/types.h>
32 #include <sys/wait.h>
34 #include <sys/times.h>
35 #include <sys/resource.h>
37 #include <apertium/utf_converter.h>
39 #include "Utils.H"
41 string Utils::translation_script;
42 string Utils::likelihood_script;
43 string Utils::translation_script2;
44 string Utils::likelihood_script2;
45 bool Utils::debug;
47 wstring 
48 Utils::translate(string script, const wstring& s) {
49   int fd_in[2]; //Pipe descriptor (the father writes text to the child)
50   int fd_out[2];//Pipe descriptor (the child writes the result to the father)
52   if (pipe(fd_in)!=0) 
53     cerr<<"Error creating input pipe\n";
54   if (pipe(fd_out)!=0) 
55     cerr<<"Error creating output pipe\n";
56   
57   if (fork()==0) { //Code for the child
58     close(0);
59     dup(fd_in[0]);
60     close(1);
61     dup(fd_out[1]);
62      
63     //Those descriptor that will not be use are closed
64     close(fd_in[1]); 
65     close(fd_out[0]);
66      
67     execlp(script.c_str(), script.c_str(), NULL);
69     //If execution arrives at this point, we are in trouble
70     perror("Error");
71     close(fd_in[0]);
72     close(fd_out[1]);
73     exit(EXIT_FAILURE);
74   } else { //Code for the father
75     //Those descriptor that will not be used are closed
76     close(fd_in[0]);
77     close(fd_out[1]);
79     //char buf[256];
80     int status;
82     //Mandamos el texto 
83     //write(fd_in[1], (void*)s.c_str(), s.length());
84     //close(fd_in[1]);
85     FILE *fw=fdopen(fd_in[1], "w");
86     for (int i=0; i<s.length(); i++)
87       fputwc(s[i],fw);
88     fclose(fw);
89     
91     wait (&status); //Waiting for the child to finish
92     if(status!=0) {
93       cerr<<"Error: Child process could not execute the translation script.\n";
94       cerr<<"Input string: ["<<UtfConverter::toUtf8(s)<<"]\n";
95       close(fd_out[0]);
96       exit(EXIT_FAILURE);
97     }
99     //Now the father reads the result from the child
100     //string ret="";
101     //int nread;
102     //do {
103     //  nread=read(fd_out[0], buf, 255);
104     //  buf[nread]='\0';
105     //  ret+=buf;
106     //} while (nread==255);        
107     //close(fd_out[0]);
108     wstring ret=L"";
109     FILE *fr=fdopen(fd_out[0], "r");
110     wint_t c;
111     do {
112       c=fgetwc(fr);
113       if (c!= WEOF)
114         ret+=c;
115     } while (c != WEOF);
116     fclose(fr);
118     return ret;
119   }     
122 double 
123 Utils::likelihood(string script, const wstring& s) {
124   int fd_in[2]; //Pipe descriptor (the father writes text to the child)
125   int fd_out[2];//Pipe descriptor (the child writes the result to the father)
127   string str=UtfConverter::toUtf8(s);
129   if (pipe(fd_in)!=0) 
130     cerr<<"Error creating input pipe\n";
131   if (pipe(fd_out)!=0) 
132     cerr<<"Error creating output pipe\n";
133   
134   if (fork()==0) { //Code for the child
135     close(0);
136     dup(fd_in[0]);
137     close(1);
138     dup(fd_out[1]);
139      
140     //Those descriptor that will not be use are closed
141     close(fd_in[1]); 
142     close(fd_out[0]);
143      
144     execlp(script.c_str(), script.c_str(), NULL);
146     //If execution arrives at this point, we are in trouble
147     perror("Error");
148     close(fd_in[0]);
149     close(fd_out[1]);
150     exit(EXIT_FAILURE);
151   } else { //Code for the father
152     char buf[256];
153     int status;
155     //Those descriptor that will not be use are closed
156     close(fd_in[0]);
157     close(fd_out[1]);
158      
159     //Mandamos el texto 
160     write(fd_in[1], (void*)str.c_str(), str.length());
161     close(fd_in[1]);
162     wait (&status); //Waiting for the child to finish
163     if(status!=0) {
164       cerr<<"Error: Child process could not execute the likelihood script.\n";
165       cerr<<"Input string: ["<<str<<"]\n";
166       close(fd_out[0]);
167       exit(EXIT_FAILURE);
168     }
170     //Now the father reads the result from the child
171     string ret="";
172     int nread;
173     do {
174       nread=read(fd_out[0], buf, 255);
175       buf[nread]='\0';
176       ret+=buf;
177     } while (nread==255);
178         
179     close(fd_out[0]);
180     //cerr<<"CAD:"<<ret.c_str()<<endl;
181     return atof(ret.c_str());
182   }     
185 int 
186 Utils::get_usage_time()
188   int t;
189   struct rusage usage;
190   getrusage(RUSAGE_SELF,&usage);
191   t=usage.ru_utime.tv_sec+usage.ru_stime.tv_sec;
193   getrusage(RUSAGE_CHILDREN,&usage);
194   t+=usage.ru_utime.tv_sec+usage.ru_stime.tv_sec;
196   return t;
199 vector<wstring>
200 Utils::split_wstring(const wstring& input, const wstring& delimiter) {  
201   int pos;
202   int new_pos;
203   vector<wstring> result;
204   wstring s=L"";
205   pos=0;
207   while (pos<input.size()) {
208     new_pos=input.find(delimiter, pos);
209     if(new_pos<0)
210       new_pos=input.size();
211     s=input.substr(pos, new_pos-pos);
212     result.push_back(s);
213     pos=new_pos+1;
214   }
215   return result;
218 vector<string>
219 Utils::split_string(const string& input, const string& delimiter) {  
220   int pos;
221   int new_pos;
222   vector<string> result;
223   string s="";
224   pos=0;
226   while (pos<input.size()) {
227     new_pos=input.find(delimiter, pos);
228     if(new_pos<0)
229       new_pos=input.size();
230     s=input.substr(pos, new_pos-pos);
231     result.push_back(s);
232     pos=new_pos+1;
233   }
234   return result;
237 void
238 Utils::print_debug(const string& s) {
239   if (debug)
240     cerr<<s;
243 void
244 Utils::print_debug(const wstring& s) {
245   if (debug)
246     cerr<<UtfConverter::toUtf8(s);
249 void 
250 Utils::print_debug(const double& d){
251   if (debug)
252     cerr<<d;
254 void 
255 Utils::print_debug(const int& i) {
256   if (debug)
257     cerr<<i;
260 // wstring
261 // Utils::string2wstring(const string& s) {
262 //   //WARNING
263 //   wchar_t auxws[s.length()+1];
264 //   mbstowcs(auxws, optarg, strlen(optarg)+1);
265 //   auxws[strlen(optarg)]=L'\0';
267 //   return auxws;
268 // }