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