Repository navigation
Expand file tree
/
Copy pathWspp.java
More file actions
97 lines (92 loc) · 4.26 KB
/
Copy pathWspp.java
File metadata and controls
97 lines (92 loc) · 4.26 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
import java.io.*;
import java.util.*;
import IntList.IntList;
public class Wspp {
static boolean partOfWrord(char partOfLine) {
if ((partOfLine == '\'') || (Character.DASH_PUNCTUATION == Character.getType(partOfLine))
|| (Character.isLetter(partOfLine))) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
String fileNameInput = args[0];
String fileNameOutput = args[1];
try (BufferedReader input = new BufferedReader(
new InputStreamReader(new FileInputStream(fileNameInput), "UTF-8"));
BufferedWriter outputFile = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(fileNameOutput), "UTF-8"));) {
int position = 1;
// IntList intList = new IntList();
HashMap<String , int[]> output = new HashMap<>();
String keys = "";
while (input.ready()) {
String line = input.readLine().toLowerCase() + " ";
int i = 0;
String word = "";
while (i < line.length()) {
if (partOfWrord(line.charAt(i))) {
word += line.charAt(i);
} else {
// if (word.length() > 4) {
// String sliceWord = word.substring(2, word.length() - 2);
if (word != ""){
// System.err.println(word);
if ((output.containsKey(word))) {
int[] listValue = output.get(word);
if (listValue.length == output.get(word)[0] + 1){
listValue = IntList.alloc(listValue);
}
listValue[0] = listValue[0] + 1;
listValue[listValue[0]] = position;
output.put(word, listValue);
// position = position + 1;
} else {
int[] listValue = new int[10];
listValue[0] = 1;
listValue[1] = position;
output.put(word, listValue);
keys = keys + word + " ";
// position = position + 1;
}
position = position + 1;
}
// }
word = "";
}
i++;
}
}
keys.strip();
String[] listKeys = keys.split(" ");
// Arrays.sort(listKeys, 0, listKeys.length, new Comparator<String>() {
// public int compare(String s1, String s2) {
// return output.get(s1)[0] - output.get(s2)[0];
// }
// });
if (keys.length() != 0) {
for (int j = 0; j < listKeys.length; j++) {
// System.err.println(output);
// System.err.println(output.get(listKeys[j]));
// outputFile.write(listKeys[j] + " " + Arrays.toString(output.get(listKeys[j])) + "\n");
outputFile.write(listKeys[j] + " ");
for (int i = 0; i < output.get(listKeys[j])[0] + 1; i ++){
outputFile.write(Integer.toString(output.get(listKeys[j])[i]));
if (i != output.get(listKeys[j])[0]){
outputFile.write(" ");
}
// System.err.println("pochemu" + " " + listKeys[j]);
}
outputFile.write("\n");
}
}
} catch (FileNotFoundException e) {
System.err.println("Error File Not found" + e.getMessage());
} catch (UnsupportedEncodingException e) {
System.err.println("Error Unsupported Encoding Exception" + e.getMessage());
} catch (IOException e) {
System.err.println("Error IOE" + e.getMessage());
}
}
}