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 | |
| 12 | using namespace std; |
| 13 | using namespace boost; |
| 14 | |
| 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) |
| 22 | { |
| 23 | string oneToken(*tok_iter); |
| 24 | trim(oneToken); |
| 25 | if(!oneToken.empty()) |
| 26 | { |
| 27 | insertToken(oneToken); |
| 28 | } |
| 29 | } |
| 30 | firstToken=vTokenList[0]; |
| 31 | makeRestOfTheLine(); |
| 32 | } |
| 33 | |
| 34 | void |
| 35 | nlsrTokenizer::insertToken(const string& token) |
| 36 | { |
| 37 | tokenList.push_back(token); |
| 38 | vTokenList.push_back(token); |
| 39 | } |
| 40 | |
| 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++) |
| 48 | { |
| 49 | if( (*it) == token ) |
| 50 | { |
| 51 | break; |
| 52 | } |
| 53 | i++; |
| 54 | } |
| 55 | if( i < tokenList.size() ) |
| 56 | { |
| 57 | pos=i; |
| 58 | } |
| 59 | return pos; |
| 60 | } |
| 61 | |
| 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())) |
| 68 | { |
| 69 | for(int i=from; i<=to; i++) |
| 70 | { |
| 71 | returnString+=seps; |
| 72 | returnString+=vTokenList[i]; |
| 73 | } |
| 74 | } |
| 75 | trim(returnString); |
| 76 | return returnString; |
| 77 | } |
| 78 | |
| 79 | string |
| 80 | nlsrTokenizer::getTokenString(int from) |
| 81 | { |
| 82 | return getTokenString(from,tokenList.size()-1); |
| 83 | } |
| 84 | |
| 85 | static bool |
| 86 | tokenCompare(string& s1, string& s2) |
| 87 | { |
| 88 | return s1==s2; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | nlsrTokenizer::makeRestOfTheLine() |
| 93 | { |
| 94 | restOfTheLine=getTokenString(1); |
| 95 | } |
| 96 | |
| 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() ) |
| 104 | { |
| 105 | return true; |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | }//namespace nlsr |