11package aima .core .nlp .parsing ;
22
3- import java .util .ArrayList ;
4- import java .util .Arrays ;
5- import java .util .HashMap ;
6- import java .util .Iterator ;
7- import java .util .Map ;
8- import java .util .Set ;
3+ import java .util .*;
94
105import aima .core .nlp .parsing .grammars .Rule ;
116
@@ -24,27 +19,26 @@ public class Lexicon extends HashMap<String,ArrayList<LexWord>> {
2419 private static final long serialVersionUID = 1L ;
2520
2621 public ArrayList <Rule > getTerminalRules ( String partOfSpeech ) {
27- ArrayList < LexWord > lexWords = this . get ( partOfSpeech .toUpperCase () );
28- ArrayList <Rule > rules = new ArrayList <Rule >();
29- if ( lexWords . size () > 0 ) {
30- for ( int i = 0 ; i < lexWords . size (); i ++ ) {
31- rules . add ( new Rule ( partOfSpeech . toUpperCase (),
32- lexWords . get ( i ). word ,
33- lexWords . get ( i ). prob ));
34- }
35- }
22+ final String partOfSpeechUpperCase = partOfSpeech .toUpperCase ();
23+ final ArrayList <Rule > rules = new ArrayList <>();
24+
25+ Optional . ofNullable ( this . get ( partOfSpeechUpperCase )). ifPresent ( lexWords -> {
26+ for ( LexWord word : lexWords ) {
27+ rules . add ( new Rule ( partOfSpeechUpperCase , word . word , word . prob ));
28+ }
29+ });
30+
3631 return rules ;
3732 }
3833
3934 public ArrayList <Rule > getAllTerminalRules () {
40- ArrayList <Rule > allRules = new ArrayList <Rule >();
41- Set <String > keys = this .keySet ();
42- Iterator <String > it = keys .iterator ();
43- while ( it .hasNext () ) {
44- String key = (String ) it .next ();
35+ final ArrayList <Rule > allRules = new ArrayList <>();
36+ final Set <String > keys = this .keySet ();
37+
38+ for (String key : keys ) {
4539 allRules .addAll ( this .getTerminalRules (key ));
4640 }
47-
41+
4842 return allRules ;
4943 }
5044
@@ -53,21 +47,21 @@ public boolean addEntry( String category, String word, float prob ) {
5347 this .get (category ).add ( new LexWord ( word , prob ));
5448 }
5549 else {
56- this .put (category , new ArrayList <LexWord >( Arrays .asList (new LexWord (word ,prob ))));
50+ this .put (category , new ArrayList <>( Arrays .asList (new LexWord (word ,prob ))));
5751 }
5852
5953 return true ;
6054 }
6155
6256 public boolean addLexWords ( String ... vargs ) {
63-
64- String key ; ArrayList <LexWord > lexWords = new ArrayList <LexWord >();
57+ ArrayList <LexWord > lexWords = new ArrayList <>();
6558 boolean containsKey = false ;
6659 // number of arguments must be key (1) + lexWord pairs ( x * 2 )
6760 if ( vargs .length % 2 != 1 ) {
6861 return false ;
6962 }
70- key = vargs [0 ].toUpperCase ();
63+
64+ String key = vargs [0 ].toUpperCase ();
7165 if ( this .containsKey (key )) { containsKey = true ; }
7266
7367 for ( int i =1 ; i < vargs .length ; i ++ ) {
@@ -92,19 +86,19 @@ public boolean addLexWords( String... vargs ) {
9286 /**
9387 * Add words to an lexicon from an existing lexicon. Using this
9488 * you can combine lexicons.
95- * @param l
89+ * @param lexicon
9690 */
97- public void addLexWords ( Lexicon l ) {
98- Iterator <Map .Entry <String ,ArrayList <LexWord >>> it = l .entrySet ().iterator ();
99- while (it .hasNext ()) {
100- Map .Entry <String ,ArrayList <LexWord >> pair = it .next ();
101- if ( this .containsKey ( pair .getKey ())) {
102- for ( int i =0 ; i < pair .getValue ().size (); i ++ ) {
103- this .get (pair .getKey ()).add (pair .getValue ().get (i ));
91+ public void addLexWords ( Lexicon lexicon ) {
92+ for (Map .Entry <String , ArrayList <LexWord >> pair : lexicon .entrySet ()) {
93+ final String key = pair .getKey ();
94+ final ArrayList <LexWord > lexWords = pair .getValue ();
95+
96+ if (this .containsKey (key )) {
97+ for (LexWord word : lexWords ) {
98+ this .get (key ).add (word );
10499 }
105- }
106- else {
107- this .put (pair .getKey (), pair .getValue ());
100+ } else {
101+ this .put (key , lexWords );
108102 }
109103 }
110104 }
0 commit comments