LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SpiritCharChartParser.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
7
8void readStream(std::istream &is,std::string &dest)
9{
10 while (is.good() && !is.eof())
11 {
12 const int bufferSize = 10240;
13 char buffer[bufferSize];
14 is.read(buffer,bufferSize);
15 const int size = is.gcount();
16 if (size>0)
17 dest.insert(dest.end(),buffer,buffer+size);
18 }
19}
20
22// Main program
24int main(int argc, char* argv[])
25{
26 std::vector<std::string> files;
27
28 po::options_description desc("Usage");
29 desc.add_options()
30 ("help,h", "Display this help message")
31 ("input-file", po::value< std::vector<std::string> >(&files), "Set a text file to analyze");
32
33 po::positional_options_description p;
34 p.add("input-file", -1);
35
36 po::variables_map vm;
37 po::store(po::command_line_parser(argc, argv).
38 options(desc).positional(p).run(), vm);
39 po::notify(vm);
40
41 if (vm.count("help"))
42 {
43 std::cout << desc << std::endl;
44 return 1;
45 }
46
47 namespace qi = boost::spirit::qi;
48 namespace ascii = boost::spirit::ascii;
49 using ascii::space;
50 typedef std::string::const_iterator iterator_type;
52
53 // @ERROR DOES NOT WORK ON WINDOWS PORT
54 charchart_parser parser; // Our grammar
55 for (std::vector<std::string>::iterator fileItr=files.begin(); fileItr!=files.end(); fileItr++)
56 {
57 std::string str;
58 std::ifstream file(fileItr->c_str(), std::ifstream::binary);
59 readStream(file, str);
60
61 std::string::const_iterator iter = str.begin();
62 std::string::const_iterator end = str.end();
63
65 bool r = false;
66 // @ERROR DOES NOT WORK ON WINDOWS PORT
67 r = phrase_parse(iter, end, parser, skipper, charchart);
68 if (r && iter == end)
69 {
70 std::cout << "Parsing succeeded: "<<charchart.classes.size()<<" classes, "<<charchart.chars.size()<<" chars" << std::endl;
71 }
72 else
73 {
74 std::string rest(iter, end);
75 std::cout << "Parsing failed. Stopped at: \": " << rest << "\"\n";
76 }
77 }
78
79 return 0;
80}
int main(int argc, char *argv[])
void readStream(std::istream &is, std::string &dest)
#define skipper
int run(int aargc, char **aargv)
Definition main.cpp:31
std::vector< charchart_char > chars
std::vector< charchart_class > classes