1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
8 * \brief ICU tokenization - using ubrk_-functions from ICU
16 #include <yaz/xmalloc.h>
18 #include <yaz/icu_I18N.h>
26 #include <unicode/ustring.h> /* some more string fcns*/
27 #include <unicode/uchar.h> /* char names */
33 struct icu_buf_utf16 * buf16;
44 0 <= token_id <= token_count
48 struct icu_tokenizer *icu_tokenizer_create(const char *locale, char action,
51 struct icu_tokenizer * tokenizer
52 = (struct icu_tokenizer *) xmalloc(sizeof(struct icu_tokenizer));
54 tokenizer->action = action;
56 tokenizer->buf16 = icu_buf_utf16_create(0);
57 tokenizer->token_count = 0;
58 tokenizer->token_id = 0;
59 tokenizer->token_start = 0;
60 tokenizer->token_end = 0;
62 switch (tokenizer->action)
66 tokenizer->bi = ubrk_open(UBRK_LINE, locale, 0, 0, status);
70 tokenizer->bi = ubrk_open(UBRK_SENTENCE, locale, 0, 0, status);
74 tokenizer->bi = ubrk_open(UBRK_WORD, locale, 0, 0, status);
78 tokenizer->bi = ubrk_open(UBRK_CHARACTER, locale, 0, 0, status);
82 tokenizer->bi = ubrk_open(UBRK_TITLE, locale, 0, 0, status);
85 *status = U_UNSUPPORTED_ERROR;
90 /* ICU error stuff is a very funny business */
91 if (U_SUCCESS(*status))
94 /* freeing if failed */
95 icu_tokenizer_destroy(tokenizer);
99 void icu_tokenizer_destroy(struct icu_tokenizer * tokenizer)
103 icu_buf_utf16_destroy(tokenizer->buf16);
105 ubrk_close(tokenizer->bi);
110 int icu_tokenizer_attach(struct icu_tokenizer * tokenizer,
111 struct icu_buf_utf16 * src16,
114 if (!tokenizer || !tokenizer->bi || !src16)
117 icu_buf_utf16_copy(tokenizer->buf16, src16);
119 tokenizer->token_count = 0;
120 tokenizer->token_id = 0;
121 tokenizer->token_start = 0;
122 tokenizer->token_end = 0;
124 ubrk_setText(tokenizer->bi,
125 tokenizer->buf16->utf16, tokenizer->buf16->utf16_len, status);
127 if (U_FAILURE(*status))
133 int32_t icu_tokenizer_next_token(struct icu_tokenizer * tokenizer,
134 struct icu_buf_utf16 * tkn16,
137 int32_t tkn_start = 0;
141 if (!tokenizer || !tokenizer->bi
142 || !tokenizer->buf16 || !tokenizer->buf16->utf16_len)
145 never change tokenizer->buf16 and keep always invariant
146 0 <= tokenizer->token_start
147 <= tokenizer->token_end
148 <= tokenizer->buf16->utf16_len
149 returns length of token
152 if (0 == tokenizer->token_end) /* first call */
153 tkn_start = ubrk_first(tokenizer->bi);
154 else /* successive calls */
155 tkn_start = tokenizer->token_end;
157 /* get next position */
158 tkn_end = ubrk_next(tokenizer->bi);
160 /* repairing invariant at end of ubrk, which is UBRK_DONE = -1 */
161 if (UBRK_DONE == tkn_end)
162 tkn_end = tokenizer->buf16->utf16_len;
164 /* copy out if everything is well */
165 if (U_FAILURE(*status))
168 /* everything OK, now update internal state */
169 tkn_len = tkn_end - tkn_start;
173 tokenizer->token_count++;
174 tokenizer->token_id++;
176 tokenizer->token_id = 0;
178 tokenizer->token_start = tkn_start;
179 tokenizer->token_end = tkn_end;
181 /* copying into token buffer if it exists */
183 if (tkn16->utf16_cap < tkn_len)
184 icu_buf_utf16_resize(tkn16, (size_t) tkn_len * 2);
186 u_strncpy(tkn16->utf16, &(tokenizer->buf16->utf16)[tkn_start],
189 tkn16->utf16_len = tkn_len;
195 int32_t icu_tokenizer_token_count(struct icu_tokenizer * tokenizer)
197 return tokenizer->token_count;
200 #endif /* YAZ_HAVE_ICU */
205 * c-file-style: "Stroustrup"
206 * indent-tabs-mode: nil
208 * vim: shiftwidth=4 tabstop=8 expandtab