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