Skip to content

Commit 653e4ff

Browse files
author
Thomas L. Redman
committed
Added a modicum of error handling to bits of code around what I touched.
1 parent 1e896a6 commit 653e4ff

File tree

5 files changed

+133
-71
lines changed

5 files changed

+133
-71
lines changed

md/src/main/java/org/cogcomp/md/BIOCombinedReader.java

+31-26
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
import edu.illinois.cs.cogcomp.nlp.corpusreaders.ereReader.EREDocumentReader;
2121
import edu.illinois.cs.cogcomp.nlp.corpusreaders.ereReader.EREMentionRelationReader;
2222
import edu.illinois.cs.cogcomp.pos.POSAnnotator;
23+
import io.minio.errors.InvalidEndpointException;
24+
import io.minio.errors.InvalidPortException;
25+
import net.didion.jwnl.JWNLException;
26+
2327
import org.cogcomp.Datastore;
28+
import org.cogcomp.DatastoreException;
2429

2530
import java.io.*;
2631
import java.util.*;
@@ -46,8 +51,13 @@ public class BIOCombinedReader extends BIOReader {
4651
* @param mode Indicates the corpus and train/eval e.g. "ERE-TRAIN"
4752
* mode "ALL-TRAIN/EVAL" indicates hybrid corpus.
4853
* @param type Indicates the type (NAM/NOM/PRO/ALL) kept
54+
* @throws DatastoreException
55+
* @throws JWNLException
56+
* @throws IOException
57+
* @throws InvalidEndpointException
58+
* @throws InvalidPortException
4959
*/
50-
public BIOCombinedReader(int fold, String mode, String type){
60+
public BIOCombinedReader(int fold, String mode, String type) throws InvalidPortException, InvalidEndpointException, IOException, JWNLException, DatastoreException{
5161
_mode = mode;
5262
_type = type;
5363
_taOnly = false;
@@ -58,7 +68,7 @@ public BIOCombinedReader(int fold, String mode, String type){
5868
id = "Hybrid_" + fold;
5969
}
6070

61-
public BIOCombinedReader(int fold, String mode, String type, Boolean taOnly){
71+
public BIOCombinedReader(int fold, String mode, String type, Boolean taOnly) throws InvalidPortException, InvalidEndpointException, IOException, JWNLException, DatastoreException{
6272
_mode = mode;
6373
_type = type;
6474
_taOnly = taOnly;
@@ -135,34 +145,29 @@ else if (mode.contains("ALL")){
135145
}
136146
return ret;
137147
}
138-
private List<Constituent> getTokensFromTAs(){
148+
private List<Constituent> getTokensFromTAs() throws InvalidPortException, InvalidEndpointException, IOException, JWNLException, DatastoreException {
139149
List<Constituent> ret = new ArrayList<>();
140150
WordNetManager wordNet = null;
141151
Gazetteers gazetteers = null;
142152
BrownClusters brownClusters = null;
143-
try {
144-
Datastore ds = new Datastore(new ResourceConfigurator().getDefaultConfig());
145-
File gazetteersResource = ds.getDirectory("org.cogcomp.gazetteers", "gazetteers", 1.3, false);
146-
gazetteers = GazetteersFactory.get(5, gazetteersResource.getPath() + File.separator + "gazetteers", true, Language.English);
147-
Vector<String> bcs = new Vector<>();
148-
bcs.add("brown-clusters/brown-english-wikitext.case-intact.txt-c1000-freq10-v3.txt");
149-
bcs.add("brown-clusters/brownBllipClusters");
150-
bcs.add("brown-clusters/brown-rcv1.clean.tokenized-CoNLL03.txt-c1000-freq1.txt");
151-
Vector<Integer> bcst = new Vector<>();
152-
bcst.add(5);
153-
bcst.add(5);
154-
bcst.add(5);
155-
Vector<Boolean> bcsl = new Vector<>();
156-
bcsl.add(false);
157-
bcsl.add(false);
158-
bcsl.add(false);
159-
brownClusters = BrownClusters.get(bcs, bcst, bcsl);
160-
WordNetManager.loadConfigAsClasspathResource(true);
161-
wordNet = WordNetManager.getInstance();
162-
}
163-
catch (Exception e){
164-
e.printStackTrace();
165-
}
153+
Datastore ds = new Datastore(new ResourceConfigurator().getDefaultConfig());
154+
File gazetteersResource = ds.getDirectory("org.cogcomp.gazetteers", "gazetteers", 1.3, false);
155+
gazetteers = GazetteersFactory.get(5, gazetteersResource.getPath() + File.separator + "gazetteers", true, Language.English);
156+
Vector<String> bcs = new Vector<>();
157+
bcs.add("brown-clusters/brown-english-wikitext.case-intact.txt-c1000-freq10-v3.txt");
158+
bcs.add("brown-clusters/brownBllipClusters");
159+
bcs.add("brown-clusters/brown-rcv1.clean.tokenized-CoNLL03.txt-c1000-freq1.txt");
160+
Vector<Integer> bcst = new Vector<>();
161+
bcst.add(5);
162+
bcst.add(5);
163+
bcst.add(5);
164+
Vector<Boolean> bcsl = new Vector<>();
165+
bcsl.add(false);
166+
bcsl.add(false);
167+
bcsl.add(false);
168+
brownClusters = BrownClusters.get(bcs, bcst, bcsl);
169+
WordNetManager.loadConfigAsClasspathResource(true);
170+
wordNet = WordNetManager.getInstance();
166171
for (TextAnnotation ta : currentTas){
167172
View tokenView = ta.getView(ViewNames.TOKENS);
168173
String mentionViewName = "";

md/src/main/java/org/cogcomp/md/BIOReader.java

+36-26
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@
2222
import edu.illinois.cs.cogcomp.nlp.corpusreaders.ereReader.EREDocumentReader;
2323
import edu.illinois.cs.cogcomp.nlp.corpusreaders.ereReader.EREMentionRelationReader;
2424
import edu.illinois.cs.cogcomp.pos.POSAnnotator;
25+
import io.minio.errors.InvalidEndpointException;
26+
import io.minio.errors.InvalidPortException;
27+
import net.didion.jwnl.JWNLException;
28+
2529
import org.cogcomp.Datastore;
30+
import org.cogcomp.DatastoreException;
2631

2732
import java.io.File;
33+
import java.io.IOException;
2834
import java.util.ArrayList;
2935
import java.util.List;
3036
import java.util.Vector;
@@ -71,8 +77,13 @@ public BIOReader(){
7177
* "PRO" -> Only pronouns
7278
* "ALL" -> All mentions
7379
* @param isBIO Indicates if the tagging schema is "BIO" or "BIOLU"
80+
* @throws JWNLException
81+
* @throws IOException
82+
* @throws DatastoreException
83+
* @throws InvalidEndpointException
84+
* @throws InvalidPortException
7485
*/
75-
public BIOReader(String path, String mode, String type, Boolean isBIO){
86+
public BIOReader(String path, String mode, String type, Boolean isBIO) {
7687
_path = path;
7788
_mode = mode.split("-")[0];
7889
_binary_indicator = mode.split("-")[1];
@@ -83,7 +94,11 @@ public BIOReader(String path, String mode, String type, Boolean isBIO){
8394
id = group + "_" + type;
8495
taList = getTextAnnotations();
8596
annotateTas();
86-
tokenList = getTokensFromTAs();
97+
try {
98+
tokenList = getTokensFromTAs();
99+
} catch (Throwable t) {
100+
throw new RuntimeException("Tokens could not be reproduced form the text annotations.",t);
101+
}
87102
}
88103

89104
public List<TextAnnotation> getTextAnnotations(){
@@ -137,34 +152,29 @@ private void annotateTas(){
137152
}
138153
}
139154

140-
private List<Constituent> getTokensFromTAs(){
155+
private List<Constituent> getTokensFromTAs() throws InvalidPortException, InvalidEndpointException, DatastoreException, IOException, JWNLException{
141156
List<Constituent> ret = new ArrayList<>();
142157
WordNetManager wordNet = null;
143158
Gazetteers gazetteers = null;
144159
BrownClusters brownClusters = null;
145-
try {
146-
Datastore ds = new Datastore(new ResourceConfigurator().getDefaultConfig());
147-
File gazetteersResource = ds.getDirectory("org.cogcomp.gazetteers", "gazetteers", 1.3, false);
148-
gazetteers = GazetteersFactory.get(5, gazetteersResource.getPath() + File.separator + "gazetteers", true, Language.English);
149-
Vector<String> bcs = new Vector<>();
150-
bcs.add("brown-clusters" + File.separator + "brown-english-wikitext.case-intact.txt-c1000-freq10-v3.txt");
151-
bcs.add("brown-clusters" + File.separator + "brownBllipClusters");
152-
bcs.add("brown-clusters" + File.separator + "brown-rcv1.clean.tokenized-CoNLL03.txt-c1000-freq1.txt");
153-
Vector<Integer> bcst = new Vector<>();
154-
bcst.add(5);
155-
bcst.add(5);
156-
bcst.add(5);
157-
Vector<Boolean> bcsl = new Vector<>();
158-
bcsl.add(false);
159-
bcsl.add(false);
160-
bcsl.add(false);
161-
brownClusters = BrownClusters.get(bcs, bcst, bcsl);
162-
WordNetManager.loadConfigAsClasspathResource(true);
163-
wordNet = WordNetManager.getInstance();
164-
}
165-
catch (Exception e){
166-
e.printStackTrace();
167-
}
160+
Datastore ds = new Datastore(new ResourceConfigurator().getDefaultConfig());
161+
File gazetteersResource = ds.getDirectory("org.cogcomp.gazetteers", "gazetteers", 1.3, false);
162+
gazetteers = GazetteersFactory.get(5, gazetteersResource.getPath() + File.separator + "gazetteers", true, Language.English);
163+
Vector<String> bcs = new Vector<>();
164+
bcs.add("brown-clusters" + File.separator + "brown-english-wikitext.case-intact.txt-c1000-freq10-v3.txt");
165+
bcs.add("brown-clusters" + File.separator + "brownBllipClusters");
166+
bcs.add("brown-clusters" + File.separator + "brown-rcv1.clean.tokenized-CoNLL03.txt-c1000-freq1.txt");
167+
Vector<Integer> bcst = new Vector<>();
168+
bcst.add(5);
169+
bcst.add(5);
170+
bcst.add(5);
171+
Vector<Boolean> bcsl = new Vector<>();
172+
bcsl.add(false);
173+
bcsl.add(false);
174+
bcsl.add(false);
175+
brownClusters = BrownClusters.get(bcs, bcst, bcsl);
176+
WordNetManager.loadConfigAsClasspathResource(true);
177+
wordNet = WordNetManager.getInstance();
168178

169179
String mentionViewName = "";
170180
if (_mode.equals("ACE05")){

md/src/main/java/org/cogcomp/md/BIOTester.java

+34-8
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
import edu.illinois.cs.cogcomp.nlp.corpusreaders.ACEReaderWithTrueCaseFixer;
2525
import edu.illinois.cs.cogcomp.nlp.corpusreaders.ereReader.EREDocumentReader;
2626
import edu.illinois.cs.cogcomp.nlp.corpusreaders.ereReader.EREMentionRelationReader;
27+
import io.minio.errors.InvalidEndpointException;
28+
import io.minio.errors.InvalidPortException;
29+
import net.didion.jwnl.JWNLException;
30+
31+
import org.cogcomp.DatastoreException;
2732
import org.cogcomp.md.LbjGen.bio_classifier_nam;
2833
import org.cogcomp.md.LbjGen.bio_classifier_nom;
2934
import org.cogcomp.md.LbjGen.bio_classifier_pro;
3035
import org.cogcomp.md.LbjGen.bio_label;
3136

37+
import java.io.IOException;
3238
import java.lang.reflect.Method;
3339
import java.util.ArrayList;
3440
import java.util.HashMap;
@@ -364,8 +370,13 @@ public static Constituent getConstituent(Constituent curToken, Classifier classi
364370

365371
/**
366372
* Cross Validation tester
373+
* @throws DatastoreException
374+
* @throws JWNLException
375+
* @throws IOException
376+
* @throws InvalidEndpointException
377+
* @throws InvalidPortException
367378
*/
368-
public static void test_cv(){
379+
public static void test_cv() throws InvalidPortException, InvalidEndpointException, IOException, JWNLException, DatastoreException{
369380
boolean isBIO = false;
370381
int total_labeled_mention = 0;
371382
int total_predicted_mention = 0;
@@ -468,8 +479,13 @@ public static void test_cv(){
468479

469480
/**
470481
* Test set tester
482+
* @throws JWNLException
483+
* @throws IOException
484+
* @throws DatastoreException
485+
* @throws InvalidEndpointException
486+
* @throws InvalidPortException
471487
*/
472-
public static void test_ts(){
488+
public static void test_ts() throws InvalidPortException, InvalidEndpointException, DatastoreException, IOException, JWNLException{
473489
boolean isBIO = false;
474490
int total_labeled_mention = 0;
475491
int total_predicted_mention = 0;
@@ -582,8 +598,13 @@ public static void test_ts(){
582598

583599
/**
584600
* ERE corpus tester
601+
* @throws JWNLException
602+
* @throws IOException
603+
* @throws DatastoreException
604+
* @throws InvalidEndpointException
605+
* @throws InvalidPortException
585606
*/
586-
public static void test_ere(){
607+
public static void test_ere() throws InvalidPortException, InvalidEndpointException, DatastoreException, IOException, JWNLException{
587608
int total_labeled_mention = 0;
588609
int total_predicted_mention = 0;
589610
int total_correct_mention = 0;
@@ -655,7 +676,7 @@ public static void test_ere(){
655676
System.out.println("F1: " + f);
656677
}
657678

658-
public static void test_tac(){
679+
public static void test_tac() throws InvalidPortException, InvalidEndpointException, DatastoreException, IOException, JWNLException{
659680
int total_labeled_mention = 0;
660681
int total_predicted_mention = 0;
661682
int total_correct_mention = 0;
@@ -744,8 +765,13 @@ public static void calculateAvgMentionLength(){
744765
/**
745766
* Test the model trained on hybrid ACE/ERE and evaluated on hybrid ACE/ERE
746767
* Produce results on separate types
768+
* @throws DatastoreException
769+
* @throws JWNLException
770+
* @throws IOException
771+
* @throws InvalidEndpointException
772+
* @throws InvalidPortException
747773
*/
748-
public static void test_hybrid(){
774+
public static void test_hybrid() throws InvalidPortException, InvalidEndpointException, IOException, JWNLException, DatastoreException{
749775
int total_labeled_mention = 0;
750776
int total_predicted_mention = 0;
751777
int total_correct_mention = 0;
@@ -951,7 +977,7 @@ public static void statistics(){
951977
System.out.println("TAC_NOM: " + tac_nom);
952978
}
953979

954-
public static void TrainModel(String corpus){
980+
public static void TrainModel(String corpus) throws InvalidPortException, InvalidEndpointException, DatastoreException, IOException, JWNLException{
955981
if (corpus.equals("ACE")) {
956982
Parser train_parser_nam = new BIOReader(getPath("all", "ACE", 0), "ACE05-TRAIN", "NAM", false);
957983
Parser train_parser_nom = new BIOReader(getPath("all", "ACE", 0), "ACE05-TRAIN", "NOM", false);
@@ -970,11 +996,11 @@ else if (corpus.equals("ERE")){
970996
}
971997
}
972998

973-
public static void TrainACEModel(){
999+
public static void TrainACEModel() throws InvalidPortException, InvalidEndpointException, DatastoreException, IOException, JWNLException{
9741000
TrainModel("ACE");
9751001
}
9761002

977-
public static void TrainEREModel(){
1003+
public static void TrainEREModel() throws InvalidPortException, InvalidEndpointException, DatastoreException, IOException, JWNLException{
9781004
TrainModel("ERE");
9791005
}
9801006

md/src/main/java/org/cogcomp/md/ExtentReader.java

+20-5
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121
import edu.illinois.cs.cogcomp.nlp.corpusreaders.ereReader.EREDocumentReader;
2222
import edu.illinois.cs.cogcomp.nlp.corpusreaders.ereReader.EREMentionRelationReader;
2323
import edu.illinois.cs.cogcomp.pos.POSAnnotator;
24+
import io.minio.errors.InvalidEndpointException;
25+
import io.minio.errors.InvalidPortException;
26+
import net.didion.jwnl.JWNLException;
27+
2428
import org.cogcomp.Datastore;
29+
import org.cogcomp.DatastoreException;
2530

2631
import java.io.File;
32+
import java.io.IOException;
2733
import java.util.ArrayList;
2834
import java.util.List;
2935
import java.util.Vector;
@@ -46,8 +52,13 @@ public class ExtentReader implements Parser
4652
*
4753
* @param path The data pth
4854
* @param corpus The corpus "ACE/ERE"
55+
* @throws DatastoreException
56+
* @throws JWNLException
57+
* @throws IOException
58+
* @throws InvalidEndpointException
59+
* @throws InvalidPortException
4960
*/
50-
public ExtentReader(String path, String corpus){
61+
public ExtentReader(String path, String corpus) throws InvalidPortException, InvalidEndpointException, IOException, JWNLException, DatastoreException{
5162
_path = path;
5263
_corpus = corpus;
5364
taList = getTextAnnotations();
@@ -57,11 +68,15 @@ public ExtentReader(String path, String corpus){
5768
/**
5869
* When no corpus is selected, it is set to "ACE"
5970
*/
60-
public ExtentReader(String path){
71+
public ExtentReader(String path) {
6172
_path = path;
6273
_corpus = "ACE";
63-
taList = getTextAnnotations();
64-
pairList = getPairs();
74+
try {
75+
taList = getTextAnnotations();
76+
pairList = getPairs();
77+
} catch (Throwable t) {
78+
throw new RuntimeException("TextAnnotation generation failed",t);
79+
}
6580
}
6681

6782
/**
@@ -74,7 +89,7 @@ public String getId(){
7489
return ret;
7590
}
7691

77-
public List<TextAnnotation> getTextAnnotations(){
92+
public List<TextAnnotation> getTextAnnotations() throws InvalidPortException, InvalidEndpointException, IOException, JWNLException, DatastoreException{
7893
List<TextAnnotation> ret = new ArrayList<>();
7994
if (_corpus.equals("ACE")) {
8095
ACEReaderWithTrueCaseFixer aceReader = null;

0 commit comments

Comments
 (0)