~ubuntu-branches/ubuntu/hardy/texmacs/hardy

« back to all changes in this revision

Viewing changes to src/Resource/Dictionaries/dictionary.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Treinen
  • Date: 2004-04-19 20:34:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040419203400-g4e34ih0315wcn8v
Tags: upstream-1.0.3-R2
ImportĀ upstreamĀ versionĀ 1.0.3-R2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
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
******************************************************************************/
 
12
 
 
13
#include "dictionary.hpp"
 
14
#include "file.hpp"
 
15
#include "convert.hpp"
 
16
 
 
17
RESOURCE_CODE(dictionary);
 
18
 
 
19
/******************************************************************************
 
20
* Dictionary initialization
 
21
******************************************************************************/
 
22
 
 
23
dictionary_rep::dictionary_rep (string from2, string to2):
 
24
  rep<dictionary> (from2 * "-" * to2), table ("?"), from (from2), to (to2) {}
 
25
 
 
26
void
 
27
dictionary_rep::load (string fname) {
 
28
  string s;
 
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;
 
34
 
 
35
  int i, n= N(t);
 
36
  for (i=0; i<n; i++)
 
37
    if (is_func (t[i], TUPLE, 2) &&
 
38
        is_atomic (t[i][0]) && is_atomic (t[i][1]))
 
39
      {
 
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);
 
42
        table (l)= r;
 
43
      }
 
44
}
 
45
 
 
46
dictionary
 
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);
 
53
  return dict;
 
54
}
 
55
 
 
56
/******************************************************************************
 
57
* Translation routines
 
58
******************************************************************************/
 
59
 
 
60
string
 
61
dictionary_rep::translate (string s) {
 
62
  int i, n=N(s);
 
63
  if (n==0) return s;
 
64
  for (i=0; i<n; i++)
 
65
    if (is_iso_alpha (s[i]) || (s[i]=='|') || (s[i]==' ') ||
 
66
        ((i>0) && (s[i]=='#'))) break;
 
67
  if (i==n) {
 
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);
 
73
    return s;
 
74
  }
 
75
  if (i>0) return translate (s (0, i)) * translate (s (i, n));
 
76
  for (i=0; i<n; i++)
 
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;
 
80
  if (i==n) return s;
 
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);
 
84
 
 
85
  for (i=n-1; i>=0; i--)
 
86
    if (s[i]=='|') break;
 
87
  string radical= s (0, i<0? i+1: i);
 
88
  string word   = s (i+1, n);
 
89
 
 
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;
 
94
 
 
95
  if (!table->contains (source)) {
 
96
    if (N(radical)>0) {
 
97
      for (i=0; i<n; i++)
 
98
        if (s[i]=='|') break;
 
99
      return translate (s (i+1, n));
 
100
    }
 
101
    return word;
 
102
  }
 
103
 
 
104
  string dest= table [source];
 
105
  if (flag) dest= upcase_first (dest);
 
106
  return dest;
 
107
}