Repository navigation
Expand file tree
/
Copy pathtemaAPD.java
More file actions
126 lines (104 loc) · 3.23 KB
/
Copy pathtemaAPD.java
File metadata and controls
126 lines (104 loc) · 3.23 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
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/*
* Anda Nenu
* 334CA
*/
public class temaAPD {
public static void main(String[] args) {
int nrThreads = Integer.parseInt(args[0]);
String f_in = args[1];
String f_out = args[2];
try {
BufferedReader in = new BufferedReader(new FileReader(f_in));
String sursa = in.readLine();
long dim = Integer.parseInt(in.readLine());
float similaritateMax = Float.parseFloat(in.readLine());
int nrDocumente = Integer.parseInt(in.readLine());
String documente[] = new String[nrDocumente];
int checkDocument = -1;
for (int i=0;i<nrDocumente;i++){
documente[i] = in.readLine();
if (documente[i].compareTo(sursa)==0){
checkDocument = i;
}
}
in.close();
for (int i=0;i<nrDocumente;i++){
ProcSimilar.nrCuvinte.put(documente[i], 0);
ProcSimilar.MapFile.put(documente[i], new ConcurrentHashMap<String,Integer>());
}
ExecutorService executor1 = Executors.newFixedThreadPool(nrThreads);
long startChar = 0;
int crtDoc = 0;
ProcSimilar proc = null;
while (true){
proc = new ProcSimilar(documente[crtDoc], startChar, startChar + dim);
executor1.execute(proc);
startChar += dim;
RandomAccessFile f = new RandomAccessFile(documente[crtDoc],"r");
if (startChar>=f.length()){
f.close();
startChar = 0;
crtDoc++;
if (crtDoc>=nrDocumente){
break;
}
}
else {
f.close();
}
}
executor1.shutdown();
while (!executor1.isTerminated()){
}
/*
for (String key : ProcSimilar.MapFile.keySet()){
System.out.println(key + " : " + ProcSimilar.MapFile.get(key));
}
for (String key : ProcSimilar.nrCuvinte.keySet()){
System.out.println(key + " : " + ProcSimilar.nrCuvinte.get(key));
}*/
ExecutorService executor2 = Executors.newFixedThreadPool(nrThreads);
calculateSim similar = null;
for (crtDoc=0;crtDoc<nrDocumente;crtDoc++){
if (crtDoc==checkDocument)
crtDoc ++;
similar = new calculateSim(
ProcSimilar.MapFile.get(documente[checkDocument]),
ProcSimilar.MapFile.get(documente[crtDoc]),
ProcSimilar.nrCuvinte.get(documente[checkDocument]),
ProcSimilar.nrCuvinte.get(documente[crtDoc]),
documente[crtDoc]);
executor2.execute(similar);
}
executor2.shutdown();
while (!executor2.isTerminated()){
}
BufferedWriter out = new BufferedWriter(new FileWriter(f_out));
out.write("Rezultate pentru: ("+documente[checkDocument]+")\n");
out.write("\n");
for (int i=0;i<nrDocumente;i++){
if (i!=checkDocument){
float f = calculateSim.frecvente.get(documente[i]);
String trunc = String.format("%.4f",f);
trunc = trunc.substring(0, trunc.length()-1);
//System.out.print((documente[i]+" ("+trunc+"%)\n"));
if (f>=similaritateMax){
out.write(documente[i]+" ("+trunc+"%)\n");
}
}
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}