Repository navigation
Expand file tree
/
Copy pathProcSimilar.java
More file actions
120 lines (100 loc) · 2.54 KB
/
Copy pathProcSimilar.java
File metadata and controls
120 lines (100 loc) · 2.54 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
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/*
* Anda Nenu
* 334CA
*/
public class ProcSimilar implements Runnable {
static Map<String,Map<String,Integer>> MapFile = Collections.synchronizedMap(new HashMap<String,Map<String,Integer>>());
private Map<String,Integer> map;
static Map<String,Integer> nrCuvinte = Collections.synchronizedMap(new HashMap<String,Integer>());
String fileName ;
long startRead;
long endRead;
public ProcSimilar(String fileName, long startRead, long endRead) {
map = new HashMap<String,Integer>();
this.fileName = fileName;
this.startRead = startRead;
this.endRead = endRead;
}
public void run() {
try {
RandomAccessFile in = new RandomAccessFile(fileName,"r");
String word = "";
char c=' ';
if (startRead<in.length()){
in.seek(startRead);
c = (char) in.read();
c = Character.toLowerCase(c);
startRead ++;
if (c>='a' && c<='z'){
if (startRead>0){
in.seek(startRead-1);
c = (char) in.read();
c = Character.toLowerCase(c);
if (c>='a' && c<='z'){
while (c>='a' && c<='z'){
c = (char) in.read();
c = Character.toLowerCase(c);
startRead ++;
}
}
else {
word += c;
}
}
}
}
while (startRead<endRead && startRead<in.length()){
while ((c<'a' || c>'z') && (startRead<endRead)){
c = (char) in.read();
c = Character.toLowerCase(c);
startRead ++;
}
if (startRead<endRead){
while (c>='a' && c<='z' && startRead<in.length()){
word += c;
c = (char) in.read();
c = Character.toLowerCase(c);
startRead ++;
}
if (map.containsKey(word)){
int l = (int) map.get(word);
map.put(word,l+1);
}
else {
map.put(word,1);
}
synchronized (nrCuvinte){
int cuv = nrCuvinte.get(fileName)+1;
nrCuvinte.put(fileName, cuv);
}
word = "";
}
}
synchronized (MapFile) {
Map<String,Integer> aux = MapFile.get(fileName);
for (String key : map.keySet() ){
if (aux.containsKey(key)){
int l = aux.get(key) + map.get(key);
aux.put(key,l);
}
else{
aux.put(key,map.get(key));
}
}
MapFile.remove(fileName);
MapFile.put(fileName,aux);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
map.clear();
}
}