📄 phonet.cpp
字号:
/* phonetic.c - generic replacement aglogithms for phonetic transformation Copyright (C) 2000 Bj鰎n Jacke This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation; This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Bj鰎n Jacke may be reached by email at bjoern.jacke@gmx.de Changelog: 2000-01-05 Bj鰎n Jacke <bjoern.jacke@gmx.de> Initial Release insprired by the article about phonetic transformations out of c't 25/1999*/#include <string.h>#include <assert.h>#include <vector>#include "asc_ctype.hpp"#include "string.hpp"#include "phonet.hpp"#include "errors.hpp"#include "fstream.hpp"#include "getdata.hpp"using namespace acommon;namespace aspeller { const char * const PhonetParms::rules_end = ""; static bool to_bool(const String & str) { if (str == "1" || str == "true") return true; else return false; }#if 0 void dump_phonet_rules(ostream & out, const PhonetParms & parms) { out << "version " << parms.version << "\n"; out << "followup " << parms.followup << "\n"; out << "collapse_result " << parms.collapse_result << "\n"; out << "\n"; ios::fmtflags flags = out.setf(ios::left); for (int i = 0; parms.rules[i] != PhonetParms::rules_end; i += 2) { out << setw(20) << parms.rules[i] << " " << (parms.rules[i+1][0] == '\0' ? "_" : parms.rules[i+1]) << "\n"; } out.flags(flags); }#endif struct PhonetParmsImpl : public PhonetParms { std::vector<const char *> rdata; std::vector<char> data; void assign(const PhonetParms * other) { abort(); *this = *(const PhonetParmsImpl *)other; this->rules = &rdata.front(); } PhonetParmsImpl * clone() const { PhonetParmsImpl * other = new PhonetParmsImpl(*this); return other; } PhonetParmsImpl() {} PhonetParmsImpl(const PhonetParmsImpl & other) : PhonetParms(other), rdata(other.rdata.size()), data(other.data) { fix_pointers(other); } void fix_pointers(const PhonetParmsImpl & other) { if (other.rdata.empty()) return; rules = &rdata.front(); int i = 0; for (;other.rules[i] != rules_end; ++i) { rules[i] = &data.front() + (&other.data.front() - other.rules[i]); } rules[i] = rules_end; rules[i+1] = rules_end; } }; PosibErr<PhonetParms *> load_phonet_rules(const String & file) { FStream in; RET_ON_ERR(in.open(file, "r")); PhonetParmsImpl * parms = new PhonetParmsImpl(); parms->followup = true; parms->collapse_result = false; String key; String data; int size = 0; int num = 0; while (true) { if (!getdata_pair(in, key, data)) break; if (key != "followup" && key != "collapse_result" && key != "version") { ++num; size += key.size() + 1; if (data != "_") { size += data.size() + 1; } } } parms->data.reserve(size); char * d = &parms->data.front(); parms->rdata.reserve(2 * num + 2); std::vector<const char *>::iterator r = parms->rdata.begin(); in.restart(); while (true) { if (!getdata_pair(in, key, data)) break; if (key == "followup") { parms->followup = to_bool(data); } else if (key == "collapse_result") { parms->collapse_result = to_bool(data); } else if (key == "version") { parms->version = data; } else { strncpy(d, key.c_str(), key.size() + 1); *r = d; ++r; d += key.size() + 1; if (data == "_") { *r = ""; } else { strncpy(d, data.c_str(), data.size() + 1); *r = d; d += data.size() + 1; } ++r; } } if (d != &parms->data.front() + size) { delete parms; return make_err(file_error, file); } if (parms->version.empty()) { delete parms; return make_err(bad_file_format, file, "You must specify a version string"); } *(r ) = PhonetParms::rules_end; *(r+1) = PhonetParms::rules_end; parms->rules = &parms->rdata.front(); return parms; } // FIXME: Get this information from the language class void init_phonet_charinfo(PhonetParms & parms) { const unsigned char * vowels_low = (const unsigned char *) "噌忏溴骁栝觌祉铒瘃蝮趱鲽
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -