2 **********************************************************************
3 * Copyright (C) 1999-2004 IBM Corp. All rights reserved.
4 **********************************************************************
5 * Date Name Description
6 * 12/1/99 rgillam Complete port from Java.
7 * 01/13/2000 helena Added UErrorCode to ctors.
8 **********************************************************************
14 #include "unicode/rbbi.h"
16 #if !UCONFIG_NO_BREAK_ITERATION
20 /* forward declaration */
21 class DictionaryBasedBreakIteratorTables
;
24 * A subclass of RuleBasedBreakIterator that adds the ability to use a dictionary
25 * to further subdivide ranges of text beyond what is possible using just the
26 * state-table-based algorithm. This is necessary, for example, to handle
27 * word and line breaking in Thai, which doesn't use spaces between words. The
28 * state-table-based algorithm used by RuleBasedBreakIterator is used to divide
29 * up text as far as possible, and then contiguous ranges of letters are
30 * repeatedly compared against a list of known words (i.e., the dictionary)
31 * to divide them up into words.
33 * <p>Applications do not normally need to include this header.</p>
35 * <p>This class will probably be deprecated in a future release of ICU, and replaced
36 * with a more flexible and capable dictionary based break iterator. This change
37 * should be invisible to applications, because creation and use of instances of
38 * DictionaryBasedBreakIterator is through the factories and abstract
39 * API on class BreakIterator, which will remain stable.</p>
41 * <p>This class is not intended to be subclassed.</p>
44 * DictionaryBasedBreakIterator uses the same rule language as RuleBasedBreakIterator,
45 * but adds one more special substitution name: <dictionary>. This substitution
46 * name is used to identify characters in words in the dictionary. The idea is that
47 * if the iterator passes over a chunk of text that includes two or more characters
48 * in a row that are included in <dictionary>, it goes back through that range and
49 * derives additional break positions (if possible) using the dictionary.
51 * DictionaryBasedBreakIterator is also constructed with the filename of a dictionary
52 * file. It follows a prescribed search path to locate the dictionary (right now,
53 * it looks for it in /com/ibm/text/resources in each directory in the classpath,
54 * and won't find it in JAR files, but this location is likely to change). The
55 * dictionary file is in a serialized binary format. We have a very primitive (and
56 * slow) BuildDictionaryFile utility for creating dictionary files, but aren't
57 * currently making it public. Contact us for help.
59 * <b> NOTE </b> The DictionaryBasedIterator class is still under development. The
60 * APIs are not in stable condition yet.
62 class U_COMMON_API DictionaryBasedBreakIterator
: public RuleBasedBreakIterator
{
67 * when a range of characters is divided up using the dictionary, the break
68 * positions that are discovered are stored here, preventing us from having
69 * to use either the dictionary or the state table again until the iterator
70 * leaves this range of text
72 int32_t* cachedBreakPositions
;
75 * The number of elements in cachedBreakPositions
77 int32_t numCachedBreakPositions
;
80 * if cachedBreakPositions is not null, this indicates which item in the
81 * cache the current iteration position refers to
83 int32_t positionInCache
;
85 DictionaryBasedBreakIteratorTables
*fTables
;
87 /**=======================================================================
88 * Create a dictionary based break boundary detection iterator.
89 * @param tablesImage The location for the dictionary to be loaded into memory
90 * @param dictionaryFilename The name of the dictionary file
91 * @param status the error code status
92 * @return A dictionary based break detection iterator. The UErrorCode& status
93 * parameter is used to return status information to the user.
94 * To check whether the construction succeeded or not, you should check
95 * the value of U_SUCCESS(err). If you wish more detailed information, you
96 * can check for informational error results which still indicate success. For example,
97 * U_FILE_ACCESS_ERROR will be returned if the file does not exist.
98 * The caller owns the returned object and is responsible for deleting it.
99 ======================================================================= */
100 DictionaryBasedBreakIterator(UDataMemory
* tablesImage
, const char* dictionaryFilename
, UErrorCode
& status
);
103 //=======================================================================
105 //=======================================================================
111 virtual ~DictionaryBasedBreakIterator();
114 * Default constructor. Creates an "empty" break iterator.
115 * Such an iterator can subsequently be assigned to.
116 * @return the newly created DictionaryBaseBreakIterator.
119 DictionaryBasedBreakIterator();
123 * @param other The DictionaryBasedBreakIterator to be copied.
124 * @return the newly created DictionaryBasedBreakIterator.
127 DictionaryBasedBreakIterator(const DictionaryBasedBreakIterator
&other
);
130 * Assignment operator.
131 * @param that The object to be copied.
132 * @return the newly set DictionaryBasedBreakIterator.
135 DictionaryBasedBreakIterator
& operator=(const DictionaryBasedBreakIterator
& that
);
138 * Returns a newly-constructed RuleBasedBreakIterator with the same
139 * behavior, and iterating over the same text, as this one.
140 * @return Returns a newly-constructed RuleBasedBreakIterator.
143 virtual BreakIterator
* clone(void) const;
145 //=======================================================================
146 // BreakIterator overrides
147 //=======================================================================
149 * Advances the iterator backwards, to the last boundary preceding this one.
150 * @return The position of the last boundary position preceding this one.
153 virtual int32_t previous(void);
156 * Sets the iterator to refer to the first boundary position following
157 * the specified position.
158 * @param offset The position from which to begin searching for a break position.
159 * @return The position of the first break after the current position.
162 virtual int32_t following(int32_t offset
);
165 * Sets the iterator to refer to the last boundary position before the
166 * specified position.
167 * @param offset The position to begin searching for a break from.
168 * @return The position of the last boundary before the starting position.
171 virtual int32_t preceding(int32_t offset
);
174 * Returns the class ID for this class. This is useful only for
175 * comparing to a return value from getDynamicClassID(). For example:
177 * Base* polymorphic_pointer = createPolymorphicObject();
178 * if (polymorphic_pointer->getDynamicClassID() ==
179 * Derived::getStaticClassID()) ...
181 * @return The class ID for all objects of this class.
184 static UClassID U_EXPORT2
getStaticClassID(void);
187 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
188 * This method is to implement a simple version of RTTI, since not all
189 * C++ compilers support genuine RTTI. Polymorphic operator==() and
190 * clone() methods call this method.
192 * @return The class ID for this object. All objects of a
193 * given class have the same class ID. Objects of
194 * other classes have different class IDs.
197 virtual UClassID
getDynamicClassID(void) const;
200 //=======================================================================
202 //=======================================================================
204 * This method is the actual implementation of the next() method. All iteration
205 * vectors through here. This method initializes the state machine to state 1
206 * and advances through the text character by character until we reach the end
207 * of the text or the state machine transitions to state 0. We update our return
208 * value every time the state machine passes through a possible end state.
211 virtual int32_t handleNext(void);
214 * removes the cache of break positions (usually in response to a change in
215 * position of some sort)
218 virtual void reset(void);
221 * init Initialize a dbbi. Common routine for use by constructors.
227 * @param stackBuffer user allocated space for the new clone. If NULL new memory will be allocated.
228 * If buffer is not large enough, new memory will be allocated.
229 * @param BufferSize reference to size of allocated space.
230 * If BufferSize == 0, a sufficient size for use in cloning will
231 * be returned ('pre-flighting')
232 * If BufferSize is not enough for a stack-based safe clone,
233 * new memory will be allocated.
234 * @param status to indicate whether the operation went on smoothly or there were errors
235 * An informational status value, U_SAFECLONE_ALLOCATED_ERROR, is used if any allocations were
237 * @return pointer to the new clone
240 virtual BreakIterator
* createBufferClone(void *stackBuffer
,
247 * This is the function that actually implements the dictionary-based
248 * algorithm. Given the endpoints of a range of text, it uses the
249 * dictionary to determine the positions of any boundaries in this
250 * range. It stores all the boundary positions it discovers in
251 * cachedBreakPositions so that we only have to do this work once
252 * for each time we enter the range.
253 * @param startPos The start position of a range of text
254 * @param endPos The end position of a range of text
255 * @param status The error code status
257 void divideUpDictionaryRange(int32_t startPos
, int32_t endPos
, UErrorCode
&status
);
261 * HSYS : Please revisit with Rich, the ctors of the DBBI class is currently
264 friend class DictionaryBasedBreakIteratorTables
;
265 friend class BreakIterator
;
270 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */