forked from hmm34/narq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
89 lines (74 loc) · 2.31 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
@file main.cpp
@brief Execution point for narq
@author Drew Guarnera, Heather Michaud
*/
#include "tools.hpp"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
std::string expectedInput = "narq [-copy] <original.txt> <copy.txt> <number of partitions>\n";
int main(int argc, const char* argv[])
{
if (argc != 5) {
std::cerr << "Invalid arguments. Expected: \n";
std::cerr << expectedInput;
return 1;
}
std::string approach = argv[1];
std::string fNeedle = argv[2];
std::string fHaystack = argv[3];
int numNeedles = 0;
std::stringstream ss;
ss << argv[4];
ss >> numNeedles;
std::ifstream inputNeedle(fNeedle.c_str());
if (!inputNeedle)
{
std::cerr << "Could not open original text file: " << fNeedle << "\n";
return 2;
}
std::ifstream inputHaystack(fHaystack.c_str(), std::ios::in | std::ios::binary);
if (!inputHaystack)
{
std::cerr << "Could not open suspected copied text file: " << fHaystack << "\n";
return 2;
}
std::string needle;
inputNeedle.seekg(0, std::ios::end);
needle.resize(inputNeedle.tellg());
inputNeedle.seekg(0, std::ios::beg);
inputNeedle.read(&needle[0], needle.size());
inputNeedle.close();
std::vector<std::string> needles = narq::partition(needle, numNeedles);
// Read entirue haystack file into the haystack string.
std::string haystack;
inputHaystack.seekg(0, std::ios::end);
haystack.resize(inputHaystack.tellg());
inputHaystack.seekg(0, std::ios::beg);
inputHaystack.read(&haystack[0], haystack.size());
inputHaystack.close();
std::cout << "\nDetecting plagiarism using " << numNeedles << " partitions";
std::vector<int> matches = narq::rabinKarpMulti(needles, haystack, numNeedles);
int totalMatches = 0;
for (int i = 0; i < numNeedles; ++i)
{
if (i == 0)
std::cout << ", each containing " << needles[i].length() << " characters.\n";
if (matches[i] > 0)
{
std::cout << std::left
<< std::setw(10) << matches[i] << std::setw(3) << " : "
<< std::setw(50) << needles[i] << "\n";
}
totalMatches += matches[i];
}
std::cout << "\nPlagiarism detection complete.\n";
std::cout << "Used " << numNeedles << " partitions, each containing "
<< needles[0].length() << " characters.\n";
std::cout << "Total matches: " << totalMatches << "\n";
return 0;
}