blob: 35d6625f72e2a51777804f6db3f4b6f82bbcb6e1 [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -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
9namespace nlsr
10{
11
akmhoque5a44dd42014-03-12 18:11:32 -050012 using namespace std;
13 using namespace boost;
akmhoqueba094742014-02-28 11:47:21 -060014
akmhoque5a44dd42014-03-12 18:11:32 -050015 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();
21 for ( ; tok_iter != tokens.end(); ++tok_iter)
akmhoqueba094742014-02-28 11:47:21 -060022 {
akmhoque5a44dd42014-03-12 18:11:32 -050023 string oneToken(*tok_iter);
24 trim(oneToken);
25 if(!oneToken.empty())
26 {
27 insertToken(oneToken);
28 }
akmhoqueba094742014-02-28 11:47:21 -060029 }
akmhoque5a44dd42014-03-12 18:11:32 -050030 firstToken=vTokenList[0];
31 makeRestOfTheLine();
32 }
akmhoqueba094742014-02-28 11:47:21 -060033
akmhoque5a44dd42014-03-12 18:11:32 -050034 void
35 nlsrTokenizer::insertToken(const string& token)
36 {
37 tokenList.push_back(token);
38 vTokenList.push_back(token);
39 }
akmhoqueba094742014-02-28 11:47:21 -060040
akmhoque5a44dd42014-03-12 18:11:32 -050041 int
42 nlsrTokenizer::getTokenPosition(string& token)
43 {
44 int pos=-1;
45 int i=0;
46 for(std::list<string>::iterator it=tokenList.begin();
47 it!=tokenList.end(); it++)
akmhoqueba094742014-02-28 11:47:21 -060048 {
akmhoque5a44dd42014-03-12 18:11:32 -050049 if( (*it) == token )
50 {
51 break;
52 }
53 i++;
akmhoqueba094742014-02-28 11:47:21 -060054 }
akmhoque5a44dd42014-03-12 18:11:32 -050055 if( i < tokenList.size() )
56 {
57 pos=i;
58 }
59 return pos;
60 }
akmhoqueba094742014-02-28 11:47:21 -060061
akmhoque5a44dd42014-03-12 18:11:32 -050062 string
63 nlsrTokenizer::getTokenString(int from , int to)
64 {
65 string returnString="";
66 if((from>=0 && to<tokenList.size()) &&
67 (to>=from && to <tokenList.size()))
akmhoqueba094742014-02-28 11:47:21 -060068 {
akmhoque5a44dd42014-03-12 18:11:32 -050069 for(int i=from; i<=to; i++)
70 {
71 returnString+=seps;
72 returnString+=vTokenList[i];
73 }
akmhoqueba094742014-02-28 11:47:21 -060074 }
akmhoque5a44dd42014-03-12 18:11:32 -050075 trim(returnString);
76 return returnString;
77 }
akmhoqueba094742014-02-28 11:47:21 -060078
akmhoque5a44dd42014-03-12 18:11:32 -050079 string
80 nlsrTokenizer::getTokenString(int from)
81 {
82 return getTokenString(from,tokenList.size()-1);
83 }
akmhoqueba094742014-02-28 11:47:21 -060084
akmhoque5a44dd42014-03-12 18:11:32 -050085 static bool
86 tokenCompare(string& s1, string& s2)
87 {
88 return s1==s2;
89 }
akmhoqueba094742014-02-28 11:47:21 -060090
akmhoque5a44dd42014-03-12 18:11:32 -050091 void
92 nlsrTokenizer::makeRestOfTheLine()
93 {
94 restOfTheLine=getTokenString(1);
95 }
akmhoqueba094742014-02-28 11:47:21 -060096
akmhoque5a44dd42014-03-12 18:11:32 -050097 bool
98 nlsrTokenizer::doesTokenExist(string token)
99 {
100 std::list<string >::iterator it = std::find_if( tokenList.begin(),
101 tokenList.end(),
102 bind(&tokenCompare, _1 , token));
103 if( it != tokenList.end() )
akmhoqueba094742014-02-28 11:47:21 -0600104 {
akmhoque5a44dd42014-03-12 18:11:32 -0500105 return true;
akmhoqueba094742014-02-28 11:47:21 -0600106 }
akmhoque5a44dd42014-03-12 18:11:32 -0500107 return false;
108 }
akmhoqueba094742014-02-28 11:47:21 -0600109
110}//namespace nlsr