| 1 | /* ***** BEGIN LICENSE BLOCK ***** |
| 2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 3 | * |
| 4 | * Copyright (C) 2002-2017 Németh László |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * Hunspell is based on MySpell which is Copyright (C) 2002 Kevin Hendricks. |
| 17 | * |
| 18 | * Contributor(s): David Einstein, Davide Prina, Giuseppe Modugno, |
| 19 | * Gianluca Turconi, Simon Brouwer, Noll János, Bíró Árpád, |
| 20 | * Goldman Eleonóra, Sarlós Tamás, Bencsáth Boldizsár, Halácsy Péter, |
| 21 | * Dvornik László, Gefferth András, Nagy Viktor, Varga Dániel, Chris Halls, |
| 22 | * Rene Engelhard, Bram Moolenaar, Dafydd Jones, Harri Pitkänen |
| 23 | * |
| 24 | * Alternatively, the contents of this file may be used under the terms of |
| 25 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 27 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 28 | * of those above. If you wish to allow use of your version of this file only |
| 29 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 30 | * use your version of this file under the terms of the MPL, indicate your |
| 31 | * decision by deleting the provisions above and replace them with the notice |
| 32 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 33 | * the provisions above, a recipient may use your version of this file under |
| 34 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 35 | * |
| 36 | * ***** END LICENSE BLOCK ***** */ |
| 37 | |
| 38 | #include <cstring> |
| 39 | #include <cstdlib> |
| 40 | #include <cstdio> |
| 41 | |
| 42 | #include "../hunspell/hunspell.hxx" |
| 43 | #include "../parsers/textparser.hxx" |
| 44 | |
| 45 | #ifndef W32 |
| 46 | using namespace std; |
| 47 | #endif |
| 48 | |
| 49 | int main(int, char** argv) { |
| 50 | FILE* f; |
| 51 | |
| 52 | /* first parse the command line options */ |
| 53 | |
| 54 | for (int i = 1; i < 6; i++) |
| 55 | if (!argv[i]) { |
| 56 | fprintf( |
| 57 | stderr, |
| 58 | "chmorph - change affixes by morphological analysis and generation\n" |
| 59 | "correct syntax is:\nchmorph affix_file " |
| 60 | "dictionary_file file_to_convert STRING1 STRING2\n" |
| 61 | "STRINGS may be arbitrary parts of the morphological descriptions\n" |
| 62 | "example: chmorph hu.aff hu.dic hu.txt SG_2 SG_3 " |
| 63 | " (convert informal Hungarian second person texts to formal third " |
| 64 | "person texts)\n"); |
| 65 | exit(1); |
| 66 | } |
| 67 | |
| 68 | /* open the words to check list */ |
| 69 | |
| 70 | f = fopen(argv[3], "r"); |
| 71 | if (!f) { |
| 72 | fprintf(stderr, "Error - could not open file to check\n"); |
| 73 | exit(1); |
| 74 | } |
| 75 | |
| 76 | Hunspell* pMS = new Hunspell(argv[1], argv[2]); |
| 77 | TextParser* p = new TextParser( |
| 78 | "qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM"); |
| 79 | |
| 80 | char buf[MAXLNLEN]; |
| 81 | |
| 82 | while (fgets(buf, MAXLNLEN, f)) { |
| 83 | p->put_line(buf); |
| 84 | std::string next; |
| 85 | while (p->next_token(next)) { |
| 86 | std::vector<std::string> pl = pMS->analyze(next); |
| 87 | if (!pl.empty()) { |
| 88 | int gen = 0; |
| 89 | for (size_t i = 0; i < pl.size(); ++i) { |
| 90 | const char* pos = strstr(pl[i].c_str(), argv[4]); |
| 91 | if (pos) { |
| 92 | std::string r(pl[i], pos - pl[i].c_str()); |
| 93 | r.append(argv[5]); |
| 94 | r.append(pos + strlen(argv[4])); |
| 95 | pl[i] = r; |
| 96 | gen = 1; |
| 97 | } |
| 98 | } |
| 99 | if (gen) { |
| 100 | std::vector<std::string> pl2 = pMS->generate(next, pl); |
| 101 | if (!pl2.empty()) { |
| 102 | p->change_token(pl2[0].c_str()); |
| 103 | // jump over the (possibly un)modified word |
| 104 | (void)p->next_token(next); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | fprintf(stdout, "%s\n", p->get_line().c_str()); |
| 110 | } |
| 111 | |
| 112 | delete p; |
| 113 | fclose(f); |
| 114 | return 0; |
| 115 | } |