Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /***************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : /**
30 : : *
31 : : *
32 : : *
33 : : *
34 : : * TODO
35 : : * - Add exception throwing when h == NULL
36 : : * - Not init h when implicit constructor is launched
37 : : */
38 : :
39 : :
40 : : #include <string.h>
41 : : #include <sstream>
42 : : #include <iostream>
43 : :
44 : : #include <libexttextcat/textcat.h>
45 : : #include <libexttextcat/common.h>
46 : : #include <libexttextcat/constants.h>
47 : : #include <libexttextcat/fingerprint.h>
48 : : #include <libexttextcat/utf8misc.h>
49 : :
50 : : #include <sal/types.h>
51 : :
52 : : #include "altstrfunc.hxx"
53 : : #include "simpleguesser.hxx"
54 : :
55 : : #ifndef _UTF8_
56 : : #define _UTF8_
57 : : #endif
58 : :
59 : :
60 : : using namespace std;
61 : :
62 : :
63 : : /**
64 : : * This 3 following structures are from fingerprint.c and textcat.c
65 : : */
66 : :
67 : : typedef struct ngram_t {
68 : :
69 : : sint2 rank;
70 : : char str[MAXNGRAMSIZE+1];
71 : :
72 : : } ngram_t;
73 : :
74 : : typedef struct fp_t {
75 : :
76 : : const char *name;
77 : : ngram_t *fprint;
78 : : uint4 size;
79 : :
80 : : } fp_t;
81 : :
82 : : typedef struct textcat_t{
83 : :
84 : : void **fprint;
85 : : char *fprint_disable;
86 : : uint4 size;
87 : : uint4 maxsize;
88 : :
89 : : char output[MAXOUTPUTSIZE];
90 : :
91 : : } textcat_t;
92 : : /** end of the 3 structs */
93 : :
94 : 0 : SimpleGuesser::SimpleGuesser()
95 : : {
96 : 0 : h = NULL;
97 : 0 : }
98 : :
99 : 0 : void SimpleGuesser::operator=(SimpleGuesser& sg){
100 : 0 : if(h){textcat_Done(h);}
101 : 0 : h = sg.h;
102 : 0 : }
103 : :
104 : 0 : SimpleGuesser::~SimpleGuesser()
105 : : {
106 : 0 : if(h){textcat_Done(h);}
107 : 0 : }
108 : :
109 : :
110 : : /*!
111 : : \fn SimpleGuesser::GuessLanguage(char* text)
112 : : */
113 : 0 : vector<Guess> SimpleGuesser::GuessLanguage(const char* text)
114 : : {
115 : 0 : vector<Guess> guesses;
116 : :
117 : 0 : if (!h)
118 : 0 : return guesses;
119 : :
120 : 0 : int len = strlen(text);
121 : :
122 : 0 : if (len > MAX_STRING_LENGTH_TO_ANALYSE)
123 : 0 : len = MAX_STRING_LENGTH_TO_ANALYSE;
124 : :
125 : 0 : const char *guess_list = textcat_Classify(h, text, len);
126 : :
127 : 0 : if (strcmp(guess_list, _TEXTCAT_RESULT_SHORT) == 0)
128 : 0 : return guesses;
129 : :
130 : 0 : int current_pointer = 0;
131 : :
132 : 0 : for(int i = 0; guess_list[current_pointer] != '\0'; i++)
133 : : {
134 : 0 : while (guess_list[current_pointer] != GUESS_SEPARATOR_OPEN && guess_list[current_pointer] != '\0')
135 : 0 : current_pointer++;
136 : 0 : if(guess_list[current_pointer] != '\0')
137 : : {
138 : 0 : Guess g(guess_list + current_pointer);
139 : :
140 : 0 : guesses.push_back(g);
141 : :
142 : 0 : current_pointer++;
143 : : }
144 : : }
145 : :
146 : 0 : return guesses;
147 : : }
148 : :
149 : 0 : Guess SimpleGuesser::GuessPrimaryLanguage(const char* text)
150 : : {
151 : 0 : vector<Guess> ret = GuessLanguage(text);
152 : 0 : return ret.empty() ? Guess() : ret[0];
153 : : }
154 : : /**
155 : : * Is used to know wich language is available, unavailable or both
156 : : * when mask = 0xF0, return only Available
157 : : * when mask = 0x0F, return only Unavailable
158 : : * when mask = 0xFF, return both Available and Unavailable
159 : : */
160 : 0 : vector<Guess> SimpleGuesser::GetManagedLanguages(const char mask)
161 : : {
162 : 0 : textcat_t *tables = (textcat_t*)h;
163 : :
164 : 0 : vector<Guess> lang;
165 : 0 : if(!h){return lang;}
166 : :
167 : 0 : for (size_t i=0; i<tables->size; ++i)
168 : : {
169 : 0 : if (tables->fprint_disable[i] & mask)
170 : : {
171 : 0 : string langStr = "[";
172 : 0 : langStr += fp_Name(tables->fprint[i]);
173 : 0 : Guess g(langStr.c_str());
174 : 0 : lang.push_back(g);
175 : : }
176 : : }
177 : :
178 : 0 : return lang;
179 : : }
180 : :
181 : 0 : vector<Guess> SimpleGuesser::GetAvailableLanguages()
182 : : {
183 : 0 : return GetManagedLanguages( sal::static_int_cast< char >( 0xF0 ) );
184 : : }
185 : :
186 : 0 : vector<Guess> SimpleGuesser::GetUnavailableLanguages()
187 : : {
188 : 0 : return GetManagedLanguages( sal::static_int_cast< char >( 0x0F ));
189 : : }
190 : :
191 : 0 : vector<Guess> SimpleGuesser::GetAllManagedLanguages()
192 : : {
193 : 0 : return GetManagedLanguages( sal::static_int_cast< char >( 0xFF ));
194 : : }
195 : :
196 : 0 : void SimpleGuesser::XableLanguage(string lang, char mask)
197 : : {
198 : 0 : textcat_t *tables = (textcat_t*)h;
199 : :
200 : 0 : if(!h){return;}
201 : :
202 : 0 : for (size_t i=0; i<tables->size; i++)
203 : : {
204 : 0 : string language(fp_Name(tables->fprint[i]));
205 : 0 : if (start(language,lang) == 0)
206 : 0 : tables->fprint_disable[i] = mask;
207 : 0 : }
208 : : }
209 : :
210 : 0 : void SimpleGuesser::EnableLanguage(string lang)
211 : : {
212 : 0 : XableLanguage(lang, sal::static_int_cast< char >( 0xF0 ));
213 : 0 : }
214 : :
215 : 0 : void SimpleGuesser::DisableLanguage(string lang)
216 : : {
217 : 0 : XableLanguage(lang, sal::static_int_cast< char >( 0x0F ));
218 : 0 : }
219 : :
220 : : /**
221 : : *
222 : : */
223 : 0 : void SimpleGuesser::SetDBPath(const char* path, const char* prefix)
224 : : {
225 : 0 : if (h)
226 : 0 : textcat_Done(h);
227 : 0 : h = special_textcat_Init(path, prefix);
228 : 0 : }
229 : :
230 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|