1818
1919import java .io .ByteArrayInputStream ;
2020import java .io .File ;
21- import java .io .FileWriter ;
2221import java .io .IOException ;
2322import java .io .InputStream ;
2423import java .nio .charset .StandardCharsets ;
3029import java .util .HashSet ;
3130import java .util .Iterator ;
3231
33- import javax .ws .rs .core .Response ;
3432import javax .ws .rs .core .MediaType ;
3533import org .apache .cxf .jaxrs .client .WebClient ;
3634import 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 }
0 commit comments