Moving more modules
[apertium.git] / trunk / apertium-tools / apertium-utils / transliterate / transliterate.cpp
blob47c81034df21dd9949e1b4bbce639045d5d43305
1 /*
2 * Copyright (C) 2007 Francis Tyers
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <cwctype>
21 #include <transliterate.h>
22 #include <XMLParseUtil.h>
24 #include "entry.h"
26 using namespace std;
28 wstring const Transliterator::TRANSLITERATOR_TRANSLITERATOR_ELEM = L"transliterator";
29 wstring const Transliterator::TRANSLITERATOR_TABLE_ELEM = L"table";
30 wstring const Transliterator::TRANSLITERATOR_TABLE_NAME_ATTR = L"name";
31 wstring const Transliterator::TRANSLITERATOR_TRANSFORM_ELEM = L"transform";
33 wstring const Transliterator::TRANSLITERATOR_LEFT_ATTR = L"left";
34 wstring const Transliterator::TRANSLITERATOR_RIGHT_ATTR = L"right";
36 Transliterator::Transliterator(string const &file, string const &_direction, wstring const &_name)
38 init(file, _direction, _name);
41 Transliterator::Transliterator(string const &file, string const &_direction)
43 wstring const _name = L"default";
44 init(file, _direction, _name);
47 Transliterator::Transliterator(string const &file)
49 wstring const _name = L"default";
50 string const _direction = "lr";
51 init(file, _direction, _name);
54 void Transliterator::init(string const &file, string const &_direction, wstring const &_name)
56 reader = xmlReaderForFile(file.c_str(), NULL, 0);
58 direction = _direction;
59 name = _name;
60 iter = table.begin();
62 int ret = xmlTextReaderRead(reader);
64 while(ret == 1) {
65 process_node();
66 ret = xmlTextReaderRead(reader);
69 if(ret != 0) {
70 wcerr << L"Error: Parse error at the end of input." << endl;
73 xmlFreeTextReader(reader);
74 xmlCleanupParser();
77 Transliterator::~Transliterator()
79 // wcout << L"--" << endl << "~Transliterator()" << endl;
83 * Transliterate a string.
85 wstring
86 Transliterator::transliterate(wstring in)
88 wstring out;
89 wstring r = L"";
91 // iterate through the string 1 char at a time.
92 for(unsigned int i = 0; i < in.length(); i++) {
94 r = in[i];
96 // iterate through the transliteration table
97 for(unsigned int j = 0; j < table.size(); j++) {
98 Entry e = table.at(j);
100 if(direction == "rl") {
101 if(in[i] == (unsigned int)(e.right())[0]) {
102 r = e.left();
104 } else {
105 if(in[i] == (unsigned int)(e.left())[0]) {
106 r = e.right();
111 out += r;
114 return out;
117 void
118 Transliterator::process_node(void)
120 wstring nombre = XMLParseUtil::towstring(xmlTextReaderConstName(reader));
121 // this needs to be static as it must remain through all calls.
122 wstring static table_name = L"";
124 if(nombre == L"#text") {
125 // ignore this
126 } else if(nombre == TRANSLITERATOR_TRANSLITERATOR_ELEM) {
127 // wcout << L"Transliterator" << endl;
128 } else if(nombre == TRANSLITERATOR_TABLE_ELEM) {
129 table_name = XMLParseUtil::attrib(reader, TRANSLITERATOR_TABLE_NAME_ATTR);
130 // wcout << L" Table " << table_name << endl;
131 } else if(nombre == TRANSLITERATOR_TRANSFORM_ELEM) {
133 if(table_name == name) {
134 wstring const &left = XMLParseUtil::attrib(reader, TRANSLITERATOR_LEFT_ATTR);
135 wstring const &right = XMLParseUtil::attrib(reader, TRANSLITERATOR_RIGHT_ATTR);
136 // wcout << L" Replace " << left << L" --> " << right << endl;
138 Entry e = Entry(left, right);
139 iter = table.insert(iter, e);
141 } else if(nombre == L"#comment") {
142 // ignore this
143 } else {
144 wcerr << "Error (" << xmlTextReaderGetParserLineNumber(reader);
145 wcerr << "): Invalid node '<" << nombre << ">'." << endl;