aboutsummaryrefslogtreecommitdiff
path: root/dw/hyphenator.cc
blob: 7a1f5df7ca4d3d859bdc580c102f5bd3936db6a4 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "hyphenator.hh"

#include "../lout/misc.hh"
#include <stdio.h>
#include <string.h>

#define LEN 1000

/*
 * This is a direct translation of the Python implementation by Ned
 * Batchelder.
 */

using namespace lout::object;
using namespace lout::container::typed;
using namespace lout::misc;

namespace dw {

HashTable <TypedPair <TypedPointer <core::Platform>, ConstString>,
           Hyphenator> *Hyphenator::hyphenators =
   new HashTable <TypedPair <TypedPointer <core::Platform>, ConstString>,
                  Hyphenator> (true, true);

Hyphenator::Hyphenator (core::Platform *platform,
                        const char *patFile, const char *excFile)
{
   this->platform = platform;
   tree = NULL; // As long we are not sure whether a pattern file can be read.

   FILE *patF = fopen (patFile, "r");
   if (patF) {
      tree = new HashTable <Integer, Collection <Integer> > (true, true);
      while (!feof (patF)) {
         char buf[LEN + 1];
         char *s = fgets (buf, LEN, patF);
         if (s) {
             // TODO Better exit with an error, when the line is too long.
            int l = strlen (s);
            if (s[l - 1] == '\n')
               s[l - 1] = 0;
            insertPattern (s);
         }
      }
      fclose (patF);
   }

   exceptions = NULL; // Again, only instanciated when needed.

   FILE *excF = fopen (excFile, "r");
   if (excF) {
      exceptions = new HashTable <ConstString, Vector <Integer> > (true, true);
      while (!feof (excF)) {
         char buf[LEN + 1];
         char *s = fgets (buf, LEN, excF);
         if (s) {
             // TODO Better exit with an error, when the line is too long.
            int l = strlen (s);
            if (s[l - 1] == '\n')
               s[l - 1] = 0;
            insertException (s);
         }
      }
      fclose (excF);
   }
}

Hyphenator::~Hyphenator ()
{
   if (tree)
      delete tree;
   if (exceptions)
      delete exceptions;
}

Hyphenator *Hyphenator::getHyphenator (core::Platform *platform,
                                       const char *lang)
{
   // TODO Not very efficient. Other key than TypedPair?
   // (Keeping the parts of the pair on the stack does not help, since
   // ~TypedPair deletes them, so they have to be kept on the heap.)
   TypedPair <TypedPointer <core::Platform>, ConstString> *pair =
      new TypedPair <TypedPointer <core::Platform>,
                     ConstString> (new TypedPointer <core::Platform> (platform),
                                   new String (lang));

   Hyphenator *hyphenator = hyphenators->get (pair);
   if (hyphenator)
      delete pair;
   else {
      // TODO Much hard-coded!
      char patFile [256];
      sprintf (patFile, "/usr/local/lib/dillo/hyphenation/%s.pat", lang);
      char excFile [256];
      sprintf (excFile, "/usr/local/lib/dillo/hyphenation/%s.exc", lang);

      //printf ("Loading hyphenation patterns for language '%s' from '%s' and "
      //        "exceptions from '%s' ...\n", lang, patFile, excFile);

      hyphenator = new Hyphenator (platform, patFile, excFile);
      hyphenators->put (pair, hyphenator);
   }

   //lout::misc::StringBuffer sb;
   //hyphenators->intoStringBuffer (&sb);
   //printf ("%s\n", sb.getChars ());

   return hyphenator;
}

void Hyphenator::insertPattern (char *s)
{
   // Convert the a pattern like 'a1bc3d4' into a string of chars 'abcd'
   // and a list of points [ 0, 1, 0, 3, 4 ].
   int l = strlen (s);
   char chars [l + 1];
   Vector <Integer> *points = new Vector <Integer> (1, true);

   // TODO numbers consisting of multiple digits?
   // TODO Encoding: This implementation works exactly like the Python
   // implementation, based on UTF-8. Does this always work?
   int numChars = 0;
   for (int i = 0; s[i]; i++)
      if (s[i] >= '0' && s[i] <= '9')
         points->put (new Integer (s[i] - '0'), numChars);
      else
         chars[numChars++] = s[i];
   chars[numChars] = 0;

   for (int i = 0; i < numChars + 1; i++) {
      Integer *val = points->get (i);
      if (val == NULL)
         points->put (new Integer (0), i);
   }

   // Insert the pattern into the tree.  Each character finds a dict
   // another level down in the tree, and leaf nodes have the list of
   // points.

   HashTable <Integer, Collection <Integer> > *t = tree;
   for (int i = 0; chars[i]; i++) {
      Integer c (chars[i]);
      if (!t->contains(&c))
         t->put (new Integer (chars[i]),
                 new HashTable <Integer, Collection <Integer> > (true, true));
      t = (HashTable <Integer, Collection <Integer> >*) t->get (&c);
   }

   t->put (new Integer (0), points);
}

void Hyphenator::insertException (char *s)
{
   Vector<Integer> *breaks = new Vector<Integer> (1, true);

   int len = strlen (s);
   for (int i = 0; i < len - 1; i++)
      if((unsigned char)s[i] == 0xc2 && (unsigned char)s[i + 1] == 0xad)
         breaks->put (new Integer (i - 2 * breaks->size()));

   char noHyphens[len - 2 * breaks->size() + 1];
   int j = 0;
   for (int i = 0; i < len; ) {
      if(i < len - 1 &&
         (unsigned char)s[i] == 0xc2 && (unsigned char)s[i + 1] == 0xad)
         i += 2;
      else
         noHyphens[j++] = s[i++];
   }
   noHyphens[j] = 0;

   exceptions->put (new String (noHyphens), breaks);
}

/**
 * Simple test to avoid much costs. Passing it does not mean that the word
 * can be hyphenated.
 */
bool Hyphenator::isHyphenationCandidate (const char *word)
{
   // Short words aren't hyphenated.
   return (strlen (word) > 4); // TODO UTF-8?
}   

/**
 * Test whether the character on which "s" points (UTF-8) is an actual
 * part of the word. Other characters at the beginning and end are
 * ignored.
 *
 * TODO Currently only suitable for English and German.
 * TODO Only lowercase. (Uppercase not needed.)
 */
bool Hyphenator::isCharPartOfActualWord (char *s)
{
   // Return true when "s" points to a letter.
   return (s[0] >= 'a' && s[0] <= 'z') ||
      // UTF-8: starts with 0xc3
      ((unsigned char)s[0] == 0xc3 &&
       ((unsigned char)s[1] == 0xa4 /* ä */ ||
        (unsigned char)s[1] == 0xb6 /* ö */ ||
        (unsigned char)s[1] == 0xbc /* ü */ ||
        (unsigned char)s[1] == 0x9f /* ß */ ));
}

/**
 * Given a word, returns a list of the possible hyphenation points.
 */
int *Hyphenator::hyphenateWord(const char *word, int *numBreaks)
{
   if ((tree == NULL && exceptions ==NULL) || !isHyphenationCandidate (word)) {
      *numBreaks = 0;
      return NULL;
   }

   char *wordLc = platform->textToLower (word, strlen (word));

   // Determine "actual" word. See isCharPartOfActualWord for exact definition.
   
   // Only this actual word is used, and "startActualWord" is added to the 
   // break positions, so that these refer to the total word.
   int startActualWord = 0;
   while (wordLc[startActualWord] &&
          !isCharPartOfActualWord (wordLc + startActualWord))
      startActualWord = platform->nextGlyph (wordLc, startActualWord);

   if (wordLc[startActualWord] == 0) {
      // No letters etc in word: do not hyphenate at all.
      delete wordLc;
      *numBreaks = 0;
      return NULL;
   }

   int endActualWord = startActualWord, i = endActualWord;
   while (wordLc[i]) {
      if (isCharPartOfActualWord (wordLc + i))
         endActualWord = i;
      i = platform->nextGlyph (wordLc, i);
   }
   
   endActualWord = platform->nextGlyph (wordLc, endActualWord);
   wordLc[endActualWord] = 0;

   // If the word is an exception, get the stored points.
   Vector <Integer> *exceptionalBreaks;
   ConstString key (wordLc + startActualWord);
   if (exceptions != NULL && (exceptionalBreaks = exceptions->get (&key))) {
      int *result = new int[exceptionalBreaks->size()];
      for (int i = 0; i < exceptionalBreaks->size(); i++)
         result[i] = exceptionalBreaks->get(i)->getValue() + startActualWord;
      delete wordLc;
      *numBreaks = exceptionalBreaks->size();
      return result;
   }

   // tree == NULL means that there is no pattern file.
   if (tree == NULL) {
      delete wordLc;
      *numBreaks = 0;
      return NULL;
   }

   char work[strlen (word) + 3];
   strcpy (work, ".");
   strcat (work, wordLc + startActualWord);
   delete wordLc;
   strcat (work, ".");
   
   int l = strlen (work);
   SimpleVector <int> points (l + 1);
   points.setSize (l + 1, 0);

   Integer null (0);

   for (int i = 0; i < l; i++) {
      HashTable <Integer, Collection <Integer> > *t = tree;
      for (int j = i; j < l; j++) {
         Integer c (work[j]);
         if (t->contains (&c)) {
            t = (HashTable <Integer, Collection <Integer> >*) t->get (&c);
            if (t->contains (&null)) {
               Vector <Integer> *p = (Vector <Integer>*) t->get (&null);

               for (int k = 0; k < p->size (); k++)
                  points.set(i + k, lout::misc::max (points.get (i + k),
                                                     p->get(k)->getValue()));
            }
         } else
            break;
      }
   }  

   // No hyphens in the first two chars or the last two.
   points.set (1, 0);
   points.set (2, 0);
   points.set (points.size() - 2, 0);
   points.set (points.size() - 3, 0);
   
   // Examine the points to build the pieces list.
   SimpleVector <int> breakPos (1);

   int n = lout::misc::min ((int)strlen (word), points.size () - 2);
   for (int i = 0; i < n; i++) {
      if (points.get(i + 2) % 2) {
         breakPos.increase ();
         breakPos.set (breakPos.size() - 1, i + 1 + startActualWord);
      }
   }

   *numBreaks = breakPos.size ();
   if (*numBreaks == 0)
      return NULL;
   else {
      // Could save some cycles by directly returning the array in the
      // SimpleVector.
      int *breakPosArray = new int[*numBreaks];
      for (int i = 0; i < *numBreaks; i++)
         breakPosArray[i] = breakPos.get(i);
      return breakPosArray;
   }
}

} // namespace dw