blob: cf655ba43e2ea9dd60d7f8443e121c7867f6bb19 [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
akmhoqueb1710aa2014-02-19 17:13:36 -06009namespace nlsr {
10
akmhoque298385a2014-02-13 14:13:09 -060011using namespace std;
12using namespace boost;
13
14void
15nlsrTokenizer::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
34void
35nlsrTokenizer::insertToken(const string& token){
36 tokenList.push_back(token);
37}
38
39int
40nlsrTokenizer::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
58string
59nlsrTokenizer::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
80string
81nlsrTokenizer::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
102static bool
103tokenCompare(string& s1, string& s2){
104 return s1==s2;
105}
106
107bool
108nlsrTokenizer::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}
akmhoqueb1710aa2014-02-19 17:13:36 -0600119
120}//namespace nlsr