-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostingsList.java
129 lines (115 loc) · 3.45 KB
/
PostingsList.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
package Indexing;
import Analyzing.Document;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
/**
* This Class is only currently used to create a posting list file to be written
* into storage.
*
* @author rl07bebb
*/
public class PostingsList{
public static ArrayList<Postings> postingsList=new ArrayList<>();
Document doc;
/**
* This Constructor is responsible for creating the post class. The
* constructor takes a document because it will write to this documents file.
*
* @param doc
*/
public PostingsList() {
}
/**
* This Constructor is responsible for creating the post class. The
* constructor takes a document because it will write to this documents file.
*
* @param doc
*/
public PostingsList(Document doc) {
this.doc = doc;
}
public Postings get(int i){
return postingsList.get(i);
}
/**
* This method is just to compare the Indexing.Terms.
*/
private static final Comparator<String> ALPHABETICAL_ORDER = new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
int res = String.CASE_INSENSITIVE_ORDER.compare(str1, str2);
if (res == 0) {
res = str1.compareTo(str2);
}
return res;
}
};
@Override
public String toString(){
String all="";
for(Postings p:postingsList){
all+=p.toString();
}
return all;
}
/**
* This method is responsible for writing the data into the file.
*
* @param s
*/
public void writeList(String s) {
Scanner scanner = null;
PrintWriter writer = null;
try {
scanner = new Scanner(s);
writer = new PrintWriter(doc.getFile(), "UTF-8");
while (scanner.hasNextLine()) {
writer.println(scanner.nextLine() + "\n");
}
} catch (IOException e) {
System.err.println("PostingsList.writeList:Error");
e.toString();
} finally {
scanner.close();
writer.close();
}
}
/**
* This method is responsible for writing the data into the file.
*/
public void writeList(){
try (PrintWriter writer = new PrintWriter(doc.getFile(), "UTF-8")) {
for(Postings post:postingsList){
writer.println(post.toString());
}
} catch (FileNotFoundException| UnsupportedEncodingException e) {
System.err.println("PostingsList.writeList:Error");
e.toString();
}
}
/**
* This method is responsible for writing the data into the file.
* @param sorted
*/
public void writeTermList(File file){
ArrayList<String> sorted=new ArrayList<>();
try (PrintWriter writer = new PrintWriter(file, "UTF-8")) {
for(Postings post:postingsList){
sorted.add(post.terms.get(0).getTerm());
}
sorted.sort(ALPHABETICAL_ORDER);
for(String s:sorted){
writer.println(s.toString());
}
} catch (FileNotFoundException| UnsupportedEncodingException e) {
System.err.println("PostingsList.writeList:Error");
e.toString();
}
}
}