/usr/include/ucto/tokenize.h is in libucto-dev 0.5.3-3.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | /*
$Id: tokenize.h 15910 2013-04-03 13:57:51Z sloot $
$URL: https://ilk.uvt.nl/svn/sources/ucto/trunk/include/ucto/tokenize.h $
Copyright (c) 2006 - 2013
Tilburg University
This file is part of Ucto.
Ucto is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Ucto is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
For more information and updates, see:
http://ilk.uvt.nl/frog
*/
#ifndef UCTO_TOKENIZE_H
#define UCTO_TOKENIZE_H
#include <vector>
#include <map>
#include <sstream>
#include <stdexcept>
#include "config.h"
#include "ucto/unicode.h"
#include "ticcutils/LogStream.h"
namespace Tokenizer {
std::string Version();
std::string VersionName();
//enum RuleTrigger { PUNCTUATION, PERIOD, NUMBER }; //TODO: implement
enum TokenRole {
NOROLE = 0,
NOSPACE = 1,
BEGINOFSENTENCE = 2,
ENDOFSENTENCE = 4,
NEWPARAGRAPH = 8,
BEGINQUOTE = 16,
ENDQUOTE = 32,
TEMPENDOFSENTENCE = 64,
LISTITEM = 128, //reserved for future use
TITLE = 256 //reserved for future use
};
std::ostream& operator<<( std::ostream&, const TokenRole& );
inline TokenRole operator|( TokenRole T1, TokenRole T2 ){
return (TokenRole)( (int)T1|(int)T2 );
}
inline TokenRole& operator|= ( TokenRole& T1, TokenRole T2 ){
T1 = (T1 | T2);
return T1;
}
inline TokenRole operator^( TokenRole T1, TokenRole T2 ){
return (TokenRole)( (int)T1^(int)T2 );
}
inline TokenRole& operator^= ( TokenRole& T1, TokenRole T2 ){
T1 = (T1 ^ T2);
return T1;
}
class Token {
friend std::ostream& operator<< (std::ostream&, const Token& );
public:
const UnicodeString *type;
UnicodeString us;
TokenRole role;
Token( const UnicodeString *,
const UnicodeString& s,
TokenRole role = NOROLE );
};
class UnicodeRegexMatcher;
class Rule {
friend std::ostream& operator<< (std::ostream&, const Rule& );
public:
Rule(): regexp(0){
};
Rule( const UnicodeString& id, const UnicodeString& pattern);
~Rule();
UnicodeString id;
UnicodeString pattern;
UnicodeRegexMatcher *regexp;
bool matchAll( const UnicodeString&,
UnicodeString&,
UnicodeString&,
std::vector<UnicodeString>& );
};
class Quoting {
friend std::ostream& operator<<( std::ostream&, const Quoting& );
struct QuotePair {
UnicodeString openQuote;
UnicodeString closeQuote;
};
public:
void add( const UnicodeString&, const UnicodeString& );
UnicodeString lookupOpen( const UnicodeString &) const;
UnicodeString lookupClose( const UnicodeString & ) const;
bool empty() const { return quotes.empty(); };
bool emptyStack() const { return quotestack.empty(); };
void clearStack() { quoteindexstack.clear(); quotestack.clear(); };
int lookup( const UnicodeString&, int& );
void eraseAtPos( int pos ) {
quotestack.erase( quotestack.begin()+pos );
quoteindexstack.erase( quoteindexstack.begin()+pos );
}
void flushStack( int ); //renamed from eraseBeforeIndex
void push( int i, UChar c ){
quoteindexstack.push_back(i);
quotestack.push_back(c);
}
private:
std::vector<QuotePair> quotes;
std::vector<int> quoteindexstack;
std::vector<UChar> quotestack;
};
class TokenizerClass{
public:
TokenizerClass();
~TokenizerClass();
bool init( const std::string& );
void setErrorLog( TiCC::LogStream *os );
// Tokenize from input stream to FoLiA document
folia::Document tokenize( std::istream& );
// Tokenize a folia document
bool tokenize(folia::Document& );
//Tokenize from input stream to output stream
std::vector<Token> tokenizeStream( std::istream& );
void tokenize( std::istream&, std::ostream& );
void tokenize( std::istream* in, std::ostream* out){
// for backward compatability
return tokenize( *in, *out );};
// Tokenize a line (a line is NOT a sentence, but an arbitrary string
// of characters, inclusive EOS markers, Newlines etc.)
int tokenizeLine( const UnicodeString& ); // Unicode chars
int tokenizeLine( const std::string& ); // UTF8 chars
void passthruLine( const std::string&, bool& );
//Processes tokens and initialises the sentence buffer. Returns the amount of sentences found
int countSentences(bool forceentirebuffer = false); //count the number of sentences (only after detectSentenceBounds) (does some extra validation as well)
int flushSentences(const int); //Flush n sentences from buffer (does some extra validation as well)
//Get the sentence with the specified index as a string (UTF-8 encoded)
std::string getSentenceString( unsigned int );
//Get all sentences as a vector of strings (UTF-8 encoded)
std::vector<std::string> getSentences();
//Enable verbose mode
bool setVerbose( bool b=true ) { bool t = verbose; verbose = b; return t; };
bool getVerbose() const { return verbose; }
//set debug value
int setDebug( int d ) { bool dd = tokDebug; tokDebug = d; return dd; };
int getDebug() const { return tokDebug; }
//Enable conversion of all output to lowercase
bool setLowercase( bool b=true ) { bool t = lowercase; lowercase = b; if (b) uppercase = false; return t; };
bool getLowercase() const { return lowercase; }
//Enable passtru mode
bool setPassThru( bool b=true ) { bool t = passthru; passthru = b; return t; };
bool getPassThru() const { return passthru; }
//Enable conversion of all output to uppercase
bool setUppercase( bool b=true ) { bool t = uppercase; uppercase = b; if (b) lowercase = false; return t; };
bool getUppercase() const { return uppercase; }
//Enable sentence-bound detection
bool setSentenceDetection( bool b=true ) { bool t = detectBounds; detectBounds = b; return t; }
bool getSentenceDetection() const { return detectBounds; }
//Enable paragraph detection
bool setParagraphDetection( bool b=true ) { bool t = detectPar; detectPar = b; return t; }
bool getParagraphDetection() const { return detectPar; }
//Enable quote detection
bool setQuoteDetection( bool b=true ) { bool t = detectQuotes; detectQuotes = b; return t; }
bool getQuoteDetection() const { return detectQuotes; }
//Enable filtering
bool setFiltering( bool b=true ) { bool t = doFilter; doFilter = b; return t; }
bool getFiltering() const { return doFilter; };
// set normalization mode
std::string setNormalization( const std::string& s ) {
return normalizer.setMode( s );
}
std::string getNormalization() const { return normalizer.getMode(); };
// set input encoding
std::string setInputEncoding( const std::string& );
std::string getInputEncoding() const { return inputEncoding; };
// set eos marker
UnicodeString setEosMarker( const std::string& s = "<utt>") { UnicodeString t = eosmark; eosmark = folia::UTF8ToUnicode(s); return t; };
UnicodeString getEosMarker( ) const { return eosmark; }
bool setSentencePerLineOutput( bool b=true ) { bool t = sentenceperlineoutput; sentenceperlineoutput = b; return t; };
bool getSentencePerLineOutput() const { return sentenceperlineoutput; }
bool setSentencePerLineInput( bool b=true ) { bool t = sentenceperlineinput; sentenceperlineinput = b; return t; };
bool getSentencePerLineInput() const { return sentenceperlineinput; }
std::string getDocID() const { return docid; }
bool getXMLOutput() const { return xmlout; }
bool getXMLInput() const { return xmlin; }
const std::string getTextClass( ) const { return textclass; }
const std::string setTextClass( const std::string& cls) {
std::string res = textclass;
textclass = cls;
return res;
}
bool setXMLOutput( bool b, const std::string& id) { bool t = xmlout; docid = id; xmlout = b; return t; }
bool setXMLInput( bool b ) { bool t = xmlin; xmlin = b; return t; }
void outputTokens( std::ostream&, const std::vector<Token>& ) const;
private:
void tokenizeWord( const UnicodeString&, bool);
bool detectEos( size_t ) const;
void detectSentenceBounds( const int offset = 0 );
void detectQuotedSentenceBounds( const int offset = 0 );
void detectQuoteBounds( const int );
//Signal the tokeniser that a paragraph is detected
void signalParagraph( bool b=true ) { paragraphsignal = b; };
bool resolveQuote( int, const UnicodeString& );
bool u_isquote( UChar );
std::string checkBOM( const std::string&, std::string& );
bool readsettings( const std::string& );
bool readrules( const std::string& );
bool readfilters( const std::string& );
bool readquotes( const std::string& );
bool readeosmarkers( const std::string& );
bool readabbreviations( const std::string&, UnicodeString& );
void sortRules( std::vector<Rule *>&, std::vector<UnicodeString>& );
void outputTokensDoc( folia::Document&, const std::vector<Token>& ) const;
void outputTokensXML( folia::FoliaElement *, const std::vector<Token>& ) const;
void tokenizeElement( folia::FoliaElement * );
void tokenizeSentenceElement( folia::FoliaElement * );
//return the sentence with the specified index in a Token vector;
std::vector<Token> getSentence( int );
Quoting quotes;
UnicodeFilter filter;
UnicodeNormalizer normalizer;
UnicodeString eosmarkers;
std::string inputEncoding;
UnicodeString eosmark;
std::vector<Token> tokens;
std::vector<Rule *> rules;
TiCC::LogStream *theErrLog;
//debug flag
int tokDebug;
//verbose tokenisation mode
bool verbose;
//detect sentence bounds?
bool detectBounds;
//detect quotes?
bool detectQuotes;
//filter special characters (default on)?
bool doFilter;
//detect paragraphs?
bool detectPar;
//has a paragraph been signaled?
bool paragraphsignal;
//one sentence per line output
bool sentenceperlineoutput;
bool sentenceperlineinput;
bool lowercase;
bool uppercase;
bool xmlout;
bool xmlin;
bool passthru;
std::string settingsfilename;
std::string docid; //document ID (UTF-8), necessary for XML output
std::string textclass; // class for folia text
};
template< typename T >
T stringTo( const std::string& str ) {
T result;
std::stringstream dummy ( str );
if ( !( dummy >> result ) ) {
throw( std::runtime_error( "conversion from '" + str + "' failed" ) );
}
return result;
}
template< typename T >
std::string toString( const T val ) {
std::stringstream dummy;
if ( !( dummy << val ) ) {
throw( std::runtime_error( "conversion failed" ) );
}
return dummy.str();
}
}
#endif
|