-
Notifications
You must be signed in to change notification settings - Fork 36
/
strtk_text_parser_example01.cpp
76 lines (63 loc) · 2.31 KB
/
strtk_text_parser_example01.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
*****************************************************************
* String Toolkit Library *
* *
* Text Parser Example 01 *
* Author: Arash Partow (2002-2020) *
* URL: http://www.partow.net/programming/strtk/index.html *
* *
* Copyright notice: *
* Free use of the String Toolkit Library is permitted under the *
* guidelines and in accordance with the most current version of *
* the MIT License. *
* http://www.opensource.org/licenses/MIT *
* *
*****************************************************************
*/
/*
Description: The following will parse a text file line-by-line,
tokenizing each line using the delimiters set as
described blow. The objective is to populate the
word_list with tokens derived from the text file.
*/
#include <iostream>
#include <iterator>
#include <utility>
#include <string>
#include <deque>
#include "strtk.hpp"
template<typename Container, typename Predicate>
struct parse_line
{
public:
parse_line(Container& c, Predicate& p)
: c_(&c),
p_(&p)
{}
inline void operator()(const std::string& s)
{
strtk::split(*p_,
s,
strtk::range_to_type_back_inserter(*c_),
strtk::split_options::compress_delimiters);
}
private:
Container* c_;
Predicate* p_;
};
template<typename Container>
void parse_text(const std::string& file_name, Container& c)
{
static const std::string delimiters = " ,.;:<>'[]{}()_?/\\'`~!@#$%^&*|-_\"=+\t\r\n0123456789";
strtk::multiple_char_delimiter_predicate predicate(delimiters);
parse_line<Container,strtk::multiple_char_delimiter_predicate> pl(c,predicate);
strtk::for_each_line(file_name,pl);
}
int main()
{
std::string text_file_name = "text.txt";
std::deque< std::string > word_list;
parse_text(text_file_name,word_list);
std::cout << "Token Count: " << word_list.size() << std::endl;
return 0;
}