summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbjculkin <bjculkin@mix.wvu.edu>2017-04-05 14:07:44 -0400
committerbjculkin <bjculkin@mix.wvu.edu>2017-04-05 14:07:44 -0400
commit16e11872321981e0c794d7c8ec73fbb949c2574f (patch)
tree7370af9801bf972a016bedf636b2d95c6060c621
parent19e411314a757f029fd157cf9b2968b839c4f1d0 (diff)
Finish markup for nouns/counts
-rw-r--r--src/examples/java/bjc/inflexion/examples/InflexionTester.java18
-rw-r--r--src/main/java/bjc/inflexion/EnglishUtils.java12
-rw-r--r--src/main/java/bjc/inflexion/InflectionML.java70
3 files changed, 58 insertions, 42 deletions
diff --git a/src/examples/java/bjc/inflexion/examples/InflexionTester.java b/src/examples/java/bjc/inflexion/examples/InflexionTester.java
index 829da97..dd247a6 100644
--- a/src/examples/java/bjc/inflexion/examples/InflexionTester.java
+++ b/src/examples/java/bjc/inflexion/examples/InflexionTester.java
@@ -29,7 +29,7 @@ import java.util.regex.Pattern;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
-import bjc.inflexion.nouns.Noun;
+import bjc.inflexion.InflectionML;
import bjc.inflexion.nouns.Nouns;
import bjc.inflexion.nouns.Prepositions;
@@ -40,9 +40,6 @@ import bjc.inflexion.nouns.Prepositions;
*
*/
public class InflexionTester {
- private static final String OUTPUT_FMT = "Word: %s\n\tSingular: %s\n\tModern Plural: %s"
- + "\n\tClassical Plural: %s\n\n";
-
/**
* Main method.
*
@@ -58,22 +55,17 @@ public class InflexionTester {
Scanner scn = new Scanner(System.in);
- System.out.print("Enter a noun to inflect (blank line to quit): ");
+ System.out.print("Enter a string to inflect (blank line to quit): ");
String ln = scn.nextLine().trim();
while(!ln.equals("")) {
System.out.println();
- Noun noun = nounDB.getNoun(ln);
+ String inflected = InflectionML.inflect(ln, nounDB);
- if(noun == null) {
- System.out.println("No inflection available for noun " + ln);
- } else {
- System.out.printf(OUTPUT_FMT, ln, noun.singular(), noun.modernPlural(),
- noun.classicalPlural());
- }
+ System.out.println("Inflected string: " + inflected);
- System.out.print("Enter a noun to inflect (blank line to quit): ");
+ System.out.print("\nEnter a noun to inflect (blank line to quit): ");
ln = scn.nextLine().trim();
}
diff --git a/src/main/java/bjc/inflexion/EnglishUtils.java b/src/main/java/bjc/inflexion/EnglishUtils.java
index 875d75e..96982ed 100644
--- a/src/main/java/bjc/inflexion/EnglishUtils.java
+++ b/src/main/java/bjc/inflexion/EnglishUtils.java
@@ -20,11 +20,11 @@ package bjc.inflexion;
*
*/
public class EnglishUtils {
- private static String[] smallNums = new String[] { "zero", "one", "two", "three", "four", "five", "six", "seven",
- "eight", "nine", "ten" };
+ private static String[] smallNums = new String[] { "zero", "one", "two", "three", "four", "five", "six",
+ "seven", "eight", "nine", "ten" };
- private static String[] summaryNums = new String[] { "no", "one", "a couple of", "a few", "several" };
- private static String[] endSummaryNums = new String[] { "no", "one", "a couple of", "a few", "several" };
+ private static String[] summaryNums = new String[] { "no", "one", "a couple of", "a few", "several" };
+ private static String[] endSummaryNums = new String[] { "no", "one", "a couple of", "a few", "several" };
private static int[] summaryMap = new int[] {
/*
@@ -49,7 +49,7 @@ public class EnglishUtils {
4, 4, 4, 4 };
public static String smallIntToWord(int num) {
- if (num >= 0 && num <= 10) {
+ if(num >= 0 && num <= 10) {
return smallNums[num];
}
@@ -59,7 +59,7 @@ public class EnglishUtils {
public static String intSummarize(int num, boolean atEnd) {
String[] nums = atEnd ? endSummaryNums : summaryNums;
- if (num >= 0 && num < 10) {
+ if(num >= 0 && num < 10) {
return nums[summaryMap[num]];
} else
return "many";
diff --git a/src/main/java/bjc/inflexion/InflectionML.java b/src/main/java/bjc/inflexion/InflectionML.java
index 6ec78ba..a281519 100644
--- a/src/main/java/bjc/inflexion/InflectionML.java
+++ b/src/main/java/bjc/inflexion/InflectionML.java
@@ -17,6 +17,7 @@ package bjc.inflexion;
import java.util.Arrays;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -30,16 +31,21 @@ import bjc.inflexion.nouns.Nouns;
*
*/
public class InflectionML {
- private static Pattern FORM_MARKER = Pattern.compile("<(?<command>[#A])(?<options>[^:]*):(?<text>[^>]*)>");
+ /**
+ *
+ */
+ private static final List<String> ESUB_OPT = Arrays.asList("a", "s", "w");
+ private static Pattern FORM_MARKER = Pattern
+ .compile("<(?<command>[#N])(?<options>[^:]*):(?<text>[^>]*)>");
/**
* Apply inflection to marked forms in the string.
*
* @param form
- * The string to inflect.
+ * The string to inflect.
*
* @param nounDB
- * The source to load nouns from.
+ * The source to load nouns from.
*
* @return The inflected string.
*/
@@ -51,69 +57,87 @@ public class InflectionML {
int curCount = 1;
boolean inflectSingular = true;
- while (formMatcher.find()) {
+ while(formMatcher.find()) {
String command = formMatcher.group("command");
String options = formMatcher.group("options");
String text = formMatcher.group("text");
Set<String> optionSet = new HashSet<>();
- for (int i = 1; i <= options.length(); i++) {
+ for(int i = 1; i <= options.length(); i++) {
optionSet.add(options.substring(i - 1, i));
}
- switch (command) {
+ switch(command) {
case "#":
try {
+ if(optionSet.contains("e")) {
+ optionSet.remove("e");
+ optionSet.addAll(ESUB_OPT);
+ }
curCount = Integer.parseInt(text);
- if (curCount != 1)
+ if(optionSet.contains("i")) {
+ curCount += 1;
+ }
+
+ if(curCount != 1)
inflectSingular = false;
else
inflectSingular = true;
+ /*
+ * Break out of switch.
+ */
+ if(optionSet.contains("d")) break;
+
String rep = text;
- if (optionSet.contains("n")) {
- if (curCount == 0)
- rep = "no";
+ if(optionSet.contains("n")) {
+ if(curCount == 0) rep = "no";
}
- if (optionSet.contains("s")) {
- if (curCount == 0) {
+ if(optionSet.contains("s")) {
+ if(curCount == 0) {
rep = "no";
inflectSingular = true;
}
}
- if (optionSet.contains("a")) {
+ if(optionSet.contains("a")) {
/*
* TODO implement a/an for nouns
*/
}
- boolean shouldOverride = !(rep.equals("no") || rep.equals("a") || rep.equals("an"));
- if (optionSet.contains("w") && shouldOverride) {
+ boolean shouldOverride = !(rep.equals("no") || rep.equals("a")
+ || rep.equals("an"));
+
+ if(optionSet.contains("w") && shouldOverride) {
rep = EnglishUtils.smallIntToWord(curCount);
- } else if (optionSet.contains("f")) {
+ }
+
+ if(optionSet.contains("f") && shouldOverride) {
rep = EnglishUtils.intSummarize(curCount, false);
}
formMatcher.appendReplacement(formBuffer, rep);
- } catch (NumberFormatException nfex) {
- throw new InflectionException("Count setter must take a number as a parameter", nfex);
+ } catch(NumberFormatException nfex) {
+ throw new InflectionException("Count setter must take a number as a parameter",
+ nfex);
}
-
+ break;
case "N":
Noun noun = nounDB.getNoun(text);
- if (inflectSingular || optionSet.contains("s")) {
- formMatcher.appendReplacement(formBuffer, noun.singular());
- } else if (optionSet.contains("p")) {
- if (optionSet.contains("c")) {
+ if(optionSet.contains("p") || !inflectSingular) {
+ if(optionSet.contains("c")) {
formMatcher.appendReplacement(formBuffer, noun.classicalPlural());
} else {
formMatcher.appendReplacement(formBuffer, noun.modernPlural());
}
+ } else {
+ formMatcher.appendReplacement(formBuffer, noun.singular());
}
+ break;
default:
String msg = String.format("Unknown command '%s'", command);