2 **********************************************************************
3 * Copyright (C) 1999-2003 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
;
90 static const char fgClassID
;
92 /**=======================================================================
93 * Create a dictionary based break boundary detection iterator.
94 * @param tablesImage The location for the dictionary to be loaded into memory
95 * @param dictionaryFilename The name of the dictionary file
96 * @param status the error code status
97 * @return A dictionary based break detection iterator. The UErrorCode& status
98 * parameter is used to return status information to the user.
99 * To check whether the construction succeeded or not, you should check
100 * the value of U_SUCCESS(err). If you wish more detailed information, you
101 * can check for informational error results which still indicate success. For example,
102 * U_FILE_ACCESS_ERROR will be returned if the file does not exist.
103 * The caller owns the returned object and is responsible for deleting it.
104 ======================================================================= */
105 DictionaryBasedBreakIterator(UDataMemory
* tablesImage
, const char* dictionaryFilename
, UErrorCode
& status
);
108 //=======================================================================
110 //=======================================================================
116 virtual ~DictionaryBasedBreakIterator();
119 * Default constructor. Creates an "empty" break iterator.
120 * Such an iterator can subsequently be assigned to.
121 * @return the newly created DictionaryBaseBreakIterator.
124 DictionaryBasedBreakIterator();
128 * @param other The DictionaryBasedBreakIterator to be copied.
129 * @return the newly created DictionaryBasedBreakIterator.
132 DictionaryBasedBreakIterator(const DictionaryBasedBreakIterator
&other
);
135 * Assignment operator.
136 * @param that The object to be copied.
137 * @return the newly set DictionaryBasedBreakIterator.
140 DictionaryBasedBreakIterator
& operator=(const DictionaryBasedBreakIterator
& that
);
143 * Returns a newly-constructed RuleBasedBreakIterator with the same
144 * behavior, and iterating over the same text, as this one.
145 * @return Returns a newly-constructed RuleBasedBreakIterator.
148 virtual BreakIterator
* clone(void) const;
150 //=======================================================================
151 // BreakIterator overrides
152 //=======================================================================
154 * Advances the iterator backwards, to the last boundary preceding this one.
155 * @return The position of the last boundary position preceding this one.
158 virtual int32_t previous(void);
161 * Sets the iterator to refer to the first boundary position following
162 * the specified position.
163 * @offset The position from which to begin searching for a break position.
164 * @return The position of the first break after the current position.
167 virtual int32_t following(int32_t offset
);
170 * Sets the iterator to refer to the last boundary position before the
171 * specified position.
172 * @offset The position to begin searching for a break from.
173 * @return The position of the last boundary before the starting position.
176 virtual int32_t preceding(int32_t offset
);
179 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
180 * This method is to implement a simple version of RTTI, since not all
181 * C++ compilers support genuine RTTI. Polymorphic operator==() and
182 * clone() methods call this method.
184 * @return The class ID for this object. All objects of a
185 * given class have the same class ID. Objects of
186 * other classes have different class IDs.
189 virtual UClassID
getDynamicClassID(void) const;
192 * Returns the class ID for this class. This is useful only for
193 * comparing to a return value from getDynamicClassID(). For example:
195 * Base* polymorphic_pointer = createPolymorphicObject();
196 * if (polymorphic_pointer->getDynamicClassID() ==
197 * Derived::getStaticClassID()) ...
199 * @return The class ID for all objects of this class.
202 static inline UClassID
getStaticClassID(void);
205 //=======================================================================
207 //=======================================================================
209 * This method is the actual implementation of the next() method. All iteration
210 * vectors through here. This method initializes the state machine to state 1
211 * and advances through the text character by character until we reach the end
212 * of the text or the state machine transitions to state 0. We update our return
213 * value every time the state machine passes through a possible end state.
216 virtual int32_t handleNext(void);
219 * removes the cache of break positions (usually in response to a change in
220 * position of some sort)
223 virtual void reset(void);
226 * init Initialize a dbbi. Common routine for use by constructors.
232 * @param stackBuffer user allocated space for the new clone. If NULL new memory will be allocated.
233 * If buffer is not large enough, new memory will be allocated.
234 * @param BufferSize reference to size of allocated space.
235 * If BufferSize == 0, a sufficient size for use in cloning will
236 * be returned ('pre-flighting')
237 * If BufferSize is not enough for a stack-based safe clone,
238 * new memory will be allocated.
239 * @param status to indicate whether the operation went on smoothly or there were errors
240 * An informational status value, U_SAFECLONE_ALLOCATED_ERROR, is used if any allocations were
242 * @return pointer to the new clone
245 virtual BreakIterator
* createBufferClone(void *stackBuffer
,
252 * This is the function that actually implements the dictionary-based
253 * algorithm. Given the endpoints of a range of text, it uses the
254 * dictionary to determine the positions of any boundaries in this
255 * range. It stores all the boundary positions it discovers in
256 * cachedBreakPositions so that we only have to do this work once
257 * for each time we enter the range.
258 * @param startPos The start position of a range of text
259 * @param endPos The end position of a range of text
260 * @param status The error code status
262 void divideUpDictionaryRange(int32_t startPos
, int32_t endPos
, UErrorCode
&status
);
266 * HSYS : Please revisit with Rich, the ctors of the DBBI class is currently
269 friend class DictionaryBasedBreakIteratorTables
;
270 friend class BreakIterator
;
274 DictionaryBasedBreakIterator::getStaticClassID(void)
275 { return (UClassID
)(&fgClassID
); }
278 DictionaryBasedBreakIterator::getDynamicClassID(void) const
279 { return DictionaryBasedBreakIterator::getStaticClassID(); }
283 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */