blob: a1ed2bd63608c2417225f94712d773522855941f [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 {
akmhoque05d5fcf2014-04-15 14:58:45 -050018 char_separator<char> sep(m_seps.c_str());
19 tokenizer< char_separator<char> >tokens(m_originalString, sep);
akmhoque5a44dd42014-03-12 18:11:32 -050020 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 }
akmhoque05d5fcf2014-04-15 14:58:45 -050030 m_firstToken=m_vTokenList[0];
akmhoque5a44dd42014-03-12 18:11:32 -050031 makeRestOfTheLine();
32 }
akmhoqueba094742014-02-28 11:47:21 -060033
akmhoque5a44dd42014-03-12 18:11:32 -050034 void
35 nlsrTokenizer::insertToken(const string& token)
36 {
akmhoque05d5fcf2014-04-15 14:58:45 -050037 m_tokenList.push_back(token);
38 m_vTokenList.push_back(token);
akmhoque5a44dd42014-03-12 18:11:32 -050039 }
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;
akmhoque05d5fcf2014-04-15 14:58:45 -050046 for(std::list<string>::iterator it=m_tokenList.begin();
47 it!=m_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 }
akmhoque05d5fcf2014-04-15 14:58:45 -050055 if( i < m_tokenList.size() )
akmhoque5a44dd42014-03-12 18:11:32 -050056 {
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="";
akmhoque05d5fcf2014-04-15 14:58:45 -050066 if((from>=0 && to<m_tokenList.size()) &&
67 (to>=from && to <m_tokenList.size()))
akmhoqueba094742014-02-28 11:47:21 -060068 {
akmhoque5a44dd42014-03-12 18:11:32 -050069 for(int i=from; i<=to; i++)
70 {
akmhoque05d5fcf2014-04-15 14:58:45 -050071 returnString+=m_seps;
72 returnString+=m_vTokenList[i];
akmhoque5a44dd42014-03-12 18:11:32 -050073 }
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 {
akmhoque05d5fcf2014-04-15 14:58:45 -050082 return getTokenString(from,m_tokenList.size()-1);
akmhoque5a44dd42014-03-12 18:11:32 -050083 }
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 {
akmhoque05d5fcf2014-04-15 14:58:45 -050094 m_restOfTheLine=getTokenString(1);
akmhoque5a44dd42014-03-12 18:11:32 -050095 }
akmhoqueba094742014-02-28 11:47:21 -060096
akmhoque5a44dd42014-03-12 18:11:32 -050097 bool
98 nlsrTokenizer::doesTokenExist(string token)
99 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500100 std::list<string >::iterator it = std::find_if( m_tokenList.begin(),
101 m_tokenList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -0500102 bind(&tokenCompare, _1 , token));
akmhoque05d5fcf2014-04-15 14:58:45 -0500103 if( it != m_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