akmhoque | ba09474 | 2014-02-28 11:47:21 -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 | |||||
9 | namespace nlsr | ||||
10 | { | ||||
11 | |||||
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 12 | using namespace std; |
13 | using namespace boost; | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 14 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [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(); | ||||
21 | for ( ; tok_iter != tokens.end(); ++tok_iter) | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 22 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 23 | string oneToken(*tok_iter); |
24 | trim(oneToken); | ||||
25 | if(!oneToken.empty()) | ||||
26 | { | ||||
27 | insertToken(oneToken); | ||||
28 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 29 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 30 | firstToken=vTokenList[0]; |
31 | makeRestOfTheLine(); | ||||
32 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 33 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 34 | void |
35 | nlsrTokenizer::insertToken(const string& token) | ||||
36 | { | ||||
37 | tokenList.push_back(token); | ||||
38 | vTokenList.push_back(token); | ||||
39 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 40 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 41 | int |
42 | nlsrTokenizer::getTokenPosition(string& token) | ||||
43 | { | ||||
44 | int pos=-1; | ||||
45 | int i=0; | ||||
46 | for(std::list<string>::iterator it=tokenList.begin(); | ||||
47 | it!=tokenList.end(); it++) | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 48 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 49 | if( (*it) == token ) |
50 | { | ||||
51 | break; | ||||
52 | } | ||||
53 | i++; | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 54 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 55 | if( i < tokenList.size() ) |
56 | { | ||||
57 | pos=i; | ||||
58 | } | ||||
59 | return pos; | ||||
60 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 61 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 62 | string |
63 | nlsrTokenizer::getTokenString(int from , int to) | ||||
64 | { | ||||
65 | string returnString=""; | ||||
66 | if((from>=0 && to<tokenList.size()) && | ||||
67 | (to>=from && to <tokenList.size())) | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 68 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 69 | for(int i=from; i<=to; i++) |
70 | { | ||||
71 | returnString+=seps; | ||||
72 | returnString+=vTokenList[i]; | ||||
73 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 74 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 75 | trim(returnString); |
76 | return returnString; | ||||
77 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 78 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 79 | string |
80 | nlsrTokenizer::getTokenString(int from) | ||||
81 | { | ||||
82 | return getTokenString(from,tokenList.size()-1); | ||||
83 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 84 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 85 | static bool |
86 | tokenCompare(string& s1, string& s2) | ||||
87 | { | ||||
88 | return s1==s2; | ||||
89 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 90 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 91 | void |
92 | nlsrTokenizer::makeRestOfTheLine() | ||||
93 | { | ||||
94 | restOfTheLine=getTokenString(1); | ||||
95 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 96 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 97 | bool |
98 | nlsrTokenizer::doesTokenExist(string token) | ||||
99 | { | ||||
100 | std::list<string >::iterator it = std::find_if( tokenList.begin(), | ||||
101 | tokenList.end(), | ||||
102 | bind(&tokenCompare, _1 , token)); | ||||
103 | if( it != tokenList.end() ) | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 104 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 105 | return true; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 106 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 107 | return false; |
108 | } | ||||
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 109 | |
110 | }//namespace nlsr |