~paul-lucas/zorba/bug-932374

« back to all changes in this revision

Viewing changes to doc/zorba/ft_tokenizer.dox

  • Committer: Paul J. Lucas
  • Date: 2012-09-21 20:26:47 UTC
  • mfrom: (10819.2.235 zorba)
  • Revision ID: paul@lucasmail.org-20120921202647-fy9n4jduhrnljrnb
MergeĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
\section ft_tokenizer_tokization Tokenization
12
12
 
13
 
Using the
14
 
<a href="http://site.icu-project.org/">ICU library</a>,
15
 
Zorba's implementation of tokenization
 
13
By default,
 
14
Zorba uses the
 
15
<a href="http://site.icu-project.org/">ICU library</a>
 
16
for tokenization.
 
17
For Roman alphabets,
 
18
Zorba (ICU)
16
19
considers only alpha-numeric sequences of characters to be part of a token;
17
20
whitespace and punctuation characters are not
18
21
and separate tokens.
35
38
  typedef /* implementation-defined */ ptr;
36
39
  typedef /* implementation-defined */ size_type;
37
40
 
38
 
  struct Numbers {
 
41
  struct State {
39
42
    typedef Tokenizer::size_type value_type;
40
43
 
41
44
    value_type token;   // Token number.
42
45
    value_type sent;    // Sentence number.
43
46
    value_type para;    // Paragraph number.
44
47
 
45
 
    Numbers();
 
48
    State();
46
49
  };
47
50
 
48
51
  class Callback {
69
72
  virtual void properties( Properties *result ) const = 0;
70
73
 
71
74
  virtual void destroy() const = 0;
72
 
  Numbers& numbers();
73
 
  Numbers const& numbers() const;
 
75
  State& state();
 
76
  State const& state() const;
74
77
 
75
78
  void tokenize_node( Item const &node, locale::iso639_1::type lang, Callback &callback );
76
79
 
78
81
                                bool wildcards, Callback &callback, Item const *item = 0 ) = 0;
79
82
 
80
83
protected:
81
 
  Tokenizer( Numbers& );
 
84
  Tokenizer( State& );
82
85
  virtual ~Tokenizer();
83
86
 
84
87
  bool find_lang_attribute( Item const&, locale::iso639_1::type *lang );
92
95
and why the destructor is \c protected,
93
96
see the \ref memory_management document.
94
97
 
95
 
The \c Numbers \c struct is created by Zorba
 
98
The \c State \c struct is created by Zorba
96
99
and passed to your constructor.
97
100
It simply keeps track of the current
98
101
token, sentence, and paragraph numbers.
117
120
  <tr>
118
121
    <td>\c lang</td>
119
122
    <td>
120
 
      The language of the string.
 
123
      The
 
124
      <a href="http://www.w3.org/TR/xmlschema-2/#language">language</a>
 
125
      of the string.
121
126
    </td>
122
127
  </tr>
123
128
  <tr>
132
137
  <tr>
133
138
    <td>\c callback</td>
134
139
    <td>
135
 
      The Callback to call once per token.
 
140
      The \c Callback to call once per token.
136
141
    </td>
137
142
  </tr>
138
143
  <tr>
147
152
  </tr>
148
153
</table>
149
154
 
150
 
A complete implementation of \c %tokenize() is non-trivial
 
155
A complete implementation of \c %tokenize_string() is non-trivial
151
156
and therefore an example is beyond the scope of this API documentation.
152
157
However,
153
158
the things a tokenizer should take into consideration include:
197
202
    Item qname;
198
203
    item.getNodeName( qname );
199
204
    if ( /* qname matches an XHTML block-level element's name */ )
200
 
      ++numbers().para;
 
205
      ++state().para;
201
206
}
202
207
\endcode
203
208
 
232
237
  <tr>
233
238
    <td>\c languages</td>
234
239
    <td>
235
 
      The list of languages supported by the tokenizer.
 
240
      The list of
 
241
      <a href="http://www.w3.org/TR/xmlschema-2/#language">languages</a>
 
242
      supported by the tokenizer.
236
243
    </td>
237
244
  </tr>
238
245
  <tr>
247
254
 
248
255
In addition to a \c Tokenizer,
249
256
you must also implement a \c TokenizerProvider
250
 
that, given a language, provides a \c Tokenizer for that language:
 
257
that,
 
258
given a <a href="http://www.w3.org/TR/xmlschema-2/#language">language</a>,
 
259
provides a \c Tokenizer for that language:
251
260
 
252
261
\code
253
262
class TokenizerProvider {
254
263
public:
255
264
  virtual ~TokenizerProvider();
256
 
  virtual bool getTokenizer( locale::iso639_1::type lang, Tokenizer::Numbers *numbers = 0, Tokenizer::ptr* = 0 ) const = 0;
 
265
  virtual bool getTokenizer( locale::iso639_1::type lang, Tokenizer::State *state = 0, Tokenizer::ptr* = 0 ) const = 0;
257
266
};
258
267
\endcode
259
268
 
262
271
<table>
263
272
  <tr>
264
273
    <td>\c lang</td>
265
 
    <td>The language to tokenize.</td>
 
274
    <td>
 
275
      The
 
276
      <a href="http://www.w3.org/TR/xmlschema-2/#language">language</a>
 
277
      to tokenize.
 
278
    </td>
266
279
  </tr>
267
280
  <tr>
268
 
    <td>\c num</td>
 
281
    <td>\c state</td>
269
282
    <td>
270
 
      The \c Numbers to use.
 
283
      The \c State to use.
271
284
      If \c null,
272
285
      \a t is not set.
273
286
    </td>
286
299
\code
287
300
class MyTokenizerProvider : public TokenizerProvider {
288
301
public:
289
 
  getTokenizer( locale::iso639_1::type lang, Tokenizer::Numbers* = 0, Tokenizer::ptr* = 0 ) const;
 
302
  getTokenizer( locale::iso639_1::type lang, Tokenizer::State* = 0, Tokenizer::ptr* = 0 ) const;
290
303
};
291
304
 
292
 
bool MyTokenizerProvider::getTokenizer( locale::iso639_1::type lang, Tokenizer::Numbers *num, Tokenizer::ptr *t ) const {
 
305
bool MyTokenizerProvider::getTokenizer( locale::iso639_1::type lang, Tokenizer::State *state, Tokenizer::ptr *t ) const {
293
306
  switch ( lang ) {
294
307
    case iso639_1::en:
295
 
      if ( num && t )
 
308
      if ( state && t )
296
309
        t->reset( new MyTokenizer );
297
310
      return true;
298
311
    default: