Skip to content

Commit 3104452

Browse files
committed
Cleaned up a bit.
- Suspicious indices changed. - Whitespace usage standardized. - for replaced by foreach.
1 parent babada9 commit 3104452

File tree

9 files changed

+151
-219
lines changed

9 files changed

+151
-219
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,28 @@
4444
*/
4545
public class CYK {
4646

47-
public float[][][] parse( List<String> words, ProbCNFGrammar grammar ) {
47+
public float[][][] parse(List<String> words, ProbCNFGrammar grammar) {
4848
final int N = length(words);
4949
final int M = grammar.vars.size();
5050
float[][][] P = new float[M][N][N]; // initialised to 0.0
51-
for( int i=0; i < N; i++ ) {
51+
for (int i=0; i < N; i++) {
5252
//for each rule of form( X -> words<sub>i</sub>[p]) do
5353
// P[X,i,1] <- p
54-
for( int j=0; j < grammar.rules.size(); j++ ) {
54+
for (int j=0; j < grammar.rules.size(); j++) {
5555
Rule r = (Rule) grammar.rules.get(j);
5656
if( r.derives(words.get(i))) { // rule is of form X -> w, where w = words[i]
5757
int x = grammar.vars.indexOf(r.lhs.get(0)); // get the index of rule's LHS variable
5858
P[x][i][0] = r.PROB; // not P[X][i][1] because we use 0-based indexing
5959
}
6060
}
6161
}
62-
for( int length=2; length <= N; length++ ) {
63-
for( int start=1; start <= N - length + 1; start++ ) {
64-
for( int len1=1; len1 <= length -1; len1++ ) { // N.B. the book incorrectly has N-1 instead of length-1
62+
for (int length=2; length <= N; length++) {
63+
for (int start=1; start <= N - length + 1; start++) {
64+
for (int len1=1; len1 <= length -1; len1++) { // N.B. the book incorrectly has N-1 instead of length-1
6565
int len2 = length - len1;
6666
// for each rule of the form X -> Y Z, where Y,Z are variables of the grammar
67-
for( int j=0; j < grammar.rules.size(); j++ ) {
68-
Rule r = grammar.rules.get(j);
69-
if( r.rhs.size() == 2 ) {
67+
for (Rule r : grammar.rules) {
68+
if(r.rhs.size() == 2) {
7069
// get index of rule's variables X, Y, and Z
7170
int x = grammar.vars.indexOf(r.lhs.get(0));
7271
int y = grammar.vars.indexOf(r.rhs.get(0));
@@ -87,7 +86,7 @@ public float[][][] parse( List<String> words, ProbCNFGrammar grammar ) {
8786
* @param ls
8887
* @return the length of the list
8988
*/
90-
public int length( List<String> ls ) {
89+
public int length(List<String> ls) {
9190
return ls.size();
9291
}
9392

@@ -97,17 +96,16 @@ public int length( List<String> ls ) {
9796
* @param words
9897
* @param g
9998
*/
100-
public void printProbTable( float[][][] probTable, List<String> words, ProbUnrestrictedGrammar g ) {
99+
public void printProbTable(float[][][] probTable, List<String> words, ProbUnrestrictedGrammar g) {
101100
final int N = words.size();
102101
final int M = g.vars.size(); // num non-terminals in grammar
103102

104-
for( int i=0; i < M; i++ ) {
103+
for (int i=0; i < M; i++) {
105104
System.out.println("Table For : " + g.vars.get(i) + "(" + i + ")");
106-
for( int j=0; j < N; j++ ) {
105+
for (int j=0; j < N; j++) {
107106
System.out.print(j + "| ");
108-
for( int k=0; k < N; k++ ) {
107+
for (int k=0; k < N; k++)
109108
System.out.print(probTable[i][j][k] + " | ");
110-
}
111109
System.out.println();
112110
}
113111
System.out.println();
@@ -121,7 +119,7 @@ public void printProbTable( float[][][] probTable, List<String> words, ProbUnres
121119
* @param g
122120
* @return
123121
*/
124-
public ArrayList<String> getMostProbableDerivation( float[][][] probTable, ProbUnrestrictedGrammar g ) {
122+
public ArrayList<String> getMostProbableDerivation(float[][][] probTable, ProbUnrestrictedGrammar g) {
125123
// TODO
126124
return null;
127125
}

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

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ public class Lexicon extends HashMap<String,ArrayList<LexWord>> {
1818

1919
private static final long serialVersionUID = 1L;
2020

21-
public ArrayList<Rule> getTerminalRules( String partOfSpeech ) {
21+
public ArrayList<Rule> getTerminalRules(String partOfSpeech) {
2222
final String partOfSpeechUpperCase = partOfSpeech.toUpperCase();
2323
final ArrayList<Rule> rules = new ArrayList<>();
2424

2525
Optional.ofNullable(this.get(partOfSpeechUpperCase)).ifPresent(lexWords -> {
26-
for (LexWord word : lexWords) {
26+
for (LexWord word : lexWords)
2727
rules.add(new Rule(partOfSpeechUpperCase, word.word, word.prob));
28-
}
2928
});
3029

3130
return rules;
@@ -35,50 +34,44 @@ public ArrayList<Rule> getAllTerminalRules() {
3534
final ArrayList<Rule> allRules = new ArrayList<>();
3635
final Set<String> keys = this.keySet();
3736

38-
for (String key : keys) {
37+
for (String key : keys)
3938
allRules.addAll( this.getTerminalRules(key));
40-
}
4139

4240
return allRules;
4341
}
4442

4543
public boolean addEntry( String category, String word, float prob ) {
46-
if( this.containsKey(category)) {
44+
if( this.containsKey(category))
4745
this.get(category).add( new LexWord( word, prob ));
48-
}
49-
else {
50-
this.put(category, new ArrayList<>( Arrays.asList(new LexWord(word,prob))));
51-
}
46+
else
47+
this.put(category, new ArrayList<>(Collections.singletonList(new LexWord(word,prob))));
5248

5349
return true;
5450
}
5551

56-
public boolean addLexWords( String... vargs ) {
52+
public boolean addLexWords(String... vargs) {
5753
ArrayList<LexWord> lexWords = new ArrayList<>();
5854
boolean containsKey = false;
5955
// number of arguments must be key (1) + lexWord pairs ( x * 2 )
60-
if( vargs.length % 2 != 1 ) {
56+
if (vargs.length % 2 != 1)
6157
return false;
62-
}
6358

6459
String key = vargs[0].toUpperCase();
65-
if( this.containsKey(key)) { containsKey = true; }
60+
if (this.containsKey(key)) { containsKey = true; }
6661

67-
for( int i=1; i < vargs.length; i++ ) {
62+
for (int i=1; i < vargs.length; i++) {
6863
try {
69-
if( containsKey ) {
64+
if( containsKey )
7065
this.get(key).add( new LexWord( vargs[i], Float.valueOf(vargs[i+1])));
71-
}
72-
else {
73-
lexWords.add( new LexWord( vargs[i], Float.valueOf(vargs[i+1])));
74-
}
66+
else
67+
lexWords.add( new LexWord( vargs[i], Float.valueOf(vargs[i+1])));
7568
i++;
7669
} catch( NumberFormatException e ) {
7770
System.err.println("Supplied args have incorrect format.");
7871
return false;
7972
}
8073
}
81-
if( !containsKey ) { this.put(key, lexWords); }
74+
if (!containsKey) { this.put(key, lexWords); }
8275
return true;
8376

8477
}
@@ -88,15 +81,14 @@ public boolean addLexWords( String... vargs ) {
8881
* you can combine lexicons.
8982
* @param lexicon
9083
*/
91-
public void addLexWords( Lexicon lexicon ) {
84+
public void addLexWords(Lexicon lexicon) {
9285
for (Map.Entry<String, ArrayList<LexWord>> pair : lexicon.entrySet()) {
9386
final String key = pair.getKey();
9487
final ArrayList<LexWord> lexWords = pair.getValue();
9588

9689
if (this.containsKey(key)) {
97-
for (LexWord word : lexWords) {
90+
for (LexWord word : lexWords)
9891
this.get(key).add(word);
99-
}
10092
} else {
10193
this.put(key, lexWords);
10294
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void main(String[] args) {
1919
System.out.println("Running...");
2020
ProbCNFGrammar exampleG = ProbCNFGrammarExamples.buildTrivialGrammar();
2121
CYK cyk = new CYK();
22-
List<String> words = new ArrayList<String>(Arrays.asList("the","man","liked","a","woman"));
22+
List<String> words = new ArrayList<>(Arrays.asList("the","man","liked","a","woman"));
2323
float[][][] probTable = cyk.parse(words, exampleG);
2424
cyk.printProbTable(probTable, words, exampleG);
2525
System.out.println("Done!");

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ProbCNFGrammar() {
2828
rules = new ArrayList<Rule>();
2929
}
3030

31-
public ProbCNFGrammar( ProbCNFGrammar grammar ) {
31+
public ProbCNFGrammar(ProbCNFGrammar grammar) {
3232
type = 4;
3333
rules = grammar.rules;
3434
}
@@ -38,16 +38,14 @@ public ProbCNFGrammar( ProbCNFGrammar grammar ) {
3838
* both the restrictions of the Context-free grammar, and all rules
3939
* or in Chomsky-Normal-Form
4040
*/
41-
public boolean addRules( List<Rule> ruleList ) {
42-
for( int i=0; i < ruleList.size(); i++ ) {
43-
if( !validRule(ruleList.get(i)) ) {
41+
public boolean addRules(List<Rule> ruleList) {
42+
for (Rule aRuleList : ruleList) {
43+
if (!validRule(aRuleList))
4444
return false;
45-
}
4645
}
47-
if( !validateRuleProbabilities(ruleList)) {
46+
if (!validateRuleProbabilities(ruleList))
4847
return false;
49-
}
50-
this.rules = ruleList;
48+
rules = ruleList;
5149
updateVarsAndTerminals();
5250
return true;
5351
}
@@ -57,10 +55,9 @@ public boolean addRules( List<Rule> ruleList ) {
5755
* both the restrictions of the Context-free grammar, and the rule is
5856
* in Chomsky Normal Form.
5957
*/
60-
public boolean addRule( Rule r ) {
61-
if( !validRule(r) ) {
58+
public boolean addRule(Rule r) {
59+
if (!validRule(r))
6260
return false;
63-
}
6461
rules.add(r);
6562
updateVarsAndTerminals(r);
6663
return true;
@@ -80,20 +77,17 @@ public boolean addRule( Rule r ) {
8077
* @return true, if rule is in CNF. false, otherwise
8178
*/
8279
public boolean validRule( Rule r ){
83-
if( !super.validRule(r) ){
80+
if (!super.validRule(r))
8481
return false;
85-
}
8682
// 3. rhs is null
87-
if( r.rhs == null || r.rhs.size() == 0) { return true; }
83+
if (r.rhs == null || r.rhs.size() == 0)
84+
return true;
8885
// 2. rhs is a terminal (a)
89-
else if( r.rhs.size() == 1 && isTerminal(r.rhs.get(0))) {
86+
else if (r.rhs.size() == 1 && isTerminal(r.rhs.get(0)))
9087
return true;
91-
}
9288
// 1. rhs is 2 variables (BC)
93-
else if( r.rhs.size() == 2 && isVariable(r.rhs.get(0))
94-
&& isVariable(r.rhs.get(1))) {
89+
else if (r.rhs.size() == 2 && isVariable(r.rhs.get(0)) && isVariable(r.rhs.get(1)))
9590
return true;
96-
}
9791
// rule is not in one of the 3 CNF forms
9892
return false;
9993
}

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ public ProbContextSensitiveGrammar() {
2121
* both the restrictions of the parent grammar (unrestricted) and
2222
* this grammar's restrictions.
2323
*/
24-
public boolean addRules( ArrayList<Rule> ruleList ) {
25-
for( int i=0; i < ruleList.size(); i++ ) {
26-
if( !super.validRule(ruleList.get(i)) ) {
24+
public boolean addRules(ArrayList<Rule> ruleList) {
25+
for (Rule aRuleList : ruleList) {
26+
if (!super.validRule(aRuleList))
2727
return false;
28-
}
29-
if( !validRule(ruleList.get(i)) ) {
28+
if (!validRule(aRuleList))
3029
return false;
31-
}
3230
}
33-
this.rules = ruleList;
31+
rules = ruleList;
3432
updateVarsAndTerminals();
3533
return true;
3634
}
@@ -40,13 +38,12 @@ public boolean addRules( ArrayList<Rule> ruleList ) {
4038
* both the restrictions of the parent grammar (unrestricted) and
4139
* this grammar's restrictions.
4240
*/
43-
public boolean addRule( Rule r ) {
44-
if( !super.validRule(r) ) {
41+
public boolean addRule(Rule r) {
42+
if (!super.validRule(r))
4543
return false;
46-
}
47-
else if( !validRule(r) ) {
44+
else if (!validRule(r))
4845
return false;
49-
}
46+
5047
rules.add(r);
5148
updateVarsAndTerminals(r);
5249
return true;
@@ -58,17 +55,16 @@ else if( !validRule(r) ) {
5855
* and the number of RHS symbols must be equal or greater than the number
5956
* of LHS symbols.
6057
*/
61-
public boolean validRule( Rule r ){
62-
if( !super.validRule(r) ){
58+
public boolean validRule(Rule r) {
59+
if (!super.validRule(r))
6360
return false;
64-
}
61+
6562
// len(rhs) >= len(lhs) must hold in context-sensitive.
66-
else if( r.rhs == null ) {
63+
else if (r.rhs == null)
6764
return false;
68-
}
69-
else if( r.rhs.size() < r.lhs.size() ) {
65+
66+
else if (r.rhs.size() < r.lhs.size())
7067
return false;
71-
}
7268

7369
return true;
7470
}

0 commit comments

Comments
 (0)