11
11
import java .util .Collections ;
12
12
import java .util .LinkedList ;
13
13
import java .util .List ;
14
+ import java .util .function .BiConsumer ;
15
+ import java .util .function .Function ;
14
16
15
17
import edu .stanford .nlp .io .NullOutputStream ;
18
+ import edu .stanford .nlp .io .RuntimeIOException ;
16
19
import edu .stanford .nlp .ling .*;
17
20
import edu .stanford .nlp .ling .SentenceUtils ;
18
21
import edu .stanford .nlp .math .ArrayMath ;
36
39
import edu .stanford .nlp .trees .TreebankLanguagePack ;
37
40
import edu .stanford .nlp .trees .TreePrint ;
38
41
import edu .stanford .nlp .trees .TreeTransformer ;
39
- import java .util .function .Function ;
40
42
import edu .stanford .nlp .util .Generics ;
41
43
import edu .stanford .nlp .util .Pair ;
42
44
import edu .stanford .nlp .util .ScoredObject ;
@@ -557,7 +559,7 @@ else if(pwFileOut != null) {
557
559
* Wrapper for a way to pass in a dataset which may need reprocessing to get parse results
558
560
*/
559
561
public static interface EvaluationDataset {
560
- List < Pair < ParserQuery , Tree >> dataset (PrintWriter pwErr , PrintWriter pwOut , PrintWriter pwFileOut , PrintWriter pwStats , TreePrint treePrint );
562
+ void processDataset (PrintWriter pwErr , PrintWriter pwOut , PrintWriter pwFileOut , PrintWriter pwStats , TreePrint treePrint , BiConsumer < ParserQuery , Tree > processResults );
561
563
562
564
void summarize (PrintWriter pwErr , TreebankLanguagePack tlp );
563
565
}
@@ -603,8 +605,7 @@ private List<CoreLabel> getInputSentence(Tree t) {
603
605
}
604
606
}
605
607
606
- public List <Pair <ParserQuery , Tree >> dataset (PrintWriter pwErr , PrintWriter pwOut , PrintWriter pwFileOut , PrintWriter pwStats , TreePrint treePrint ) {
607
- List <Pair <ParserQuery , Tree >> results = new ArrayList <>();
608
+ public void processDataset (PrintWriter pwErr , PrintWriter pwOut , PrintWriter pwFileOut , PrintWriter pwStats , TreePrint treePrint , BiConsumer <ParserQuery , Tree > processResults ) {
608
609
609
610
if (op .testOptions .testingThreads != 1 ) {
610
611
MulticoreWrapper <List <? extends HasWord >, ParserQuery > wrapper = new MulticoreWrapper <>(op .testOptions .testingThreads , new ParsingThreadsafeProcessor (pqFactory , pwErr ));
@@ -619,28 +620,26 @@ public List<Pair<ParserQuery, Tree>> dataset(PrintWriter pwErr, PrintWriter pwOu
619
620
while (wrapper .peek ()) {
620
621
ParserQuery pq = wrapper .poll ();
621
622
goldTree = goldTrees .poll ();
622
- results . add ( new Pair <>( pq , goldTree ) );
623
+ processResults . accept ( pq , goldTree );
623
624
}
624
625
} // for tree iterator
625
626
wrapper .join ();
626
627
while (wrapper .peek ()) {
627
628
ParserQuery pq = wrapper .poll ();
628
629
Tree goldTree = goldTrees .poll ();
629
- results . add ( new Pair <>( pq , goldTree ) );
630
+ processResults . accept ( pq , goldTree );
630
631
}
631
632
} else {
633
+ ParserQuery pq = pqFactory .parserQuery ();
632
634
for (Tree goldTree : testTreebank ) {
633
635
final List <CoreLabel > sentence = getInputSentence (goldTree );
634
636
635
637
pwErr .println ("Parsing [len. " + sentence .size () + "]: " + SentenceUtils .listToString (sentence ));
636
638
637
- ParserQuery pq = pqFactory .parserQuery ();
638
639
pq .parseAndReport (sentence , pwErr );
639
- results . add ( new Pair <>( pq , goldTree ) );
640
+ processResults . accept ( pq , goldTree );
640
641
} // for tree iterator
641
642
}
642
-
643
- return results ;
644
643
}
645
644
646
645
public void summarize (PrintWriter pwErr , TreebankLanguagePack tlp ) {
@@ -656,8 +655,10 @@ public PreparsedEvaluationDataset(List<Pair<ParserQuery, Tree>> testTreebank) {
656
655
this .testTreebank = testTreebank ;
657
656
}
658
657
659
- public List <Pair <ParserQuery , Tree >> dataset (PrintWriter pwErr , PrintWriter pwOut , PrintWriter pwFileOut , PrintWriter pwStats , TreePrint treePrint ) {
660
- return testTreebank ;
658
+ public void processDataset (PrintWriter pwErr , PrintWriter pwOut , PrintWriter pwFileOut , PrintWriter pwStats , TreePrint treePrint , BiConsumer <ParserQuery , Tree > processResults ) {
659
+ for (Pair <ParserQuery , Tree > result : testTreebank ) {
660
+ processResults .accept (result .first , result .second );
661
+ }
661
662
}
662
663
663
664
public void summarize (PrintWriter pwErr , TreebankLanguagePack tlp ) {
@@ -689,55 +690,53 @@ public double testOnTreebank(EvaluationDataset testTreebank) {
689
690
TreePrint treePrint = op .testOptions .treePrint (op .tlpParams );
690
691
TreebankLangParserParams tlpParams = op .tlpParams ;
691
692
TreebankLanguagePack tlp = op .langpack ();
692
- PrintWriter pwOut , pwErr ;
693
+ PrintWriter pwOut , pwEvalErr ;
693
694
if (op .testOptions .quietEvaluation ) {
694
695
NullOutputStream quiet = new NullOutputStream ();
695
696
pwOut = tlpParams .pw (quiet );
696
- pwErr = tlpParams .pw (quiet );
697
+ pwEvalErr = tlpParams .pw (quiet );
697
698
} else {
698
699
pwOut = tlpParams .pw ();
699
- pwErr = tlpParams .pw (System .err );
700
+ pwEvalErr = tlpParams .pw (System .err );
700
701
}
701
702
if (op .testOptions .verbose ) {
702
- testTreebank .summarize (pwErr , tlp );
703
+ testTreebank .summarize (pwEvalErr , tlp );
703
704
}
704
705
if (op .testOptions .evalb ) {
705
706
EvalbFormatWriter .initEVALBfiles (tlpParams );
706
707
}
707
708
708
- PrintWriter pwFileOut = null ;
709
+ final PrintWriter pwFileOut ;
709
710
if (op .testOptions .writeOutputFiles ) {
710
711
String fname = op .testOptions .outputFilesPrefix + "." + op .testOptions .outputFilesExtension ;
711
712
try {
712
713
pwFileOut = op .tlpParams .pw (new FileOutputStream (fname ));
713
714
} catch (IOException ioe ) {
714
- ioe . printStackTrace ( );
715
+ throw new RuntimeIOException ( ioe );
715
716
}
717
+ } else {
718
+ pwFileOut = null ;
716
719
}
717
720
718
- PrintWriter pwStats = null ;
719
- if (op .testOptions .outputkBestEquivocation != null ) {
721
+ final PrintWriter pwStats ;
722
+ if (op .testOptions .outputkBestEquivocation != null ) {
720
723
try {
721
724
pwStats = op .tlpParams .pw (new FileOutputStream (op .testOptions .outputkBestEquivocation ));
722
725
} catch (IOException ioe ) {
723
- ioe . printStackTrace ( );
726
+ throw new RuntimeIOException ( ioe );
724
727
}
728
+ } else {
729
+ pwStats = null ;
725
730
}
726
731
727
- List <Pair <ParserQuery , Tree >> results = testTreebank .dataset (pwErr , pwOut , pwFileOut , pwStats , treePrint );
728
- for (Pair <ParserQuery , Tree > result : results ) {
729
- ParserQuery pq = result .first ;
730
- Tree goldTree = result .second ;
731
- processResults (pq , goldTree , pwErr , pwOut , pwFileOut , pwStats , treePrint );
732
- }
732
+ testTreebank .processDataset (pwEvalErr , pwOut , pwFileOut , pwStats , treePrint ,
733
+ (pq , goldTree ) -> processResults (pq , goldTree , pwEvalErr , pwOut , pwFileOut , pwStats , treePrint ));
733
734
734
735
//Done parsing...print the results of the evaluations
735
736
if (treebankTotalTimer != null ) {
736
737
treebankTotalTimer .done ("Testing on treebank" );
737
738
}
738
- if (op .testOptions .quietEvaluation ) {
739
- pwErr = tlpParams .pw (System .err );
740
- }
739
+ PrintWriter pwErr = tlpParams .pw (System .err );
741
740
if (saidMemMessage ) {
742
741
ParserUtils .printOutOfMemory (pwErr );
743
742
}
0 commit comments