File indexing completed on 2024-04-28 15:34:19

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2006 Jacob R Rideout <kde@jacobrideout.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef TEXTBREAKS_H
0008 #define TEXTBREAKS_H
0009 
0010 class QString;
0011 
0012 #include "sonnetcore_export.h"
0013 
0014 namespace Sonnet
0015 {
0016 class TextBreaksPrivate;
0017 
0018 /**
0019  * @short TextBreaks determines the barriers between linguistic structures in any given text.
0020  *
0021  * TextBreaks is a class that determines the boundaries between graphemes
0022  * (characters as per the unicode definition,) words and sentences. The
0023  * default implementation conforms to Unicode Standard Annex #29 https://unicode.org/reports/tr29/.
0024  * You can subclass TextBreaks to create the correct behaviour for languages that require it.
0025  *
0026  * @author Jacob Rideout <kde@jacobrideout.net>
0027  * @since 4.3
0028  */
0029 class SONNETCORE_EXPORT TextBreaks
0030 {
0031 public:
0032     struct Position {
0033         int start, length;
0034     };
0035 
0036     /**
0037      * This structure abstracts the positions of breaks in the test. As per the
0038      * unicode annex, both the start and end of the text are returned.
0039      */
0040     typedef QList<Position> Positions;
0041 
0042     /** Constructor
0043      * Creates a new TextBreaks instance. If @p text is specified,
0044      * it sets the text to be checked.
0045      * @param text the text that is to be checked
0046      */
0047     explicit TextBreaks(const QString &text = QString());
0048 
0049     /** Virtual Destructor
0050      */
0051     virtual ~TextBreaks();
0052 
0053     /**
0054      * Returns the text to be checked
0055      * @return text
0056      */
0057     QString text() const;
0058 
0059     /**
0060      * Sets the text to @p text
0061      * @param text to be set
0062      * @return true if the word is misspelled. false otherwise
0063      */
0064     void setText(const QString &text);
0065 
0066     /**
0067      * Return the Positions of each word for the given  @p text.
0068      * @param text to be checked
0069      * @return positions of breaks
0070      */
0071     static Positions wordBreaks(const QString &text);
0072 
0073     /**
0074      * Return the Positions of each sentence for the given  @p text.
0075      * @param text to be checked
0076      * @return positions of breaks
0077      */
0078     static Positions sentenceBreaks(const QString &text);
0079 
0080     /**
0081      * Return the Positions of each word for the text previously set.
0082      * @return positions of breaks
0083      */
0084     virtual Positions wordBreaks() const;
0085 
0086     /**
0087      * Return the Positions of each sentence for the text previously set.
0088      * @return positions of breaks
0089      */
0090     virtual Positions sentenceBreaks() const;
0091 
0092 private:
0093     TextBreaksPrivate *const d;
0094 };
0095 }
0096 
0097 Q_DECLARE_TYPEINFO(Sonnet::TextBreaks::Position, Q_PRIMITIVE_TYPE);
0098 
0099 #endif