akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 1 | #include <iostream> |
2 | #include <boost/tokenizer.hpp> | ||||
3 | #include <boost/algorithm/string.hpp> | ||||
4 | #include <string> | ||||
5 | #include <algorithm> | ||||
6 | |||||
7 | #include "nlsr_tokenizer.hpp" | ||||
8 | |||||
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 9 | namespace nlsr |
10 | { | ||||
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 11 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 12 | using namespace std; |
13 | using namespace boost; | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 14 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 15 | void |
16 | nlsrTokenizer::makeToken() | ||||
17 | { | ||||
18 | char_separator<char> sep(seps.c_str()); | ||||
19 | tokenizer< char_separator<char> >tokens(originalString, sep); | ||||
20 | tokenizer< char_separator<char> >::iterator tok_iter = tokens.begin(); | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 21 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 22 | string ft(*tok_iter); |
23 | firstToken=ft; | ||||
24 | ++tok_iter; | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 25 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 26 | for ( ; tok_iter != tokens.end(); ++tok_iter) |
27 | { | ||||
28 | string oneToken(*tok_iter); | ||||
29 | this->insertToken(oneToken); | ||||
30 | restOfTheLine+=oneToken; | ||||
31 | restOfTheLine+=seps; | ||||
32 | } | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 33 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 34 | trim(restOfTheLine); |
35 | } | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 36 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 37 | void |
38 | nlsrTokenizer::insertToken(const string& token) | ||||
39 | { | ||||
40 | tokenList.push_back(token); | ||||
41 | } | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 42 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 43 | int |
44 | nlsrTokenizer::getTokenPosition(string& token) | ||||
45 | { | ||||
46 | int pos=-1; | ||||
47 | int i=1; | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 48 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 49 | for(std::list<string>::iterator it=tokenList.begin(); it!=tokenList.end(); it++) |
50 | { | ||||
51 | if( (*it) == token ) | ||||
52 | { | ||||
53 | break; | ||||
54 | } | ||||
55 | i++; | ||||
56 | } | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 57 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 58 | if( i < tokenList.size() ) |
59 | { | ||||
60 | pos=i; | ||||
61 | } | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 62 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 63 | return pos; |
64 | } | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 65 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 66 | string |
67 | nlsrTokenizer::getTokenString(int from , int to) | ||||
68 | { | ||||
69 | string returnString; | ||||
70 | if ( from >=0 && to < tokenList.size()) | ||||
71 | { | ||||
72 | int i=0; | ||||
73 | for(std::list<string>::iterator it=tokenList.begin(); | ||||
74 | it!=tokenList.end(); it++) | ||||
75 | { | ||||
76 | i++; | ||||
77 | if( i >= from && i<= to ) | ||||
78 | { | ||||
79 | string oneToken((*it)); | ||||
80 | returnString+=seps; | ||||
81 | returnString+=oneToken; | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 82 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 83 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 84 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 85 | } |
86 | } | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 87 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 88 | trim(returnString); |
89 | return returnString; | ||||
90 | } | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 91 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 92 | string |
93 | nlsrTokenizer::getTokenString(int from) | ||||
94 | { | ||||
95 | string returnString; | ||||
96 | if ( from >=0 && from < tokenList.size()) | ||||
97 | { | ||||
98 | int i=0; | ||||
99 | for(std::list<string>::iterator it=tokenList.begin(); | ||||
100 | it!=tokenList.end(); it++) | ||||
101 | { | ||||
102 | i++; | ||||
103 | if( i >= from) | ||||
104 | { | ||||
105 | string oneToken((*it)); | ||||
106 | returnString+=seps; | ||||
107 | returnString+=oneToken; | ||||
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 108 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 109 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 110 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 111 | } |
112 | } | ||||
113 | |||||
114 | trim(returnString); | ||||
115 | return returnString; | ||||
116 | } | ||||
117 | |||||
118 | static bool | ||||
119 | tokenCompare(string& s1, string& s2) | ||||
120 | { | ||||
121 | return s1==s2; | ||||
122 | } | ||||
123 | |||||
124 | bool | ||||
125 | nlsrTokenizer::doesTokenExist(string token) | ||||
126 | { | ||||
127 | std::list<string >::iterator it = std::find_if( tokenList.begin(), | ||||
128 | tokenList.end(), | ||||
129 | bind(&tokenCompare, _1 , token)); | ||||
130 | |||||
131 | if( it != tokenList.end() ) | ||||
132 | { | ||||
133 | return true; | ||||
134 | } | ||||
135 | |||||
136 | return false; | ||||
137 | } | ||||
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 138 | |
139 | }//namespace nlsr |