blob: d64a600122962910d13e08293f6218da6e544923 [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();
akmhoque1fd8c1e2014-02-19 19:41:49 -060021 string ft(*tok_iter);
22 firstToken=ft;
23 ++tok_iter;
akmhoque1fd8c1e2014-02-19 19:41:49 -060024 for ( ; tok_iter != tokens.end(); ++tok_iter)
25 {
26 string oneToken(*tok_iter);
27 this->insertToken(oneToken);
28 restOfTheLine+=oneToken;
29 restOfTheLine+=seps;
30 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060031 trim(restOfTheLine);
32 }
akmhoque298385a2014-02-13 14:13:09 -060033
akmhoque1fd8c1e2014-02-19 19:41:49 -060034 void
35 nlsrTokenizer::insertToken(const string& token)
36 {
37 tokenList.push_back(token);
38 }
akmhoque298385a2014-02-13 14:13:09 -060039
akmhoque1fd8c1e2014-02-19 19:41:49 -060040 int
41 nlsrTokenizer::getTokenPosition(string& token)
42 {
43 int pos=-1;
44 int i=1;
akmhoque1fd8c1e2014-02-19 19:41:49 -060045 for(std::list<string>::iterator it=tokenList.begin(); it!=tokenList.end(); it++)
46 {
47 if( (*it) == token )
48 {
49 break;
50 }
51 i++;
52 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060053 if( i < tokenList.size() )
54 {
55 pos=i;
56 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060057 return pos;
58 }
akmhoque298385a2014-02-13 14:13:09 -060059
akmhoque1fd8c1e2014-02-19 19:41:49 -060060 string
61 nlsrTokenizer::getTokenString(int from , int to)
62 {
63 string returnString;
64 if ( from >=0 && to < tokenList.size())
65 {
66 int i=0;
67 for(std::list<string>::iterator it=tokenList.begin();
68 it!=tokenList.end(); it++)
69 {
70 i++;
71 if( i >= from && i<= to )
72 {
73 string oneToken((*it));
74 returnString+=seps;
75 returnString+=oneToken;
akmhoque1fd8c1e2014-02-19 19:41:49 -060076 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060077 }
78 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060079 trim(returnString);
80 return returnString;
81 }
akmhoque298385a2014-02-13 14:13:09 -060082
akmhoque1fd8c1e2014-02-19 19:41:49 -060083 string
84 nlsrTokenizer::getTokenString(int from)
85 {
86 string returnString;
87 if ( from >=0 && from < tokenList.size())
88 {
89 int i=0;
90 for(std::list<string>::iterator it=tokenList.begin();
91 it!=tokenList.end(); it++)
92 {
93 i++;
94 if( i >= from)
95 {
96 string oneToken((*it));
97 returnString+=seps;
98 returnString+=oneToken;
akmhoque1fd8c1e2014-02-19 19:41:49 -060099 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600100 }
101 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600102 trim(returnString);
103 return returnString;
104 }
105
106 static bool
107 tokenCompare(string& s1, string& s2)
108 {
109 return s1==s2;
110 }
111
112 bool
113 nlsrTokenizer::doesTokenExist(string token)
114 {
115 std::list<string >::iterator it = std::find_if( tokenList.begin(),
116 tokenList.end(),
117 bind(&tokenCompare, _1 , token));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600118 if( it != tokenList.end() )
119 {
120 return true;
121 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600122 return false;
123 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600124
125}//namespace nlsr