Skip to content

Commit 367cd71

Browse files
committed
code reuse
1 parent 841afe8 commit 367cd71

File tree

4 files changed

+25
-36
lines changed

4 files changed

+25
-36
lines changed

nltkrest-examples/src/main/java/edu/usc/ir/visualization/Labels.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.ArrayList;
2020
import edu.usc.ir.visualization.Series;
2121

22-
public class Labels{
22+
public class Labels {
2323
ArrayList<String> labels;
2424
Series[] series;
2525
public Labels(ArrayList<String> labels, Series[] series) {

nltkrest-examples/src/main/java/edu/usc/ir/visualization/NLTKandCoreNLP.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.ByteArrayInputStream;
2020
import java.io.File;
21-
import java.io.FileWriter;
2221
import java.io.IOException;
2322
import java.io.InputStream;
2423
import java.nio.charset.StandardCharsets;
@@ -30,7 +29,6 @@
3029
import java.util.HashSet;
3130
import java.util.Iterator;
3231

33-
import javax.ws.rs.core.Response;
3432
import javax.ws.rs.core.MediaType;
3533
import org.apache.cxf.jaxrs.client.WebClient;
3634
import org.apache.tika.Tika;
@@ -58,7 +56,8 @@ public class NLTKandCoreNLP {
5856
private Metadata md;
5957
private ObjectMapper mapper;
6058

61-
public NLTKandCoreNLP(){
59+
public NLTKandCoreNLP() {
60+
6261
freq = new HashSet<String>();
6362
nltk = new HashMap<String,Integer>();
6463
nlp = new HashMap<String,Integer>();
@@ -74,15 +73,13 @@ public NLTKandCoreNLP(){
7473
}
7574
}
7675

77-
public static void main(String m[]) throws JsonParseException, JsonMappingException, IOException{
76+
public static void main(String m[]) throws JsonParseException, JsonMappingException, IOException {
7877

7978
String memexUrl = m[0];
8079
String username = m[1];
8180
String password = m[2];
8281
File destination =new File(m[3]);
8382
NLTKandCoreNLP obj = new NLTKandCoreNLP();
84-
85-
//count frequency of entities extracted using NLTK and CoreNLP
8683
obj.countNER(memexUrl, username, password);
8784
obj.createJSON(destination);
8885

@@ -95,8 +92,8 @@ private void countNER(String memexUrl, String username, String password) throws
9592
String url;
9693
String response;
9794

98-
for(int c=0; c<101; c+=100)
99-
{
95+
for (int c=0; c<101; c+=100) {
96+
10097
url = memexUrl + "/select?q=gunsamerica&start="+c+"&rows=100&fl=content%2Corganizations%2Cpersons%2Cdates%2Clocations&wt=json&indent=true";
10198
response = WebClient
10299
.create(url, username, password, null)
@@ -158,57 +155,48 @@ private void countNER(String memexUrl, String username, String password) throws
158155
}
159156
}
160157

161-
private void extract(String ner) throws JsonParseException, JsonMappingException, IOException{
158+
private void extract(String ner) throws JsonParseException, JsonMappingException, IOException {
162159
String names[]=null;
163160
names= mapper.readValue(datasetElement.get(ner).toString(),String[].class);
164-
for(int i=0; i<names.length; i++){
165-
if(!freq.contains(names[i])){
161+
for(int i=0; i<names.length; i++) {
162+
if(!freq.contains(names[i])) {
166163
freq.add(names[i]);
167164
}
168-
if(nlp.containsKey(names[i])){
165+
if(nlp.containsKey(names[i])) {
169166
nlp.put(names[i], nlp.get(names[i]) + 1);
170-
}
171-
else{
167+
} else {
172168
nlp.put(names[i], 1);
173169
}
174170
}
175171
}
176172

177173
private void createJSON(File destination) throws JsonGenerationException, JsonMappingException, IOException {
178174
ArrayList<Names> frequencies = new ArrayList<Names>();
179-
for(String val:freq){
180-
int x=0;
181-
int y=0;
182-
if(nltk.containsKey(val)){
183-
x = nltk.get(val);
184-
}
185-
if(nlp.containsKey(val)){
186-
y = nlp.get(val);
187-
}
175+
for (String value:freq) {
176+
int x = nltk.containsKey(value)?nltk.get(value):0;
177+
int y = nlp.containsKey(value)?nlp.get(value):0;
188178
int z = x+y-Math.abs(x-y);
189-
if(z==0){
179+
if (z==0) {
190180
z = x>y?0:-y;
191181
}
192-
frequencies.add(new Names(val, z ));
182+
frequencies.add(new Names(value, z ));
193183
}
194184

195185
Collections.sort(frequencies, maximumOverlap);
196186
ArrayList<String> final_labels = new ArrayList<String>();
197187
ArrayList<Integer> nltk_value = new ArrayList<Integer>();
198188
ArrayList<Integer> nlp_value = new ArrayList<Integer>();
199-
for(int i=0; i<frequencies.size(); i++){
189+
for (int i=0; i<frequencies.size(); i++) {
200190
String value = frequencies.get(i).name;
201191
final_labels.add(value);
202-
if(nltk.containsKey(value)){
192+
if (nltk.containsKey(value)) {
203193
nltk_value.add(nltk.get(value));
204-
}
205-
else{
194+
} else {
206195
nltk_value.add(0);
207196
}
208-
if(nlp.containsKey(value)){
197+
if (nlp.containsKey(value)) {
209198
nlp_value.add(nlp.get(value));
210-
}
211-
else{
199+
} else {
212200
nlp_value.add(0);
213201
}
214202
}
@@ -221,7 +209,7 @@ private void createJSON(File destination) throws JsonGenerationException, JsonMa
221209
System.out.println("Json ready for Visualization: " + destination.getAbsolutePath());
222210
}
223211

224-
public static Comparator<Names> maximumOverlap = new Comparator<Names>(){
212+
public static Comparator<Names> maximumOverlap = new Comparator<Names>() {
225213
public int compare(Names one, Names two) {
226214
return (int)two.strength - (int)one.strength;
227215
}

nltkrest-examples/src/main/java/edu/usc/ir/visualization/Names.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
public class Names {
2020
String name;
2121
int strength;
22-
Names(String x, int y){
22+
23+
Names(String x, int y) {
2324
name=x;
2425
strength=y;
2526
}

nltkrest-examples/src/main/java/edu/usc/ir/visualization/Series.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class Series {
2222
String name;
2323
ArrayList<Integer> value;
24-
Series(String name, ArrayList<Integer> value ){
24+
Series(String name, ArrayList<Integer> value ) {
2525
this.name=name;
2626
this.value=value;
2727
}

0 commit comments

Comments
 (0)