-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadFile.java
175 lines (121 loc) · 5.04 KB
/
ReadFile.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.io.*;
import java.util.*;
public class ReadFile {
String path;
int fileIndex;
ArrayList<String> documents;
Parse parser;
/**
* This is a constructor
* @param pathOfCorpus
* @param pathOfPosting
* @param useStemmer
* @throws FileNotFoundException
*/
public ReadFile(String pathOfCorpus , String pathOfPosting , boolean useStemmer) throws FileNotFoundException {
this.path = pathOfCorpus +"//corpus";
this.fileIndex = 0;
this.documents = new ArrayList<>();
this.parser = new Parse();
parser.makeStopWordsHashSet(pathOfCorpus);
parser.indexer.path=pathOfPosting;
parser.indexer.useStemmer=useStemmer;
}
/**
* This function is splitting the folders names
* @return list of names
* @throws FileNotFoundException
*/
public String[] SplitFoldersNames() throws FileNotFoundException {
File folder = new File(path);
File[] files = folder.listFiles();
String[] names = new String[files.length];
for (int i =0 ; i<names.length ; i++)
{
names[i] = files[i].getName();
}
return names;
}
/**
* This function is reading and splitting the files
* @throws IOException
*/
public void ReadAndSplitFile() throws IOException {
String[] folderNames=SplitFoldersNames();
//BufferedReader reader = null;
Scanner s =null;
String filePath =null;
int docIndex;
String docString;
//loop on each folder and sent it to parse
for (int i =0 ; i< folderNames.length ; i++) {
documents.clear();
// System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//System.out.println(folderNames[i].toString());
filePath = path + "\\" + folderNames[i] + "\\" + folderNames[i];
// reader = new BufferedReader(new FileReader(new File(filePath)));
s = new Scanner(new File(filePath));
docIndex = 0;
// String doc = s.next();
StringBuilder docStringBulider = new StringBuilder();
if (s.hasNext()) {
docStringBulider.append(s.next());
}
while (s.hasNext()) {
String nextWord = s.next();
docStringBulider.append(" " + nextWord);
//splitting docs by <DOC> tab
while (s.hasNext() && !(nextWord.equals("<DOC>"))) {
nextWord = s.next();
docStringBulider.append(" " + nextWord);
}
docString = docStringBulider.toString(); //change from string builder to String
//remove from doc - ; : " ) ( [ ] ?
docString = docString.replaceAll(":","");
docString = docString.replaceAll(";","");
docString = docString.replaceAll("[\\[\\](){}]","");
docString = docString.replaceAll("\\?","");
docString = docString.replaceAll("!","");
docString = docString.replaceAll("\\*","");
// docString = docString.replaceAll("\\..","");
docString = docString.replaceAll("\\|","");
docString = docString.replaceAll("\"", "");
docString = docString.replaceAll("#","");
docString = docString.replaceAll("&","");
docString = docString.replaceAll("'","");
docString = docString.replaceAll("\\+","");
docString = docString.replaceAll("-/","");
docString = docString.replaceAll("`","");
docString = docString.replaceAll("$/","");
docString = docString.replaceAll("\\\\","");
//
/*
docString.replaceAll("(","");
docString.replaceAll(")","");
docString.replaceAll("]","");
*/
documents.add(docIndex, docString);
docIndex++;
docStringBulider = new StringBuilder();
docStringBulider.append(nextWord);
}
sendDocsToParser();
if ((i+1) % 20 == 0 || (i+1) == folderNames.length){ //every 20 files create posting file
parser.indexer.sendToPostingFiles();
parser.indexer.sendDocsDictionaryToPostingFiles();
}
}
s.close();
parser.indexer.sendMainDictionaryToPostingFiles();
parser.indexer.margePostingFiles();
}
/**
* this function is sent the documents to parser
* @throws IOException
*/
public void sendDocsToParser() throws IOException {
for(int i = 0; i< documents.size() ; i++){
parser.sendDocToParse(documents.get(i));
}
}
}