-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticle.java
More file actions
106 lines (83 loc) · 2.64 KB
/
Copy pathArticle.java
File metadata and controls
106 lines (83 loc) · 2.64 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
/*
* Article.java
*
* A simple blueprint class representing an article from the
* Simple English Wikipedia. An article has a title, a body,
* and a filename that corresponds to its location in the
* on-disk database of articles.
*
* Author: Theresa McNeil (tnmcneil@bu.edu)
* Date: 12.4.16
*/
import java.util.*;
public class Article implements Comparable<Article> {
private String title;
private double cosineSimilarity; // if you need this for the heap
private String body;
private String filename;
public Article(String t, String b) {
this.title = t;
this.body = b;
}
// accessor methods for various fields
public String getTitle() {
return this.title;
}
public String getBody() {
return this.body;
}
public void putCS(double cs) {
this.cosineSimilarity = cs;
}
public double getCS() {
return cosineSimilarity;
}
public String toString() { // does not include the cosine similarity
String t = getTitle();
String s = t + "\n";
for (int i = 0; i < t.length(); i++)
s += "=";
s += "\n";
s += wrapString(getBody());
return s;
}
// standard compareTo for the Comparable interface, uses lexicographic ordering on the titles
public int compareTo(Article other) {
return this.getTitle().compareTo(other.getTitle());
}
// alternate comparison using the cosineSimiliary field
public int compareCS(Article other) {
if (getCS() < other.getCS())
return -1;
else if (getCS() > other.getCS())
return 1;
else
return 0;
}
/*
* Given a string, return a new string with newlines in the
* appropriate places to keep lines less than 80 characters
* long. This method will convert single existing newlines
* to double newlines, to simulate a paragraph break.
*/
private String wrapString(String s) {
String out = "";
String[] lines = s.split("\r\n?|\n");
int cols = 0;
for (int i = 0; i < lines.length; i++) {
String[] words = lines[i].split(" ");
for (int j = 0; j < words.length; j++) {
if (cols + words[j].length() >= 80) {
cols = words[j].length() + 1;
out += "\n" + words[j] + " ";
} else {
cols += words[j].length() + 1;
out += words[j] + " ";
}
}
cols = 0;
out += "\n\n";
}
return out;
}
}