1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 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 */
29 struct icu_tokenizer *icu_tokenizer_create(const char *locale, char action,
32 struct icu_tokenizer * tokenizer
33 = (struct icu_tokenizer *) xmalloc(sizeof(struct icu_tokenizer));
35 tokenizer->action = action;
37 tokenizer->buf16 = icu_buf_utf16_create(0);
38 tokenizer->token_count = 0;
39 tokenizer->token_id = 0;
40 tokenizer->token_start = 0;
41 tokenizer->token_end = 0;
43 switch (tokenizer->action)
47 tokenizer->bi = ubrk_open(UBRK_LINE, locale, 0, 0, status);
51 tokenizer->bi = ubrk_open(UBRK_SENTENCE, locale, 0, 0, status);
55 tokenizer->bi = ubrk_open(UBRK_WORD, locale, 0, 0, status);
59 tokenizer->bi = ubrk_open(UBRK_CHARACTER, locale, 0, 0, status);
63 tokenizer->bi = ubrk_open(UBRK_TITLE, locale, 0, 0, status);
66 *status = U_UNSUPPORTED_ERROR;
71 /* ICU error stuff is a very funny business */
72 if (U_SUCCESS(*status))
75 /* freeing if failed */
76 icu_tokenizer_destroy(tokenizer);
80 void icu_tokenizer_destroy(struct icu_tokenizer * tokenizer)
84 icu_buf_utf16_destroy(tokenizer->buf16);
86 ubrk_close(tokenizer->bi);
91 int icu_tokenizer_attach(struct icu_tokenizer * tokenizer,
92 struct icu_buf_utf16 * src16,
95 if (!tokenizer || !tokenizer->bi || !src16)
98 icu_buf_utf16_copy(tokenizer->buf16, src16);
100 tokenizer->token_count = 0;
101 tokenizer->token_id = 0;
102 tokenizer->token_start = 0;
103 tokenizer->token_end = 0;
105 ubrk_setText(tokenizer->bi,
106 tokenizer->buf16->utf16, tokenizer->buf16->utf16_len, status);
108 if (U_FAILURE(*status))
114 int32_t icu_tokenizer_next_token(struct icu_tokenizer * tokenizer,
115 struct icu_buf_utf16 * tkn16,
118 int32_t tkn_start = 0;
122 if (!tokenizer || !tokenizer->bi
123 || !tokenizer->buf16 || !tokenizer->buf16->utf16_len)
126 never change tokenizer->buf16 and keep always invariant
127 0 <= tokenizer->token_start
128 <= tokenizer->token_end
129 <= tokenizer->buf16->utf16_len
130 returns length of token
133 if (0 == tokenizer->token_end) /* first call */
134 tkn_start = ubrk_first(tokenizer->bi);
135 else /* successive calls */
136 tkn_start = tokenizer->token_end;
138 /* get next position */
139 tkn_end = ubrk_next(tokenizer->bi);
141 /* repairing invariant at end of ubrk, which is UBRK_DONE = -1 */
142 if (UBRK_DONE == tkn_end)
143 tkn_end = tokenizer->buf16->utf16_len;
145 /* copy out if everything is well */
146 if (U_FAILURE(*status))
149 /* everything OK, now update internal state */
150 tkn_len = tkn_end - tkn_start;
154 tokenizer->token_count++;
155 tokenizer->token_id++;
157 tokenizer->token_id = 0;
159 tokenizer->token_start = tkn_start;
160 tokenizer->token_end = tkn_end;
162 /* copying into token buffer if it exists */
164 if (tkn16->utf16_cap < tkn_len)
165 icu_buf_utf16_resize(tkn16, (size_t) tkn_len * 2);
167 u_strncpy(tkn16->utf16, &(tokenizer->buf16->utf16)[tkn_start],
170 tkn16->utf16_len = tkn_len;
176 int32_t icu_tokenizer_token_count(struct icu_tokenizer * tokenizer)
178 return tokenizer->token_count;
181 #endif /* YAZ_HAVE_ICU */
186 * c-file-style: "Stroustrup"
187 * indent-tabs-mode: nil
189 * vim: shiftwidth=4 tabstop=8 expandtab