Skip to content

Commit

Permalink
fixes to #409
Browse files Browse the repository at this point in the history
  • Loading branch information
dhowe committed Apr 18, 2017
1 parent dab42b7 commit 9bd2428
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
51 changes: 35 additions & 16 deletions java/rita/RiLexicon.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,13 @@ public String randomWord() {

/**
* Returns a random word from the lexicon with the specified part-of-speech
*
* @see PosTagger
*/
public String randomWord(String pos) {
return randomWordByLength(pos, -1);
String word = randomWordByLength(pos, -1);
if (word == null)
throw new RiTaException("No words with pos="+pos+" found");
return word;
}

/**
Expand All @@ -186,15 +188,23 @@ public String randomWord(int syllableCount) {
*/
public String randomWord(String pos, int syllableCount) {

boolean pluralize = pos.equals("nns");

if (pos.equals("v")) pos = "vb";
if (pos.equals("r")) pos = "rb";
if (pos.equals("a")) pos = "jj";
if (pos.equals("n") || pos.equals("nns"))
pos = "nn";

Iterator<String> it = getIterator(pos);

if (it != null) {

if (syllableCount < 0)
return it.next(); // ignore syllable-count
if (syllableCount < 0) { // ignore syllable-count
return pluralize ? RiTa.pluralize(it.next()) : it.next();
}

Map<String, String> lookup = lexImpl.getLexicalData();

while (it.hasNext()) {

String word = it.next();
Expand All @@ -205,17 +215,19 @@ public String randomWord(String pos, int syllableCount) {
}

String sylStr = data.split("\\|")[0].trim();
if (sylStr.split(SP).length == syllableCount)
return word;

if (sylStr.split(SP).length == syllableCount) {
return pluralize ? RiTa.pluralize(word) : word;
}
}
}

return E;
}

private Iterator<String> getIterator(String pos) {
Iterator<String> it = (pos == null) ? lexImpl.randomIterator() :
lexImpl.randomPosIterator(pos);
Iterator<String> it = (pos == null) ? lexImpl.randomIterator() : lexImpl
.randomPosIterator(pos);
return it;
}

Expand All @@ -229,13 +241,20 @@ public String randomWordByLength(String pos, int targetLength) { // NIAPI

Iterator<String> it = getIterator(pos);

if (targetLength < 0)
return it.next();
try {

if (targetLength < 0) {
return it.next();
}

while (it.hasNext()) {
String s = it.next();
if (s.length() == targetLength)
return s;
while (it.hasNext()) {
String s = it.next();
if (s.length() == targetLength)
return s;
}

} catch (Exception e) {
return null;
}

return E;
Expand Down
17 changes: 8 additions & 9 deletions java/rita/RiTa.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public static String trim(String s) {
/*
RiLexicon delegates:
rhymes
randomWord
similarBySound
similarByLetter
similarBySoundAndLetter
Expand All @@ -155,10 +156,6 @@ public static String trim(String s) {
isAdjective
isVerb
*/
public static boolean isAlliteration(String wordA, String wordB) {
return getLexicon().isAlliteration(wordA, wordB);
}

public static String[] getRhymes(String input) {
return getLexicon().rhymes(input);
}
Expand Down Expand Up @@ -186,6 +183,10 @@ public String randomWord(int syllableCount) {
public String randomWord(String pos, int syllableCount) {
return getLexicon().randomWord(pos, syllableCount);
}

public static boolean isAlliteration(String wordA, String wordB) {
return getLexicon().isAlliteration(wordA, wordB);
}

public boolean isAdverb(String s) {
return getLexicon().isAdverb(s);
Expand Down Expand Up @@ -255,9 +256,8 @@ public String[] similarByLetter(String s, int minEditDistance) {
return getLexicon().similarByLetter(s, minEditDistance);
}

public String[] similarByLetter(String input, int minEditDistance,
boolean preserveLength) {
return getLexicon().similarByLetter(input, minEditDistance, preserveLength);
public String[] similarByLetter(String input, int med, boolean preserveLength) {
return getLexicon().similarByLetter(input, med, preserveLength);
}

public String[] similarBySound(String input, int minDist) {
Expand All @@ -268,12 +268,11 @@ public String[] alliterations(String input) {
return getLexicon().alliterations(input);
}

static RiLexicon lexicon;
static RiLexicon getLexicon() {
if (lexicon == null)
lexicon = new RiLexicon();
return lexicon;
}
} static RiLexicon lexicon;

public static int getWordCount(String s) {
return RiTa.tokenize(s).length;
Expand Down
10 changes: 10 additions & 0 deletions java/rita/support/RiPos.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package rita.support;

import rita.*;


/**
* Enumerated types for various Part-of-Speech representations.
Expand Down Expand Up @@ -198,4 +200,12 @@ public boolean equals(Object obj)
return this.tag.equals(((RiPos)obj).getTag());
}

public static void main(String[] args) {
RiLexicon rl = new RiLexicon();
for (int i = 0; i < PENN_TAGS.length; i++) {
System.out.print(PENN_TAGS[i].toString()+": ");
System.out.println(rl.randomWord(PENN_TAGS[i].toString()));
}
}

}// end

0 comments on commit 9bd2428

Please sign in to comment.