File: zemberek.cpp

package info (click to toggle)
mozzemberek 0.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 168 kB
  • ctags: 111
  • sloc: cpp: 686; makefile: 111
file content (83 lines) | stat: -rw-r--r-- 1,917 bytes parent folder | download
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
/*
  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.

  Mozilla Zemberek Client library is based on zpspell. The Initial Developer
  of the zpspell code is Baris Metin.
*/

#include <iostream>
#include <vector>

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#include "zsconn.h"
#include "zstring.h"
#include "zemberek.h"

Zemberek::Zemberek() : maxSug(25)
{
    zsconn = new ZSConn();
}

Zemberek::Zemberek(char *host, int port) : maxSug(25)
{
    zsconn = new ZSConn(host, port);
}

Zemberek::~Zemberek()
{
    delete zsconn;
}

int Zemberek::spellCheck(const char *word) const
{
    ZString zstr = zsconn->checkString(word, 0);
    return (zstr.status() == Z_TRUE);
}

int Zemberek::getSuggestions(char ***suggestions, const char *word) const
{
    int ns = 0;
    //XXX: This check is unnecessary. we call this method only for misspelled words.
    ZString zstr = zsconn->checkString(word, 0);

    if ( zstr.status() == Z_FALSE ) {
        zstr.setSuggestions( zsconn->getSuggestions( zstr.str() ) );
        if ( zstr.suggestionCount() != 0 ) {
            zstr.setStatus( Z_SUGGESTION );
        }
    }

    *suggestions = NULL;

    if (zstr.status() != Z_SUGGESTION)
	return 0;

    char **wlst = (char **)calloc(maxSug, sizeof(char *));
    if (wlst == NULL) 
      return 0;
    
    vector<string>::const_iterator words_iter;
    const vector<string> &words = zstr.suggestions();

    for (words_iter = words.begin(); words_iter != words.end() && ns < maxSug; words_iter++, ns++) {
	wlst[ns] = strdup((*words_iter).c_str());
    }

    if (ns > 0)
	*suggestions = wlst;

    return ns;
}

char *Zemberek::get_dic_encoding()
{
    return strdup("UTF-8");
}