sl@0: /* sl@0: ********************************************************************** sl@0: * Copyright (C) 1999-2005 IBM Corp. All rights reserved. sl@0: ********************************************************************** sl@0: * Date Name Description sl@0: * 12/1/99 rgillam Complete port from Java. sl@0: * 01/13/2000 helena Added UErrorCode to ctors. sl@0: ********************************************************************** sl@0: */ sl@0: sl@0: #ifndef DBBI_H sl@0: #define DBBI_H sl@0: sl@0: #include "unicode/rbbi.h" sl@0: sl@0: #if !UCONFIG_NO_BREAK_ITERATION sl@0: sl@0: /** sl@0: * \file sl@0: * \brief C++ API: Dictionary Based Break Iterator sl@0: */ sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: /* forward declaration */ sl@0: class DictionaryBasedBreakIteratorTables; sl@0: sl@0: /** sl@0: * A subclass of RuleBasedBreakIterator that adds the ability to use a dictionary sl@0: * to further subdivide ranges of text beyond what is possible using just the sl@0: * state-table-based algorithm. This is necessary, for example, to handle sl@0: * word and line breaking in Thai, which doesn't use spaces between words. The sl@0: * state-table-based algorithm used by RuleBasedBreakIterator is used to divide sl@0: * up text as far as possible, and then contiguous ranges of letters are sl@0: * repeatedly compared against a list of known words (i.e., the dictionary) sl@0: * to divide them up into words. sl@0: * sl@0: *

Applications do not normally need to include this header.

sl@0: * sl@0: *

This class will probably be deprecated in a future release of ICU, and replaced sl@0: * with a more flexible and capable dictionary based break iterator. This change sl@0: * should be invisible to applications, because creation and use of instances of sl@0: * DictionaryBasedBreakIterator is through the factories and abstract sl@0: * API on class BreakIterator, which will remain stable.

sl@0: * sl@0: *

This class is not intended to be subclassed.

sl@0: * sl@0: * sl@0: * DictionaryBasedBreakIterator uses the same rule language as RuleBasedBreakIterator, sl@0: * but adds one more special substitution name: <dictionary>. This substitution sl@0: * name is used to identify characters in words in the dictionary. The idea is that sl@0: * if the iterator passes over a chunk of text that includes two or more characters sl@0: * in a row that are included in <dictionary>, it goes back through that range and sl@0: * derives additional break positions (if possible) using the dictionary. sl@0: * sl@0: * DictionaryBasedBreakIterator is also constructed with the filename of a dictionary sl@0: * file. It follows a prescribed search path to locate the dictionary (right now, sl@0: * it looks for it in /com/ibm/text/resources in each directory in the classpath, sl@0: * and won't find it in JAR files, but this location is likely to change). The sl@0: * dictionary file is in a serialized binary format. We have a very primitive (and sl@0: * slow) BuildDictionaryFile utility for creating dictionary files, but aren't sl@0: * currently making it public. Contact us for help. sl@0: *

sl@0: * NOTE The DictionaryBasedIterator class is still under development. The sl@0: * APIs are not in stable condition yet. sl@0: */ sl@0: class U_COMMON_API DictionaryBasedBreakIterator : public RuleBasedBreakIterator { sl@0: sl@0: private: sl@0: sl@0: /** sl@0: * when a range of characters is divided up using the dictionary, the break sl@0: * positions that are discovered are stored here, preventing us from having sl@0: * to use either the dictionary or the state table again until the iterator sl@0: * leaves this range of text sl@0: */ sl@0: int32_t* cachedBreakPositions; sl@0: sl@0: /** sl@0: * The number of elements in cachedBreakPositions sl@0: */ sl@0: int32_t numCachedBreakPositions; sl@0: sl@0: /** sl@0: * if cachedBreakPositions is not null, this indicates which item in the sl@0: * cache the current iteration position refers to sl@0: */ sl@0: int32_t positionInCache; sl@0: sl@0: DictionaryBasedBreakIteratorTables *fTables; sl@0: sl@0: /**======================================================================= sl@0: * Create a dictionary based break boundary detection iterator. sl@0: * @param tablesImage The location for the dictionary to be loaded into memory sl@0: * @param dictionaryFilename The name of the dictionary file sl@0: * @param status the error code status sl@0: * @return A dictionary based break detection iterator. The UErrorCode& status sl@0: * parameter is used to return status information to the user. sl@0: * To check whether the construction succeeded or not, you should check sl@0: * the value of U_SUCCESS(err). If you wish more detailed information, you sl@0: * can check for informational error results which still indicate success. For example, sl@0: * U_FILE_ACCESS_ERROR will be returned if the file does not exist. sl@0: * The caller owns the returned object and is responsible for deleting it. sl@0: ======================================================================= */ sl@0: DictionaryBasedBreakIterator(UDataMemory* tablesImage, const char* dictionaryFilename, UErrorCode& status); sl@0: sl@0: public: sl@0: //======================================================================= sl@0: // boilerplate sl@0: //======================================================================= sl@0: sl@0: /** sl@0: * Destructor sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual ~DictionaryBasedBreakIterator(); sl@0: sl@0: /** sl@0: * Default constructor. Creates an "empty" break iterator. sl@0: * Such an iterator can subsequently be assigned to. sl@0: * @return the newly created DictionaryBaseBreakIterator. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: DictionaryBasedBreakIterator(); sl@0: sl@0: /** sl@0: * Copy constructor. sl@0: * @param other The DictionaryBasedBreakIterator to be copied. sl@0: * @return the newly created DictionaryBasedBreakIterator. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: DictionaryBasedBreakIterator(const DictionaryBasedBreakIterator &other); sl@0: sl@0: /** sl@0: * Assignment operator. sl@0: * @param that The object to be copied. sl@0: * @return the newly set DictionaryBasedBreakIterator. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: DictionaryBasedBreakIterator& operator=(const DictionaryBasedBreakIterator& that); sl@0: sl@0: /** sl@0: * Returns a newly-constructed RuleBasedBreakIterator with the same sl@0: * behavior, and iterating over the same text, as this one. sl@0: * @return Returns a newly-constructed RuleBasedBreakIterator. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual BreakIterator* clone(void) const; sl@0: sl@0: //======================================================================= sl@0: // BreakIterator overrides sl@0: //======================================================================= sl@0: /** sl@0: * Advances the iterator backwards, to the last boundary preceding this one. sl@0: * @return The position of the last boundary position preceding this one. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t previous(void); sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the first boundary position following sl@0: * the specified position. sl@0: * @param offset The position from which to begin searching for a break position. sl@0: * @return The position of the first break after the current position. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t following(int32_t offset); sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the last boundary position before the sl@0: * specified position. sl@0: * @param offset The position to begin searching for a break from. sl@0: * @return The position of the last boundary before the starting position. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t preceding(int32_t offset); sl@0: sl@0: /** sl@0: * Returns the class ID for this class. This is useful only for sl@0: * comparing to a return value from getDynamicClassID(). For example: sl@0: * sl@0: * Base* polymorphic_pointer = createPolymorphicObject(); sl@0: * if (polymorphic_pointer->getDynamicClassID() == sl@0: * Derived::getStaticClassID()) ... sl@0: * sl@0: * @return The class ID for all objects of this class. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: static UClassID U_EXPORT2 getStaticClassID(void); sl@0: sl@0: /** sl@0: * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. sl@0: * This method is to implement a simple version of RTTI, since not all sl@0: * C++ compilers support genuine RTTI. Polymorphic operator==() and sl@0: * clone() methods call this method. sl@0: * sl@0: * @return The class ID for this object. All objects of a sl@0: * given class have the same class ID. Objects of sl@0: * other classes have different class IDs. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UClassID getDynamicClassID(void) const; sl@0: sl@0: protected: sl@0: //======================================================================= sl@0: // implementation sl@0: //======================================================================= sl@0: /** sl@0: * This method is the actual implementation of the next() method. All iteration sl@0: * vectors through here. This method initializes the state machine to state 1 sl@0: * and advances through the text character by character until we reach the end sl@0: * of the text or the state machine transitions to state 0. We update our return sl@0: * value every time the state machine passes through a possible end state. sl@0: * @internal sl@0: */ sl@0: virtual int32_t handleNext(void); sl@0: sl@0: /** sl@0: * removes the cache of break positions (usually in response to a change in sl@0: * position of some sort) sl@0: * @internal sl@0: */ sl@0: virtual void reset(void); sl@0: sl@0: /** sl@0: * init Initialize a dbbi. Common routine for use by constructors. sl@0: * @internal sl@0: */ sl@0: void init(); sl@0: sl@0: /** sl@0: * @param stackBuffer user allocated space for the new clone. If NULL new memory will be allocated. sl@0: * If buffer is not large enough, new memory will be allocated. sl@0: * @param BufferSize reference to size of allocated space. sl@0: * If BufferSize == 0, a sufficient size for use in cloning will sl@0: * be returned ('pre-flighting') sl@0: * If BufferSize is not enough for a stack-based safe clone, sl@0: * new memory will be allocated. sl@0: * @param status to indicate whether the operation went on smoothly or there were errors sl@0: * An informational status value, U_SAFECLONE_ALLOCATED_ERROR, is used if any allocations were sl@0: * necessary. sl@0: * @return pointer to the new clone sl@0: * @internal sl@0: */ sl@0: virtual BreakIterator * createBufferClone(void *stackBuffer, sl@0: int32_t &BufferSize, sl@0: UErrorCode &status); sl@0: sl@0: sl@0: private: sl@0: /** sl@0: * This is the function that actually implements the dictionary-based sl@0: * algorithm. Given the endpoints of a range of text, it uses the sl@0: * dictionary to determine the positions of any boundaries in this sl@0: * range. It stores all the boundary positions it discovers in sl@0: * cachedBreakPositions so that we only have to do this work once sl@0: * for each time we enter the range. sl@0: * @param startPos The start position of a range of text sl@0: * @param endPos The end position of a range of text sl@0: * @param status The error code status sl@0: */ sl@0: void divideUpDictionaryRange(int32_t startPos, int32_t endPos, UErrorCode &status); sl@0: sl@0: sl@0: /* sl@0: * HSYS : Please revisit with Rich, the ctors of the DBBI class is currently sl@0: * marked as private. sl@0: */ sl@0: friend class DictionaryBasedBreakIteratorTables; sl@0: friend class BreakIterator; sl@0: }; sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ sl@0: sl@0: #endif