-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPIMI_ALGO.java
executable file
·231 lines (221 loc) · 9.17 KB
/
SPIMI_ALGO.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.xml.sax.Attributes;
import java.time.Duration;
import java.time.Instant;
import java.io.FileReader;
import java.io.FileWriter;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.regex.Pattern;
public class SPIMI_ALGO {
public static String valcategory="$1";
public static int subfilenumber = 0;
public static int tempnumber=0;
static String word1 ;
static String word2 ;
static String value1 ;
static String value2 ;
public static void main(String[] args) throws Exception {
mergeblocks();
}
public static void createblock (TreeMap<String, SortedSet<Pair<Integer, String>>> indexer) throws Exception
{
long curTime=System.currentTimeMillis();
File statText = new File("temp/block-"+subfilenumber+".txt");
statText.getParentFile().mkdirs();
statText.createNewFile();
FileOutputStream is = new FileOutputStream(statText);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
subfilenumber++;
String key2 = "%$%";
boolean firstline = true;
for(Map.Entry<String, SortedSet<Pair<Integer, String>>> entry : indexer.entrySet()) {
String key = entry.getKey();
valcategory = key;
if(!firstline)
w.write("\n");
firstline=false;
SortedSet<Pair<Integer, String>> pr2 = entry.getValue();
w.write(valcategory + ":");
//for(Map.Entry<Integer, SortedSet<Pair<Integer, String>>> entry2 : value.entrySet())
//{
//Integer doc = entry2.getKey();
//w.write(doc.toString() + ":");
// SortedSet<Pair<Integer, String>> pr2 = entry.getValue();
boolean ft=true;
String dq="";
for (Pair<Integer, String> pr:pr2)
{
Integer in = pr.getFirst();
String qr = pr.getSecond();
if(!ft)
dq=",";
w.write(dq+in.toString()+"-"+qr);
ft=false;
}
key2=key;
// w.write(";");
//}
}
w.close();
System.out.println(subfilenumber + " Block created in "+((System.currentTimeMillis()-curTime)/1000)+" seconds.");
}
public static boolean keycheck(String key,String key2){
String PATTERN_TOKEN = "$";
//String s1 = s2.replaceAll("[-:()/^[!|=,?._'{}@+\\[\\]]]", " ");
boolean fd=true;
String tg=".";
String fkey=".",fkey2="..";
HashSet<String> parts2 = new HashSet<String>();
final StringTokenizer normalTokenizer = new StringTokenizer(key,PATTERN_TOKEN);
while(normalTokenizer.hasMoreTokens()){
String sr= normalTokenizer.nextToken().trim();
if(fd)
{
fkey=sr;
}
else{
tg=sr;
}
fd=false;
}
fd=true;
final StringTokenizer normalTokenizer2 = new StringTokenizer(key2,PATTERN_TOKEN);
while(normalTokenizer2.hasMoreTokens()){
String sr= normalTokenizer2.nextToken().trim();
if(fd)
{
fkey2=sr;
}
fd=false;
}
if(fkey.equalsIgnoreCase(fkey2))
{
valcategory="|$" + tg;
return true;
}
else
{
return false;
}
}
public static void setValues(String input1 ,String input2){
word1=input1.substring(0,input1.indexOf(":"));
value1=input1.substring(input1.indexOf(":")+1);
word2=input2.substring(0,input2.indexOf(":"));
value2=input2.substring(input2.indexOf(":")+1);
}
public static void mergeblocks() throws Exception
{
File folder = new File("temp/");
File[] listOfFiles = folder.listFiles();
Arrays.sort(listOfFiles, new Comparator<File>(){
@Override
public int compare(File f1, File f2) {
String s1 = f1.getName().substring(f1.getName().indexOf("-")+1,f1.getName().indexOf("."));
String s2 = f2.getName().substring(f2.getName().indexOf("-")+1,f2.getName().indexOf("."));
return Integer.valueOf(s1).compareTo(Integer.valueOf(s2));
}
});
for(File t:listOfFiles)
System.out.println(t.getName());
String gh="";
while(listOfFiles.length>=2)
{
gh+="0";
int r=listOfFiles.length/2;
tempnumber = subfilenumber%1;
for(int i=0;i<r;i++)
{
File filepath1=listOfFiles[2*i] ;
File filepath2=listOfFiles[2*i+1] ;
System.out.println(filepath1 +" "+filepath2);
try {
FileWriter ostream = new FileWriter("temp/block-"+gh+i+".txt");
BufferedWriter out = new BufferedWriter(ostream);
BufferedReader br1 = new BufferedReader(new FileReader(filepath1.getAbsolutePath()),10*1024);
BufferedReader br2 = new BufferedReader(new FileReader(filepath2.getAbsolutePath()),10*1024);
String line1=null ;
String line2=null ;
line1 = br1.readLine();
line2 = br2.readLine();
while(true){
setValues(line1,line2);
if(word1.compareTo(word2) < 0){
out.write(line1);
out.newLine();
line1=br1.readLine();
if(line1==null)
break ;
}
if(word1.compareTo(word2) > 0){
out.write(line2);
out.newLine();
line2=br2.readLine();
if(line2==null)
break ;
}
if(word1.compareTo(word2) == 0){
String line = line1+ "," + value2;
out.write(line);
out.newLine();
line1=br1.readLine();
line2=br2.readLine();
if(line2==null || line1==null)
break ;
}
}
if(line1==null){
while(line2!=null){
out.write(line2);
out.newLine();
line2=br2.readLine();
}
}
if(line2==null){
while(line1!=null){
out.write(line1);
out.newLine();
line1=br1.readLine();
}
}
br1.close();
br2.close();
out.close();
ostream.close();
}catch(IOException e){
e.printStackTrace();
System.exit(0) ;
}
filepath1.delete();
filepath2.delete();
}
listOfFiles = folder.listFiles();
Arrays.sort(listOfFiles, new Comparator<File>(){
@Override
public int compare(File f1, File f2) {
String s1 = f1.getName().substring(f1.getName().indexOf("-")+1,f1.getName().indexOf("."));
String s2 = f2.getName().substring(f2.getName().indexOf("-")+1,f2.getName().indexOf("."));
return Integer.valueOf(s1).compareTo(Integer.valueOf(s2));
}
});
for(File t:listOfFiles)
System.out.println(t.getName());
}
}
}