-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReader.cpp
112 lines (81 loc) · 2.38 KB
/
Reader.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// Reader.cpp
// Trabalho ED2
//
// Created by Yan Ferreira on 6/26/16.
// Copyright © 2016 Yan Ferreira. All rights reserved.
//
#include "Reader.hpp"
std::list<string> Reader::getFilesInDirectory(string directory)
{
std::list<string> files;
DIR * diretorio;
struct dirent * entry;
if((diretorio = opendir(directory.c_str()))){
while((entry = readdir(diretorio))){
if (string(entry->d_name).find(".txt") != string::npos || string(entry->d_name).find(".rtf") != string::npos) {
files.push_front(directory + entry->d_name);
}
}
closedir(diretorio);
}
return files;
}
void Reader::inserePorDiretorio(Estrutura *estrutura, string directory)
{
std::list<string> files = getFilesInDirectory(directory);
while(!files.empty()){
string file_name = files.front();
ifstream file (file_name.c_str());
insereDicionario(estrutura, file);
file.close();
files.pop_front();
}
}
void Reader::insereDicionario(Estrutura * estrutura, ifstream &file)
{
string linha;
while(file >> linha){
transform(linha.begin(), linha.end(), linha.begin(), ::tolower);
estrutura->insere(linha.substr(0, linha.find('/', 0)));
}
}
int Reader::getNumRegistros(string directory)
{
std::list<string> files = getFilesInDirectory(directory);
int count = 0;
while(!files.empty()){
string file_name = files.front();
ifstream file (file_name.c_str());
string linha;
while (file >>linha){
count++;
}
file.close();
files.pop_front();
}
return count;
}
void Reader::fazBuscaOrtograficaPorDiretorio(Estrutura * corretor, Estrutura * estrutura, string directory)
{
std::list<string> files = getFilesInDirectory(directory);
while(!files.empty()){
string file_name = files.front();
ifstream file (file_name.c_str());
buscaDicionario(corretor, estrutura, file);
file.close();
files.pop_front();
}
}
void Reader::buscaDicionario(Estrutura * corretor, Estrutura * estrutura, ifstream &file)
{
string linha;
while(file >> linha){
string palavra = helper.normalize(linha);
if(palavra == "")
continue;
if(!estrutura->busca(palavra)){
corretor->insere(palavra);
}
}
}