blob: 7c82ecd7e0d585589e9e683eef8ee0065f8b00fb [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()){
60 int i=1;
61 for(std::list<string>::iterator it=tokenList.begin();
62 it!=tokenList.end();it++){
63 if( i >= from && i<= to ){
64 string oneToken((*it));
65 returnString+=seps;
66 returnString+=oneToken;
67
68 }
69 i++;
70 }
71 }
72
73 trim(returnString);
74 return returnString;
75}
76
77string
78nlsrTokenizer::getTokenString(int from){
79 string returnString;
80 if ( from >=0 && from < tokenList.size()){
81 int i=1;
82 for(std::list<string>::iterator it=tokenList.begin();
83 it!=tokenList.end();it++){
84 if( i >= from){
85 string oneToken((*it));
86 returnString+=seps;
87 returnString+=oneToken;
88
89 }
90 i++;
91 }
92 }
93
94 trim(returnString);
95 return returnString;
96}
97
98static bool
99tokenCompare(string& s1, string& s2){
100 return s1==s2;
101}
102
103bool
104nlsrTokenizer::doesTokenExist(string token){
105 std::list<string >::iterator it = std::find_if( tokenList.begin(),
106 tokenList.end(),
107 bind(&tokenCompare, _1 , token));
108
109 if( it != tokenList.end() ){
110 return true;
111 }
112
113 return false;
114}