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()){ |
| 60 | int i=1; |
| 61 | for(std::list<string>::iterator it=tokenList.begin(); |
| 62 | it!=tokenList.end();it++){ |
| 63 | if( i >= from && i<= to ){ |
| 64 | string oneToken((*it)); |
| 65 | returnString+=seps; |
| 66 | returnString+=oneToken; |
| 67 | |
| 68 | } |
| 69 | i++; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | trim(returnString); |
| 74 | return returnString; |
| 75 | } |
| 76 | |
| 77 | string |
| 78 | nlsrTokenizer::getTokenString(int from){ |
| 79 | string returnString; |
| 80 | if ( from >=0 && from < tokenList.size()){ |
| 81 | int i=1; |
| 82 | for(std::list<string>::iterator it=tokenList.begin(); |
| 83 | it!=tokenList.end();it++){ |
| 84 | if( i >= from){ |
| 85 | string oneToken((*it)); |
| 86 | returnString+=seps; |
| 87 | returnString+=oneToken; |
| 88 | |
| 89 | } |
| 90 | i++; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | trim(returnString); |
| 95 | return returnString; |
| 96 | } |
| 97 | |
| 98 | static bool |
| 99 | tokenCompare(string& s1, string& s2){ |
| 100 | return s1==s2; |
| 101 | } |
| 102 | |
| 103 | bool |
| 104 | nlsrTokenizer::doesTokenExist(string token){ |
| 105 | std::list<string >::iterator it = std::find_if( tokenList.begin(), |
| 106 | tokenList.end(), |
| 107 | bind(&tokenCompare, _1 , token)); |
| 108 | |
| 109 | if( it != tokenList.end() ){ |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |