2
/******************************************************************************
3
* MODULE : dictionary.cpp
4
* DESCRIPTION: used for translations and analysing text
5
* COPYRIGHT : (C) 1999 Joris van der Hoeven
6
*******************************************************************************
7
* This software falls under the GNU general public license and comes WITHOUT
8
* ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
9
* If you don't have this file, write to the Free Software Foundation, Inc.,
10
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
11
******************************************************************************/
13
#include "dictionary.hpp"
15
#include "convert.hpp"
17
RESOURCE_CODE(dictionary);
19
/******************************************************************************
20
* Dictionary initialization
21
******************************************************************************/
23
dictionary_rep::dictionary_rep (string from2, string to2):
24
rep<dictionary> (from2 * "-" * to2), table ("?"), from (from2), to (to2) {}
27
dictionary_rep::load (string fname) {
29
fname= fname * ".scm";
30
if (DEBUG_AUTO) cout << "TeXmacs] Loading " << fname << "\n";
31
if (load_string (url ("$TEXMACS_PATH/langs/natural/dic", fname), s)) return;
32
tree t= block_to_scheme_tree (s);
33
if (!is_tuple (t)) return;
37
if (is_func (t[i], TUPLE, 2) &&
38
is_atomic (t[i][0]) && is_atomic (t[i][1]))
40
string l= t[i][0]->label; if (is_quoted (l)) l= unquote (l);
41
string r= t[i][1]->label; if (is_quoted (r)) r= unquote (r);
47
load_dictionary (string from, string to) {
48
string name= from * "-" * to;
49
if (dictionary::instances -> contains (name))
50
return dictionary (name);
51
dictionary dict= new dictionary_rep (from, to);
52
if (from != to) dict->load (name);
56
/******************************************************************************
57
* Translation routines
58
******************************************************************************/
61
dictionary_rep::translate (string s) {
65
if (is_iso_alpha (s[i]) || (s[i]=='|') || (s[i]==' ') ||
66
((i>0) && (s[i]=='#'))) break;
68
if (s[0]=='#') return " " * translate (s (1,n));
69
if (s[n-1]=='#') return translate (s (0,n-1)) * " ";
70
if ((to == "french") &&
71
((s[n-1] == ':') || (s[n-1] == '!') || (s[n-1] == '?')) &&
72
((n==1) || (s[n-2] != ' '))) return s (0, n-1) * " " * s (n-1, n);
75
if (i>0) return translate (s (0, i)) * translate (s (i, n));
77
if ((!is_iso_alpha (s[i])) && (s[i]!='|') && (s[i]!=' ')) break;
78
if (i<n) return translate (s (0, i)) * translate (s (i, n));
79
for (i=0; i<n; i++) if (s[i]!=' ') break;
81
if (i>0) return s (0, i) * translate (s (i, n));
82
for (i=n-1; i>=0; i--) if (s[i]!=' ') break;
83
if (i<n-1) return translate (s (0, i+1)) * s (i+1, n);
85
for (i=n-1; i>=0; i--)
87
string radical= s (0, i<0? i+1: i);
88
string word = s (i+1, n);
90
if (N(word)==0) return "";
91
bool flag= is_upcase (word[0]);
92
string source= locase_first (word);
93
if (N(radical)>0) source= locase_all (radical) * "|" * source;
95
if (!table->contains (source)) {
99
return translate (s (i+1, n));
104
string dest= table [source];
105
if (flag) dest= upcase_first (dest);