Skip to content

Commit

Permalink
Add adverb parsing. Tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanQuan committed Aug 12, 2018
1 parent 5f9df0c commit ce75f8c
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 41 deletions.
68 changes: 41 additions & 27 deletions src/game/system/input/PlayerInputParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,30 @@ public static void syntacticalAnalysis(PlayerCommand playerCommand,

PlayerAction playerAction = new PlayerAction();

String first = tokens.get(0);
if (Word.isVerb(first)) {
// Get adverbs
VerbPhrase verbPhrase = new VerbPhrase();
ArrayList<String> adverbs = new ArrayList<>();
while (!tokens.isEmpty() && Word.isAdverb(tokens.get(0))) {
adverbs.add(tokens.get(0));
tokens.remove(0);
}
verbPhrase.setAdverbs(adverbs);
if (tokens.isEmpty()) {
// If the action is only adverbs, then return early
playerAction.setVerbPhrase(verbPhrase);
playerCommand.addAction(playerAction);
return;
}
if (Word.isVerb(tokens.get(0))) {
// 0. The first word is a verb. Remove it and parse the rest of the
// input. No adverbs are allowed as it would not be
// possible to distinguish between the end of the verb phrase and
// the start of the proceeding indirect/direct object
// phrase without a dictionary of all possible verbs.
verbPhrase.setVerb(tokens.get(0));
tokens.remove(0);
playerAction.setVerbPhrase(new VerbPhrase(first));
}
playerAction.setVerbPhrase(verbPhrase);
// 1. Scan for a preposition. If one is found, remove it.
// Parse the input preceding the preposition as a direct object
// phrase. Parse the input following the preposition as an
Expand Down Expand Up @@ -383,31 +397,31 @@ public static void syntacticalCleanup(PlayerCommand playerCommand) {
* @param actions
*/
public static void fixSyntaxForward(ArrayList<PlayerAction> actions) {
String verbToCopy = null;
VerbPhrase verbPhraseToCopy = null;
String prepositionToCopy = null;
for (PlayerAction action : actions) {
verbToCopy = getForwardVerbToCopy(action, verbToCopy);
prepositionToCopy = getForwardPrepositionToCopy(action, verbToCopy,
verbPhraseToCopy = getForwardVerbPhraseToCopy(action, verbPhraseToCopy);
prepositionToCopy = getForwardPrepositionToCopy(action, verbPhraseToCopy,
prepositionToCopy);

setForwardVerbPhraseToCopy(action, verbToCopy);
setForwardVerbPhraseToCopy(action, verbPhraseToCopy);
setForwardPrepositionToCopy(action, prepositionToCopy);
moveForwardDirectToIndirect(action, prepositionToCopy);


}
}

private static String getForwardVerbToCopy(PlayerAction action,
String previousVerbToCopy) {
private static VerbPhrase getForwardVerbPhraseToCopy(PlayerAction action,
VerbPhrase previousVerbPhraseToCopy) {
if (action.hasVerbPhrase()
&& (action.hasDirectObjectPhrase() || action.hasIndirectObjectPhrase())) {
return action.getVerbPhrase().getVerb();
return action.getVerbPhrase();
}
if (action.hasVerbPhrase()) {
return null;
}
return previousVerbToCopy;
return previousVerbPhraseToCopy;
}

/**
Expand All @@ -418,9 +432,9 @@ private static String getForwardVerbToCopy(PlayerAction action,
* @return
*/
private static String getForwardPrepositionToCopy(PlayerAction action,
String verbToCopy,
VerbPhrase verbPhraseToCopy,
String previousPrepositionToCopy) {
if (verbToCopy != null) {
if (verbPhraseToCopy != null) {
if (!action.hasDirectObjectPhrase()
&& action.hasPreposition()
&& action.hasIndirectObjectPhrase()) {
Expand All @@ -439,12 +453,12 @@ private static String getForwardPrepositionToCopy(PlayerAction action,
* If there is a verb to copy forward, then copy it to the action.
*
* @param action
* @param verbToCopy
* @param verbPhraseToCopy
*/
private static void setForwardVerbPhraseToCopy(PlayerAction action,
String verbToCopy) {
if (verbToCopy != null) {
action.setVerbPhrase(verbToCopy);
VerbPhrase verbPhraseToCopy) {
if (verbPhraseToCopy != null) {
action.setVerbPhrase(verbPhraseToCopy);
}
}

Expand Down Expand Up @@ -489,18 +503,18 @@ private static void moveForwardDirectToIndirect(PlayerAction action,
* @param actions
*/
public static void fixSyntaxBackwards(ArrayList<PlayerAction> actions) {
String lastVerb = actions.get(0).hasVerbPhrase() ?
actions.get(0).getVerbPhrase().getVerb() : null;
VerbPhrase lastVerbPhrase = actions.get(0).hasVerbPhrase() ?
actions.get(0).getVerbPhrase() : null;
String prepositionToCopy = null;
ObjectPhrase indirectToCopy = null;
PlayerAction action;
for (int i = actions.size() - 1; i >= 0; i--) {
action = actions.get(i);

prepositionToCopy = getBackwardPrepositionToCopyBefore(action,
lastVerb, prepositionToCopy);
lastVerbPhrase, prepositionToCopy);
indirectToCopy = getBackwardsIndirectToCopyBefore(action,
lastVerb, indirectToCopy);
lastVerbPhrase, indirectToCopy);

setBackwardsPrepositionToCopy(action, prepositionToCopy);
setBackwardsIndirectToCopy(action, indirectToCopy);
Expand All @@ -510,8 +524,8 @@ public static void fixSyntaxBackwards(ArrayList<PlayerAction> actions) {
// indirectToCopy = getBackwardsIndirectToCopyAfter(action,
// indirectToCopy);

lastVerb = action.hasVerbPhrase() ?
action.getVerbPhrase().getVerb() : null;
lastVerbPhrase = action.hasVerbPhrase() ?
action.getVerbPhrase() : null;
}

}
Expand All @@ -525,9 +539,9 @@ public static void fixSyntaxBackwards(ArrayList<PlayerAction> actions) {
* @return
*/
private static String getBackwardPrepositionToCopyBefore(PlayerAction action,
String lastVerb,
VerbPhrase lastVerbPhrase,
String prepositionToCopy) {
if (action.hasVerbPhrase() && action.getVerbPhrase().getVerb().equals(lastVerb)) {
if (action.hasVerbPhrase() && action.getVerbPhrase().equals(lastVerbPhrase)) {
if (action.hasPreposition()) {
// Same verb, different preposition means update to new
// preposition to copy
Expand All @@ -540,9 +554,9 @@ private static String getBackwardPrepositionToCopyBefore(PlayerAction action,
}

private static ObjectPhrase getBackwardsIndirectToCopyBefore(PlayerAction action,
String lastVerb,
VerbPhrase lastVerbPhrase,
ObjectPhrase indirectToCopy) {
if (action.hasVerbPhrase() && action.getVerbPhrase().getVerb().equals(lastVerb)) {
if (action.hasVerbPhrase() && action.getVerbPhrase().equals(lastVerbPhrase)) {
if (action.hasIndirectObjectPhrase()) {
return action.getIndirectObjectPhrase();
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/system/input/words/ObjectPhrase.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String getNoun() {
}

public boolean hasAdjectives() {
return this.adjectives != null && !this.adjectives.isEmpty();
return !this.adjectives.isEmpty();
}

public String getBelongingPreposition() {
Expand Down
31 changes: 28 additions & 3 deletions src/game/system/input/words/VerbPhrase.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package game.system.input.words;
import util.FuncUtils;

import java.util.ArrayList;

/**
* Verb phrases describe a verb. As it stands now, this is equivalent to just a single verb String,
* but later on, extra properties may be added (Ex. adverbs).
Expand All @@ -13,12 +15,14 @@
*/
public class VerbPhrase {

ArrayList<String> adverbs;
private String verb;

public VerbPhrase() {
}

public VerbPhrase(String verb) {
adverbs = new ArrayList<>();
this.verb = verb;
}

Expand All @@ -32,19 +36,32 @@ public VerbPhrase(String verb) {
@Override
public boolean equals(Object other) {
if (other instanceof VerbPhrase) {
return hasSameVerb((VerbPhrase) other);
return hasSameAdjectives((VerbPhrase) other)
&& hasSameVerb((VerbPhrase) other);
}
return false;
}

public ArrayList<String> getAdverbs() {
return this.adverbs;
}

public String getVerb() {
return this.verb;
}

public boolean hasAdverbs() {
return !this.adverbs.isEmpty();
}

public boolean hasVerb() {
return this.verb != null;
}

public boolean hasSameAdjectives(VerbPhrase other) {
return FuncUtils.nullablesEqual(this.adverbs, other.getAdverbs());
}

public boolean hasSameVerb(VerbPhrase other) {
return FuncUtils.nullablesEqual(this.verb, other.getVerb());
}
Expand All @@ -54,15 +71,23 @@ public boolean hasSameVerb(VerbPhrase other) {
* @return true if this verb phrase does not have a verb.
*/
public boolean isEmpty() {
return !hasVerb();
return !hasAdverbs() && !hasVerb();
}

public void setAdverbs(ArrayList<String> adverbs) {
this.adverbs = adverbs;
}

public void setVerb(String verb) {
this.verb = verb;
}


@Override
public String toString() {
return "[verb: " + verb + "]";
return "["
+ (hasAdverbs() ? "adverbs: " + adverbs : "")
+ (hasVerb() ? " | verb: " + verb : "")
+ "]";
}
}
28 changes: 18 additions & 10 deletions src/game/system/input/words/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class Word {
* Connect 2 object phrases together (in some sense)
*/
public static final HashSet<String> JOINING_PREPOSITIONS = new HashSet<>(
Set.of("with", "about")
Set.of("with", "about", "against")
);

/**
Expand Down Expand Up @@ -103,7 +103,7 @@ public abstract class Word {
*/
public static final HashSet<String> NON_INDIRECT_TRANSITIVE_VERBS
= new HashSet<>(
Set.of("eat", "take", "get", "drop", "remove")
Set.of("eat", "take", "get", "drop", "remove", "hit")
);

/**
Expand Down Expand Up @@ -248,29 +248,37 @@ public static boolean isBelongingPreposition(String word) {
}

/**
* TODO. This is for resolving the indeterminism of receiveInput
* multi-playerAction stringCommands.
* All words that end with "ly" are treated as adverbs for parsing purposes.
*
* @param word
* @return false, until actually implemented
* @return true if the specified word is recognized as valid adverb.
*/
public static boolean isAdverb(String word) {
return word.toLowerCase().endsWith("ly");
}

/**
*
* @param word
* @return true if the specified word is recognized as a valid verb.
*/
public static boolean isVerb(String word) {
return VERBS.contains(word);
return VERBS.contains(word.toLowerCase());
}

public static boolean isNonIndirectTransitiveVerb(String word) {
return NON_INDIRECT_TRANSITIVE_VERBS.contains(word);
return NON_INDIRECT_TRANSITIVE_VERBS.contains(word.toLowerCase());
}

public static boolean isIndirectTransitiveVerb(String word) {
return MANDATORY_INDIRECT_TRANSITIVE_VERBS.contains(word);
return MANDATORY_INDIRECT_TRANSITIVE_VERBS.contains(word.toLowerCase());
}

public static boolean isOptionallyIndirectTransitiveVerb(String word) {
return OPTIONALLY_INDIRECT_TRANSITIVE_VERBS.contains(word);
return OPTIONALLY_INDIRECT_TRANSITIVE_VERBS.contains(word.toLowerCase());
}

public static boolean isIntransitiveVerb(String word) {
return INTRANSITIVE_VERBS.contains(word);
return INTRANSITIVE_VERBS.contains(word.toLowerCase());
}
}
Loading

0 comments on commit ce75f8c

Please sign in to comment.