This repository was archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerm.java
More file actions
114 lines (100 loc) · 3.17 KB
/
Copy pathTerm.java
File metadata and controls
114 lines (100 loc) · 3.17 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
import java.util.ArrayList;
//TEst comment
public class Term implements Comparable {
//Set 0 to compare alphabetically, set 1 to compare by index
public static int compareType = 0;
//Word parsed from web page
private String name;
//Number of documents the word appears in. NOT a word count
private int docFrequency;
//Array of occurrences, each contain the number of times the word was in a specified document
private LinkedOccurrence docsList = new LinkedOccurrence();
//Constructor
public Term(String name) {
docFrequency = 0;
this.name = name;
}
//Constructor
public Term(String document, String name){
docFrequency = 0;
this.name = name;
addNewOccurrence(document);
}
//Increases words document frequency by 1
public void incFrequency() {
docFrequency++;
}
//Updates docsList by one term occurring in *document*
public void addNewOccurrence(String document) {
//Duplicate checking
boolean add = true;
for(Occurrence occ: docsList.toArray()){
if(occ.getDocName().compareTo(document) == 0){
add = false;
occ.incFrequency();
}
}
if(add){
docsList.insert(document);
incFrequency();
}
}
//Returns the word the term is associated with
public String getName() {
return name;
}
//Returns total count of the word in all documents
public int getTotalFrequency() {
int total = 0;
for (Occurrence occ : docsList.toArray()) {
total += occ.getTermFrequency();
}
return total;
}
public ArrayList<Occurrence> getDocsList() {
return docsList.toArray();
}
@Override
public int compareTo(Object o) {
if(compareType == 0) {
String name1 = this.name;
String name2 = ((Term) o).name;
int size = name1.length();
if (name1.length() > name2.length())
size = name2.length();
for (int i = 0; i < size; i++) {
char word1 = name1.charAt(i);
char word2 = name2.charAt(i);
if (word1 < word2) {
return -1;
}
if (word1 > word2) {
return 1;
}
}
if (name1.length() < name2.length())
return -1;
else
return 1;
}
if(compareType == 1){
Integer count1 = this.getTotalFrequency();
Integer count2 = ((Term) o).getTotalFrequency();
return count1.compareTo(count2);
}
return 0;
}
//Returns frequency of term in specified document
public int getInDocumentFrequency(String document){
for(Occurrence occ: docsList.toArray()){
if(occ.getDocName().compareTo(document) == 0){
return occ.getTermFrequency();
}
}
return -1;
}
//Returns total number of documents the term is in
public int getDocFrequency() {
return docFrequency;
}
}