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