blob: e03d2d2959c2c46f18f28a145b1a45daaa124cb4 [file] [log] [blame]
akmhoque87347a32014-01-31 11:00:44 -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
9using namespace std;
10using namespace boost;
11
12void
13nlsrTokenizer::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
32void
33nlsrTokenizer::insertToken(const string& token){
34 tokenList.push_back(token);
35}
36
37int
38nlsrTokenizer::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
56string
57nlsrTokenizer::getTokenString(int from , int to){
58 string returnString;
59 if ( from >=0 && to < tokenList.size()){
akmhoquea8cd6b92014-01-31 20:13:26 -060060 int i=0;
akmhoque87347a32014-01-31 11:00:44 -060061 for(std::list<string>::iterator it=tokenList.begin();
62 it!=tokenList.end();it++){
akmhoquea8cd6b92014-01-31 20:13:26 -060063 i++;
akmhoque87347a32014-01-31 11:00:44 -060064 if( i >= from && i<= to ){
65 string oneToken((*it));
66 returnString+=seps;
67 returnString+=oneToken;
68
69 }
akmhoquea8cd6b92014-01-31 20:13:26 -060070
akmhoque87347a32014-01-31 11:00:44 -060071 }
72 }
73
74 trim(returnString);
75 return returnString;
76}
77
78string
79nlsrTokenizer::getTokenString(int from){
80 string returnString;
81 if ( from >=0 && from < tokenList.size()){
akmhoquea8cd6b92014-01-31 20:13:26 -060082 int i=0;
akmhoque87347a32014-01-31 11:00:44 -060083 for(std::list<string>::iterator it=tokenList.begin();
84 it!=tokenList.end();it++){
akmhoquea8cd6b92014-01-31 20:13:26 -060085 i++;
akmhoque87347a32014-01-31 11:00:44 -060086 if( i >= from){
87 string oneToken((*it));
88 returnString+=seps;
89 returnString+=oneToken;
90
91 }
akmhoquea8cd6b92014-01-31 20:13:26 -060092
akmhoque87347a32014-01-31 11:00:44 -060093 }
94 }
95
96 trim(returnString);
97 return returnString;
98}
99
100static bool
101tokenCompare(string& s1, string& s2){
102 return s1==s2;
103}
104
105bool
106nlsrTokenizer::doesTokenExist(string token){
107 std::list<string >::iterator it = std::find_if( tokenList.begin(),
108 tokenList.end(),
109 bind(&tokenCompare, _1 , token));
110
111 if( it != tokenList.end() ){
112 return true;
113 }
114
115 return false;
116}