From 7f16ae0286ab7492eee9f4019d976bc5ca95d556 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Thu, 7 Jun 2018 16:26:46 -0300 Subject: Indefinites --- src/main/java/bjc/inflexion/InflectionML.java | 62 +++++++++++++++++++-------- 1 file changed, 45 insertions(+), 17 deletions(-) (limited to 'src/main/java/bjc/inflexion/InflectionML.java') diff --git a/src/main/java/bjc/inflexion/InflectionML.java b/src/main/java/bjc/inflexion/InflectionML.java index 939e96e..9ee175d 100644 --- a/src/main/java/bjc/inflexion/InflectionML.java +++ b/src/main/java/bjc/inflexion/InflectionML.java @@ -1,6 +1,4 @@ -/** - * (C) Copyright 2017 Benjamin Culkin. - * +/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,9 +13,11 @@ */ package bjc.inflexion; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Iterator; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -45,6 +45,8 @@ public class InflectionML { private static Pattern FORM_MARKER = Pattern.compile("<(?[#N])(?[^:]*):(?[^>]*)>"); + private static Pattern AN_MARKER = Pattern.compile("\\{an(\\d+)\\}"); + /* The database of nouns. */ private static Nouns nounDB; @@ -66,14 +68,19 @@ public class InflectionML { * @return * The inflected string. */ - public static String inflect(final String form) { - final Matcher formMatcher = FORM_MARKER.matcher(form); - - final StringBuffer formBuffer = new StringBuffer(); + public static String inflect(String form) { + Matcher formMatcher = FORM_MARKER.matcher(form); + StringBuffer formBuffer = new StringBuffer(); int curCount = 1; + boolean inflectSingular = true; + int anCount = 0; + List anVals = new ArrayList<>(); + + boolean pendingAN = false; + while (formMatcher.find()) { final String command = formMatcher.group("command"); final String options = formMatcher.group("options"); @@ -129,9 +136,12 @@ public class InflectionML { } if (optionSet.contains("a")) { - /* :InflectionML - * Implement a/an for nouns. - */ + if (curCount == 1) { + anCount += 1; + rep = "{an" + anCount + "}"; + + pendingAN = true; + } } /* Break out of switch. */ @@ -140,10 +150,7 @@ public class InflectionML { break; } - final boolean shouldOverride = - !(rep.equals("no") || - rep.equals("a") || - rep.equals("an") ); + final boolean shouldOverride = !(rep.equals("no") || rep.matches("\\{an\\d+\\}")); if (optionSet.contains("w") && shouldOverride) { rep = EnglishUtils.smallIntToWord(curCount); @@ -162,15 +169,25 @@ public class InflectionML { case "N": final Noun noun = nounDB.getNoun(text); + String nounVal; + if (optionSet.contains("p") || !inflectSingular) { if (optionSet.contains("c")) { - formMatcher.appendReplacement(formBuffer, noun.classicalPlural()); + nounVal = noun.classicalPlural(); } else { - formMatcher.appendReplacement(formBuffer, noun.modernPlural()); + nounVal = noun.modernPlural(); } } else { - formMatcher.appendReplacement(formBuffer, noun.singular()); + nounVal = noun.singular(); } + + formMatcher.appendReplacement(formBuffer, nounVal); + if(pendingAN) { + anVals.add(EnglishUtils.pickIndefinite(nounVal)); + + pendingAN = false; + } + break; default: final String msg = String.format("Unknown command '%s'", command); @@ -181,6 +198,17 @@ public class InflectionML { formMatcher.appendTail(formBuffer); + String res = formBuffer.toString(); + formBuffer = new StringBuffer(); + + Matcher anMat = AN_MARKER.matcher(res); + + Iterator anItr = anVals.iterator(); + while(anMat.find()) { + anMat.appendReplacement(formBuffer, anItr.next()); + } + anMat.appendTail(formBuffer); + return formBuffer.toString(); } -- cgit v1.2.3