11package edu .stanford .nlp .coref ;
22
3+ import java .io .File ;
34import java .io .FileOutputStream ;
5+ import java .io .IOException ;
46import java .io .PrintWriter ;
57import java .util .Calendar ;
68import java .util .Map ;
79import java .util .Properties ;
10+ import java .util .logging .FileHandler ;
11+ import java .util .logging .Level ;
812import java .util .logging .Logger ;
13+ import java .util .stream .Collectors ;
914
1015import edu .stanford .nlp .coref .data .CorefChain ;
1116import edu .stanford .nlp .coref .data .CorefCluster ;
1520import edu .stanford .nlp .pipeline .Annotation ;
1621import edu .stanford .nlp .util .Generics ;
1722import edu .stanford .nlp .util .StringUtils ;
23+ import edu .stanford .nlp .util .logging .NewlineLogFormatter ;
1824import edu .stanford .nlp .util .logging .Redwood ;
1925
2026/**
@@ -69,16 +75,36 @@ public void annotate(Annotation ann) {
6975 ann .set (CorefCoreAnnotations .CorefChainAnnotation .class , result );
7076 }
7177
78+ public void initLogger (Logger logger , String logFileName ) {
79+ try {
80+ FileHandler fh = new FileHandler (logFileName , false );
81+ logger .addHandler (fh );
82+ logger .setLevel (Level .FINE );
83+ fh .setFormatter (new NewlineLogFormatter ());
84+ } catch (SecurityException | IOException e ) {
85+ throw new RuntimeException ("Cannot initialize logger!" , e );
86+ }
87+ }
88+
7289 public void runOnConll (Properties props ) throws Exception {
73- String baseName = CorefProperties .conllOutputPath (props ) +
74- Calendar .getInstance ().getTime ().toString ().replaceAll ("\\ s" , "-" ).replaceAll (":" , "-" );
90+ File f = new File (CorefProperties .conllOutputPath (props ));
91+ if (! f .exists ()) {
92+ f .mkdirs ();
93+ }
94+ String timestamp = Calendar .getInstance ().getTime ().toString ().replaceAll ("\\ s" , "-" ).replaceAll (":" , "-" );
95+ String baseName = CorefProperties .conllOutputPath (props ) + timestamp ;
7596 String goldOutput = baseName + ".gold.txt" ;
7697 String beforeCorefOutput = baseName + ".predicted.txt" ;
7798 String afterCorefOutput = baseName + ".coref.predicted.txt" ;
7899 PrintWriter writerGold = new PrintWriter (new FileOutputStream (goldOutput ));
79100 PrintWriter writerBeforeCoref = new PrintWriter (new FileOutputStream (beforeCorefOutput ));
80101 PrintWriter writerAfterCoref = new PrintWriter (new FileOutputStream (afterCorefOutput ));
81102
103+ Logger logger = Logger .getLogger (CorefSystem .class .getName ());
104+ initLogger (logger ,baseName + ".log" );
105+ logger .info (timestamp );
106+ logger .info (props .toString ());
107+
82108 (new CorefDocumentProcessor () {
83109 @ Override
84110 public void process (int id , Document document ) {
@@ -94,7 +120,14 @@ public void process(int id, Document document) {
94120 if (verbose ) {
95121 CorefUtils .printHumanReadableCoref (document );
96122 }
97- writerAfterCoref .print (CorefPrinter .printConllOutput (document , false , true ));
123+ if (document .filterMentionSet != null ) {
124+ Map <Integer ,CorefCluster > filteredClusters = document .corefClusters
125+ .values ().stream ().filter (x -> CorefUtils .filterClustersWithMentionSpans (x , document .filterMentionSet ) )
126+ .collect (Collectors .toMap (x -> x .clusterID , x -> x ));
127+ writerAfterCoref .print (CorefPrinter .printConllOutput (document , false , true , filteredClusters ));
128+ } else {
129+ writerAfterCoref .print (CorefPrinter .printConllOutput (document , false , true ));
130+ }
98131 }
99132
100133 @ Override
@@ -106,14 +139,20 @@ public String getName() {
106139 }
107140 }).run (docMaker );
108141
109- Logger logger = Logger .getLogger (CorefSystem .class .getName ());
110142 String summary = CorefScorer .getEvalSummary (CorefProperties .getScorerPath (props ),
111143 goldOutput , beforeCorefOutput );
144+
145+ logger .info ("Before Coref" );
112146 CorefScorer .printScoreSummary (summary , logger , false );
147+ CorefScorer .printScoreSummary (summary , logger , true );
148+ CorefScorer .printFinalConllScore (summary , logger );
149+
113150 summary = CorefScorer .getEvalSummary (CorefProperties .getScorerPath (props ), goldOutput ,
114151 afterCorefOutput );
152+ logger .info ("After Coref" );
153+ CorefScorer .printScoreSummary (summary , logger , false );
115154 CorefScorer .printScoreSummary (summary , logger , true );
116- CorefScorer .printFinalConllScore (summary );
155+ CorefScorer .printFinalConllScore (summary , logger );
117156
118157 writerGold .close ();
119158 writerBeforeCoref .close ();
0 commit comments