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