blob: 3c49f8c4e64b0cde4461ff48da1a31c37b1b68f3 [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#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
akmhoque1fd8c1e2014-02-19 19:41:49 -06009namespace nlsr
10{
akmhoqueb1710aa2014-02-19 17:13:36 -060011
akmhoque1fd8c1e2014-02-19 19:41:49 -060012 using namespace std;
13 using namespace boost;
akmhoque298385a2014-02-13 14:13:09 -060014
akmhoque1fd8c1e2014-02-19 19:41:49 -060015 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();
akmhoque298385a2014-02-13 14:13:09 -060021
akmhoque1fd8c1e2014-02-19 19:41:49 -060022 string ft(*tok_iter);
23 firstToken=ft;
24 ++tok_iter;
akmhoque298385a2014-02-13 14:13:09 -060025
akmhoque1fd8c1e2014-02-19 19:41:49 -060026 for ( ; tok_iter != tokens.end(); ++tok_iter)
27 {
28 string oneToken(*tok_iter);
29 this->insertToken(oneToken);
30 restOfTheLine+=oneToken;
31 restOfTheLine+=seps;
32 }
akmhoque298385a2014-02-13 14:13:09 -060033
akmhoque1fd8c1e2014-02-19 19:41:49 -060034 trim(restOfTheLine);
35 }
akmhoque298385a2014-02-13 14:13:09 -060036
akmhoque1fd8c1e2014-02-19 19:41:49 -060037 void
38 nlsrTokenizer::insertToken(const string& token)
39 {
40 tokenList.push_back(token);
41 }
akmhoque298385a2014-02-13 14:13:09 -060042
akmhoque1fd8c1e2014-02-19 19:41:49 -060043 int
44 nlsrTokenizer::getTokenPosition(string& token)
45 {
46 int pos=-1;
47 int i=1;
akmhoque298385a2014-02-13 14:13:09 -060048
akmhoque1fd8c1e2014-02-19 19:41:49 -060049 for(std::list<string>::iterator it=tokenList.begin(); it!=tokenList.end(); it++)
50 {
51 if( (*it) == token )
52 {
53 break;
54 }
55 i++;
56 }
akmhoque298385a2014-02-13 14:13:09 -060057
akmhoque1fd8c1e2014-02-19 19:41:49 -060058 if( i < tokenList.size() )
59 {
60 pos=i;
61 }
akmhoque298385a2014-02-13 14:13:09 -060062
akmhoque1fd8c1e2014-02-19 19:41:49 -060063 return pos;
64 }
akmhoque298385a2014-02-13 14:13:09 -060065
akmhoque1fd8c1e2014-02-19 19:41:49 -060066 string
67 nlsrTokenizer::getTokenString(int from , int to)
68 {
69 string returnString;
70 if ( from >=0 && to < tokenList.size())
71 {
72 int i=0;
73 for(std::list<string>::iterator it=tokenList.begin();
74 it!=tokenList.end(); it++)
75 {
76 i++;
77 if( i >= from && i<= to )
78 {
79 string oneToken((*it));
80 returnString+=seps;
81 returnString+=oneToken;
akmhoque298385a2014-02-13 14:13:09 -060082
akmhoque1fd8c1e2014-02-19 19:41:49 -060083 }
akmhoque298385a2014-02-13 14:13:09 -060084
akmhoque1fd8c1e2014-02-19 19:41:49 -060085 }
86 }
akmhoque298385a2014-02-13 14:13:09 -060087
akmhoque1fd8c1e2014-02-19 19:41:49 -060088 trim(returnString);
89 return returnString;
90 }
akmhoque298385a2014-02-13 14:13:09 -060091
akmhoque1fd8c1e2014-02-19 19:41:49 -060092 string
93 nlsrTokenizer::getTokenString(int from)
94 {
95 string returnString;
96 if ( from >=0 && from < tokenList.size())
97 {
98 int i=0;
99 for(std::list<string>::iterator it=tokenList.begin();
100 it!=tokenList.end(); it++)
101 {
102 i++;
103 if( i >= from)
104 {
105 string oneToken((*it));
106 returnString+=seps;
107 returnString+=oneToken;
akmhoque298385a2014-02-13 14:13:09 -0600108
akmhoque1fd8c1e2014-02-19 19:41:49 -0600109 }
akmhoque298385a2014-02-13 14:13:09 -0600110
akmhoque1fd8c1e2014-02-19 19:41:49 -0600111 }
112 }
113
114 trim(returnString);
115 return returnString;
116 }
117
118 static bool
119 tokenCompare(string& s1, string& s2)
120 {
121 return s1==s2;
122 }
123
124 bool
125 nlsrTokenizer::doesTokenExist(string token)
126 {
127 std::list<string >::iterator it = std::find_if( tokenList.begin(),
128 tokenList.end(),
129 bind(&tokenCompare, _1 , token));
130
131 if( it != tokenList.end() )
132 {
133 return true;
134 }
135
136 return false;
137 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600138
139}//namespace nlsr