File: main.cpp

package info (click to toggle)
zpspell 0.4.3-4.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 120 kB
  • sloc: cpp: 290; xml: 106; makefile: 18
file content (157 lines) | stat: -rw-r--r-- 3,687 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
  Copyright (c) 2004, TUBITAK/UEKAE

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  Please read the COPYING file.
*/

/*
  ISpell-like, command line ZemberekServer client.
*/

#include <iostream>
#include <getopt.h>
#include <cstring>
#include <cstdio>

#include "zstring.h"
#include "zsconn.h"
#include "config.h"

using namespace std;

static const string desc = "@(#) Zemberek Turkish spell checker ";


static int checkAndPrint( ZSConn& zemberek, const string str, int offset )
{
    ZString zstr = zemberek.checkString( str, offset );
    switch ( zstr.status() ) {
    case Z_TRUE:
        cout << "*" << endl;
        break;
    case Z_FALSE:
        cout << "# " << zstr.str() << " " << zstr.offset() << endl;
        break;
    case Z_SUGGESTION:
        cout << "& " <<
            zstr.str() << " " <<
            zstr.suggestionCount() << " " <<
            zstr.offset() << ": " <<
            zstr.suggestionString() << endl;
        break;
    default:
        return -1;
        break;
    }

    return 0;
}

/* Ispell style interactive mode */
static int z_interactive_mode( ZSConn& zemberek )
{
    cout << desc << VERSION_STRING << endl;

    while ( true ) {
        char buf[BUFSIZ];
        char *t;

        cin.getline( buf, BUFSIZ );
        if ( strlen(buf) == 0 ) break;

        t = buf;
        int offset = 0, count = 0, ret = 0;
        bool inWord = true;

        // FIXME!
        // bu işler aslen zsconn'da yapılıyor.
        // belki buraya taşımak gereklidir?
        if ( *t == '^' && count == 0 ) {
            inWord == false;
            ++t; ++count, ++offset;
        }

        string str( "" );
        while ( *t ) {
            if ( *t == ' ' || *t == '\t' || *t == ':' ) {
                if ( !(str.empty()) ) {
                    ret = checkAndPrint( zemberek, str, offset );
                }

                inWord = false;
                str.erase();
                goto CONTINUE_LOOP;
            }

            if ( !inWord ) {
                offset = count;
                inWord = true;
            }

            str += *t;
        CONTINUE_LOOP:
            ++count;
            ++t;
        }

        // process the last word (if any)
        if ( !(str.empty()) ) {
            ret = checkAndPrint( zemberek, str, offset );
        }

        if ( ret == 0 ) cout << endl;
    }

    return 0;
}


int main( int argc, char** argv )
{
    bool aflag = false;
    int o;
    while ( ( o = getopt( argc, argv, "aBm" )) != -1 ) {
        switch ( o ) {
        case 'a':
            aflag = true;
            break;
        default:
            break;
            //            cerr << "Bilinmeyen parametre: " << optopt << endl;
        }
    }

    ZSConn zemberek;

    // start interactive mode and finalize.
    if ( aflag ) {
        return z_interactive_mode( zemberek );
    }

    // non-interactive mode (singlerun)
    for ( int i = 1; i < argc; ++i ) {
        ZString zstr = zemberek.checkString( argv[i], 0 );
        switch ( zstr.status() ) {
        case Z_TRUE:
            cout << zstr.str() << ": doğru" << endl;
            break;
        case Z_FALSE:
            cout << zstr.str() << ": yanlış (öneri yok)" << endl;
            break;
        case Z_SUGGESTION:
            cout << zstr.str() << ": yanlış (" <<
                zstr.suggestionString() << ")" << endl;
            break;
        default:
            return -1;
            break;
        }
    }

    return 0;
}