This repository has been 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
/
Copy pathTerm.java
104 lines (88 loc) · 2.33 KB
/
Term.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
public class Term {
private String word;
public int docFrequency;
public int totalFrequency;
LinkedList list = new LinkedList();
public Node first;
boolean inList;
double tfidf;
//Construction of a term
public Term(String name){
this.word = name;
docFrequency = 0;
}
public String getWord(){
return word;
}
//increment frequency for terms and add documents to linked list
public void incFrequency(String document){
totalFrequency++;
//loop through list
while(first != null){
//if it is in list, inc frequency and set inList to true
if(first.getData().equals(document)){
//increment total frequency
first.incFrequency();
inList = true;
break;
}
// else it is not in list
else{
inList = false;
}
first = first.getNext();
}
first = list.head;
//if empty, insert occurrence
if(first == null){
first = new Node(document);
inList = true;
list.add(document);
docFrequency++;
}
//if not in list, add to end
if(inList == false){
list.add(document);
docFrequency++;
}
//resets first to front of linked list
first = list.head;
}
public double[] fillTFIDFarray(){
//create array to return
double[] TFIDFarray = new double[docFrequency];
int index = 0;
// loop through linked list of document names
while(first != null){
//calculating TFIDF
tfidf = (float)first.getTermFrequency() * Math.log((float)(WebPages.totalDoc)/(float)(docFrequency));
// casting TFIDF into String from double w/ DecimalFormatter
double tFIDF = tfidf;
// adding TFIDF to array
TFIDFarray[index] = tFIDF;
index++;
first = first.getNext();
}
//set first back to head of linked list
first = this.list.head;
// return list of TFIDF and document names;
return TFIDFarray;
}
public String[] fillDocArray(){
String[] docs = new String[docFrequency];
int index = 0;
first = this.list.head;
// loop through linked list of document names
while(first != null){
//adding document name to array
String document = first.getData();
docs[index] = document;
index++;
first = first.getNext();
}
//set first back to head of linked list
first = this.list.head;
// return list of TFIDF and document names;
return docs;
}
}