~ubuntu-branches/debian/sid/dico/sid

« back to all changes in this revision

Viewing changes to dicod/lev.c

  • Committer: Bazaar Package Importer
  • Author(s): أحمد المحمودي (Ahmed El-Mahmoudy)
  • Date: 2009-04-03 06:28:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090403062825-n4tbn09hv9ve5s2s
Tags: upstream-2.0
Import upstream version 2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of GNU Dico.
 
2
   Copyright (C) 2008 Sergey Poznyakoff
 
3
 
 
4
   GNU Dico 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 3, or (at your option)
 
7
   any later version.
 
8
 
 
9
   GNU Dico 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.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */
 
16
 
 
17
#include <dicod.h>
 
18
 
 
19
static int levenshtein_distance = 1;
 
20
 
 
21
static int
 
22
lev_sel(int cmd, const char *word, const char *dict_word, void *closure)
 
23
{
 
24
    if (cmd == DICO_SELECT_RUN) {
 
25
        int dist = dico_levenshtein_distance(word, dict_word, (int)closure);
 
26
        if (dist < 0)
 
27
            return 0;
 
28
        return dist <= levenshtein_distance;
 
29
    }
 
30
    return 0;
 
31
}
 
32
 
 
33
static struct dico_strategy levstrat[] = {
 
34
    { "lev",
 
35
      "Match headwords within given Levenshtein distance",
 
36
      lev_sel,
 
37
      NULL },
 
38
    { "nlev",
 
39
      "Match headwords within given Levenshtein distance (normalized)",
 
40
      lev_sel,
 
41
      (void*)DICO_LEV_NORM },
 
42
    { "dlev",
 
43
      "Match headwords within given Damerau-Levenshtein distance",
 
44
      lev_sel,
 
45
      (void*)DICO_LEV_DAMERAU },
 
46
    { "ndlev",
 
47
      "Match headwords within given Damerau-Levenshtein distance (normalized)",
 
48
      lev_sel,
 
49
      (void*)(DICO_LEV_NORM|DICO_LEV_DAMERAU) }
 
50
};
 
51
 
 
52
static void
 
53
dicod_xlevdist(dico_stream_t str, int argc, char **argv)
 
54
{
 
55
    if (c_strcasecmp(argv[1], "tell") == 0) 
 
56
        stream_printf(str, "280 %d\r\n", levenshtein_distance);
 
57
    else if (isdigit(argv[1][0]) && argv[1][0] != '0' && argv[1][1] == 0) {
 
58
        levenshtein_distance = atoi(argv[1]);
 
59
        stream_printf(str, "250 ok - Levenshtein threshold set to %d\r\n",
 
60
                      levenshtein_distance);
 
61
    } else
 
62
        stream_writez(str, "500 invalid argument\r\n");
 
63
}
 
64
        
 
65
void
 
66
register_lev()
 
67
{
 
68
    int i;
 
69
    static struct dicod_command cmd[] = {
 
70
        { "XLEV", 2, 2, "distance", "Set Levenshtein distance",
 
71
          dicod_xlevdist },
 
72
        { NULL }
 
73
    };
 
74
    for (i = 0; i < DICO_ARRAY_SIZE(levstrat); i++)
 
75
        dico_strategy_add(&levstrat[i]);
 
76
    dico_set_default_strategy("nlev");
 
77
    dicod_capa_register("xlev", cmd, NULL, NULL);
 
78
}
 
79
 
 
80