Skip to content

Commit 9f78bb3

Browse files
Merge pull request aimacode#315 from joongwon/AIMA3e
This code can cause a NullPointerException.
2 parents f909c5e + 1316d4c commit 9f78bb3

File tree

2 files changed

+37
-47
lines changed

2 files changed

+37
-47
lines changed

aima-core/src/main/java/aima/core/nlp/parsing/Lexicon.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package 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

105
import 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
}

aima-core/src/main/java/aima/core/nlp/parsing/grammars/Rule.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,17 @@ public Rule( List<String> lhs, float probability ) {
3434

3535
// string split constructor
3636
public Rule( String lhs, String rhs, float probability) {
37-
if( lhs.equals("")) {
38-
this.lhs = new ArrayList<String>();
37+
if( "".equals(lhs)) {
38+
this.lhs = new ArrayList<>();
3939
} else {
40-
this.lhs = new ArrayList<String>(Arrays.asList(lhs.split("\\s*,\\s*")));
40+
this.lhs = new ArrayList<>(Arrays.asList(lhs.split("\\s*,\\s*")));
4141
}
42-
if( rhs.equals("")) {
43-
this.rhs = new ArrayList<String>();
42+
if( "".equals(rhs)) {
43+
this.rhs = new ArrayList<>();
4444
} else {
45-
this.rhs = new ArrayList<String>(Arrays.asList(rhs.split("\\s*,\\s*")));
45+
this.rhs = new ArrayList<>(Arrays.asList(rhs.split("\\s*,\\s*")));
4646
}
4747
this.PROB = validateProb( probability );
48-
4948
}
5049

5150
/**
@@ -74,10 +73,7 @@ public boolean derives( List<String> sentForm ) {
7473
}
7574

7675
public boolean derives( String terminal ) {
77-
if( this.rhs.size() == 1 && this.rhs.get(0).equals(terminal)) {
78-
return true;
79-
}
80-
return false;
76+
return this.rhs.size() == 1 && this.rhs.get(0).equals(terminal);
8177
}
8278

8379
@Override

0 commit comments

Comments
 (0)