test_dict.cpp
2.92 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* File: test.cpp
* Author: lennyn
*
* Created on October 22, 2013, 2:11 PM
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include "fsa.hpp"
using namespace std;
void validate(const bool cond, const string& msg) {
if (!cond) {
cerr << msg << endl;
exit(1);
}
}
unsigned char* readFile(const char* fname) {
ifstream ifs;
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ifs.open(fname, ios::in | ios::binary | ios::ate);
// if (ifs.is_open()) {
int size = ifs.tellg();
unsigned char* memblock = new unsigned char [size];
ifs.seekg(0, ios::beg);
ifs.read(reinterpret_cast<char*> (memblock), size);
ifs.close();
return memblock;
// }
// else {
// cerr << "Unable to open file " << fname << endl;
// }
}
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
static inline string &rtrim(string &s) {
s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end());
return s;
}
void testFSA(const FSA<char*>& fsa, const char* fname) {
ifstream ifs;
// ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ifs.open(fname, ios::binary);
string line;
while (getline(ifs, line)) {
vector<string> split1(split(line, '\t'));
string key = split1[0];
key = "bijekcją";
string value = split1[1];
for (unsigned int i = 0; i < key.length(); i++) {
cout << (int) key[i] << " ";
}
cout << endl;
char* value2;
if (fsa.tryToRecognize(key.c_str(), value2)) {
if (string(value) != string(value2)) {
cout << "BAD INTERP " << key << " " << value << " != " << value2 << endl;
}
else {
cout << "OK! " << key << " " << value << endl;
}
}
else {
cout << "MISS " << key << " " << value << " not recognized" << endl;
}
}
cout << ifs.good() << endl;
cout << ifs.fail() << endl;
cout << ifs.eof() << endl;
cout << "done" << endl;
}
/*
*
*/
int main(int argc, char** argv) {
validate(argc == 3, "Must provide exactly two arguments - FSA filename and dictionary filename.");
const unsigned char* fsaData = readFile(argv[1]);
StringDeserializer deserializer;
SimpleFSA<char*> fsa(fsaData, deserializer);
testFSA(fsa, argv[2]);
cout << argc << endl;
return 0;
}