00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef Tanl_PtbTokenizer_H
00025 #define Tanl_PtbTokenizer_H
00026
00027 #ifndef yyFlexLexer
00028
00029 # define yyFlexLexer ptbFlexLexer
00030 # include "FlexLexer.h"
00031 #endif
00032
00033
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <cstdio>
00037 #include <fstream>
00038
00039
00040 #include "Enumerator.h"
00041
00042 using namespace std;
00043
00044 namespace Tanl {
00045
00046 class PtbScanner : public yyFlexLexer
00047 {
00048
00049 public:
00050
00052 enum TokenType {
00053 Eof = 0,
00054 Word = 200,
00055 Abbrev,
00056 Date,
00057 Email,
00058 None,
00059 Number,
00060 Phone,
00061 Special,
00062 Tag,
00063 Url,
00064 Punct,
00065 Eos
00066 };
00067
00071 struct Token {
00072 Token(char const* text = "", TokenType type = Eof) :
00073 text(strdup(text)), type(type) { }
00074
00075 Token(std::string const& text, TokenType type = Eof) :
00076 text(strdup(text.c_str())), type(type) { }
00077
00078 char const* text;
00079 TokenType type;
00080
00081 Token const& operator =(Token const& o) {
00082 if (this != &o) {
00083 delete text;
00084 text = strdup(o.text);
00085 type = o.type;
00086 }
00087 return *this;
00088 }
00089
00090 ~Token() { free((void*)text); }
00091
00092 };
00093
00098 Token scan();
00099 };
00100
00101 std::ostream& operator <<(std::ostream& os, PtbScanner::Token const& tk) {
00102 return os << tk.text;
00103 }
00104
00105 #undef YY_DECL
00106 #define YY_DECL Tanl::PtbScanner::Token Tanl::PtbScanner::scan()
00107 #define yylval token.text
00108
00115 class PtbTokenizer : public Tanl::Enumerator<PtbScanner::Token const*>
00116 {
00117 public:
00118
00123 PtbTokenizer(std::istream* is, char const* lang = 0) :
00124 stream(*is)
00125 {
00126 scanner.switch_streams(&stream, 0);
00127 }
00128
00130 PtbScanner::Token const* Current() { return &token; }
00131
00133 bool MoveNext() {
00134 token = scanner.scan();
00135 return token.type != PtbScanner::Eof;
00136 }
00137
00139 void Reset() {
00140 stream.seekg(0);
00141 scanner.switch_streams(&stream, 0);
00142 }
00143
00144 private:
00145 PtbScanner scanner;
00146 PtbScanner::Token token;
00147 std::istream& stream;
00148 };
00149
00150 }
00151
00152 #endif // Tanl_PtbTokenizer_H