private Noun<?> getNounFor(String arg) {
   Noun<?> n = null;
   for (Noun<?> noun : nouns) {
     if (RandomUtils.contains(noun.getNames(), arg)) n = noun;
   }
   return n;
 }
Exemple #2
0
  /**
   * ************************************************************************************************************
   * Return all the entities of the given role in the correct case. Assumes the role will be some
   * kind of noun.
   *
   * @param role
   * @param element
   * @param kb
   * @return
   */
  private String formulateNounPhraseForCaseRole(CaseRole role, SVOElement element, KB kb) {
    if (!getCaseRolesScratchpad().containsKey(role)) {
      return "";
    }
    Set<String> rawNouns = Sentence.getRoleEntities(role, getCaseRolesScratchpad());

    List<String> fixedNouns = Lists.newArrayList();
    // We're assuming that only names and reified objects are in uppercase.
    for (String noun : rawNouns) {
      String temp = noun;
      if (!NLGStringUtils.isVariable(noun)) {
        temp = addProperties(noun);
        if (Noun.takesIndefiniteArticle(noun, kb)) {
          temp = Noun.aOrAn(temp) + " " + temp;
        }
        // Replace the noun with its SUMO representation if it has one.
        String kbStr = SumoProcessCollector.getProperFormOfEntity(noun, kb);
        temp = temp.replaceAll(noun, kbStr);
      }
      fixedNouns.add(temp);
      element.addConsumedCaseRole(role);
    }

    return NLGStringUtils.concatenateWithCommas(fixedNouns);
  }
 private String getPermission(Noun noun, Verb verb) {
   String nounPerm = noun.getClass().getAnnotation(PermissionName.class).value();
   String verbPerm = verb.getClass().getAnnotation(PermissionName.class).value();
   if (nounPerm == null || verbPerm == null)
     throw new IllegalArgumentException(
         "The arguments you supplied do not specify permission values!");
   return "core.permissions." + nounPerm + "." + verbPerm;
 }
 protected <T extends CPermissible> void handleCommandUnspecific0(
     Noun<T> n, CommandSender sender, String[] args) throws CommandException {
   Verb<T> verb = null;
   for (Verb<T> verb1 : n.getVerbs()) {
     if (RandomUtils.contains(verb1.getNames(), args[2])) verb = verb1;
   }
   if (verb == null)
     throw new ArgumentRequirementException("The verb you specified is not valid!");
   if (!sender.hasPermission(getPermission(n, verb)))
     throw new PermissionException("You do not have permission for this command!");
   if (args.length - 3 < verb.getRequiredArguments())
     throw new ArgumentRequirementException("You have not specified enough arguments!");
   T target = n.get(args[1]);
   String[] strings =
       args.length <= 3 ? new String[] {} : Arrays.copyOfRange(args, 3, args.length);
   if (target == null && !verb.canAcceptNullTarget())
     throw new ArgumentRequirementException("The target you specified is invalid!");
   else if (target == null) strings = new String[] {args[1]};
   verb.perform(sender, target, strings);
   Core.getPermissionsManager().save();
   Core.getPermissionsManager().reloadPermissions();
   if (Core.getNetworkManager() != null)
     Core.getNetworkManager().sendMassNetCommand(new PermissionsReloadNetCommand());
 }
 @Override
 protected List<String> handleTabComplete(
     CommandSender sender, Command command, String alias, String[] args) {
   if (!sender.hasPermission(PERMISSION)) return Collections.emptyList();
   // 0 - noun
   // 1 - noun autocomplete
   // 2 - verb
   // 3+ - not supported
   switch (args.length) {
     case 1:
       List<String> nounNames = new ArrayList<>();
       for (Noun<?> noun : nouns) {
         String s1 = args[0].toLowerCase();
         for (String s : noun.getNames()) {
           if (s.toLowerCase().startsWith(s1)) nounNames.add(s);
         }
       }
       return nounNames;
     case 2:
       Noun<?> nounFor = getNounFor(args[0]);
       if (nounFor == null) return Collections.emptyList();
       return nounFor.getTabCompleteFor(args[1]);
     case 3:
       Noun<?> nounFor1 = getNounFor(args[0]);
       if (nounFor1 == null) return Collections.emptyList();
       List<String> strings = new ArrayList<>();
       for (Verb<? extends CPermissible> verb : nounFor1.getVerbs()) {
         String s1 = args[2].toLowerCase();
         for (String s : verb.getNames()) {
           if (s.toLowerCase().startsWith(s1)
               && sender.hasPermission(getPermission(nounFor1, verb))) strings.add(s);
         }
       }
       return strings;
     default:
       return Collections.emptyList();
   }
 }
/**
 * This class can be used to generate random movie titles. It uses it's own dictionary of words so
 * that the titles aren't <i>completely</i> unrealistic nonsense.
 */
public class RandomMovieTitleGenerator {

  private static final Determiner DETERMINER = Determiner.getInstance();
  private static final Preposition PREPOSITION = Preposition.getInstance();
  private static final Conjunction CONJUNCTION = Conjunction.getInstance();
  private static final Noun NOUN = Noun.getInstance();
  private static final Verb VERB = Verb.getInstance();
  private static final Adjective ADJECTIVE = Adjective.getInstance();

  private static final Word[][] TITLE_PATTERNS = {
    // 1-word
    {NOUN},
    {ADJECTIVE},
    // 2-word
    {DETERMINER, NOUN},
    {NOUN, VERB},
    {ADJECTIVE, NOUN},
    {PREPOSITION, NOUN},
    // 3-word
    {DETERMINER, NOUN, VERB},
    {NOUN, CONJUNCTION, NOUN},
    {DETERMINER, ADJECTIVE, NOUN},
    {NOUN, PREPOSITION, NOUN},
    // 4-word
    {DETERMINER, NOUN, CONJUNCTION, NOUN},
    {DETERMINER, ADJECTIVE, NOUN, VERB},
    {NOUN, CONJUNCTION, DETERMINER, NOUN},
    {NOUN, PREPOSITION, DETERMINER, NOUN},
    {NOUN, VERB, PREPOSITION, NOUN},
    // 5-word
    {NOUN, CONJUNCTION, DETERMINER, ADJECTIVE, NOUN},
    {DETERMINER, ADJECTIVE, NOUN, CONJUNCTION, NOUN},
    {ADJECTIVE, NOUN, CONJUNCTION, ADJECTIVE, NOUN},
    {NOUN, VERB, PREPOSITION, DETERMINER, NOUN},
    {DETERMINER, NOUN, VERB, PREPOSITION, NOUN}
  };

  public static String generateTitle() {
    // get random title pattern
    Random random = new Random();
    int idx = random.nextInt(TITLE_PATTERNS.length);
    Word[] pattern = TITLE_PATTERNS[idx];
    boolean nextWordVowel = false;
    boolean nextWordConsonant = false;
    String randomWord;

    // construct title with random words in the pattern
    List<String> titleWords = new ArrayList<>();
    for (Word word : pattern) {
      if (nextWordVowel && word instanceof Noun) {
        randomWord = ((Noun) word).getRandomVowelNoun();
        nextWordVowel = false;
      } else if (nextWordConsonant && word instanceof Noun) {
        randomWord = ((Noun) word).getRandomConsonantNoun();
        nextWordConsonant = false;
      } else if (nextWordVowel && word instanceof Adjective) {
        randomWord = ((Adjective) word).getRandomVowelAdjective();
        nextWordVowel = false;
      } else if (nextWordConsonant && word instanceof Adjective) {
        randomWord = ((Adjective) word).getRandomConsonantAdjective();
        nextWordConsonant = false;
      } else {
        randomWord = word.getRandomWord();

        // flag next word to start with consonant if this is "A"
        if (randomWord.equals("A")) {
          nextWordConsonant = true;
        }
        // flag next word to start with vowel if this is "An"
        else if (randomWord.equals("An")) {
          nextWordVowel = true;
        }
      }

      titleWords.add(randomWord);
    }
    return constructTitle(titleWords.toArray(new String[0]));
  }

  private static String constructTitle(String... words) {
    return StringUtils.join(words, " ");
  }

  /** Private classes */
  private abstract static class Word {
    public abstract String getRandomWord();
  }

  private static class Determiner extends Word {
    /* Words may be repeated to increase probability of being selected */
    private static final String[] DETERMINERS = {
      "The", "The", "The", "The", "The", "A", "A", "An", "An", "My", "Our", "Their", "His", "Her",
      "The", "The"
    };

    private static final Determiner _instance = new Determiner();

    public static Determiner getInstance() {
      return _instance;
    }

    @Override
    public String getRandomWord() {
      Random random = new Random();
      int idx = random.nextInt(DETERMINERS.length);
      return DETERMINERS[idx];
    }
  }

  private static class Preposition extends Word {
    /* Words may be repeated to increase probability of being selected */
    private static final String[] PREPOSITIONS = {
      "In", "At", "On", "Over", "After", "To", "Of", "Under", "From", "Through", "Before"
    };

    private static final Preposition _instance = new Preposition();

    public static Preposition getInstance() {
      return _instance;
    }

    @Override
    public String getRandomWord() {
      Random random = new Random();
      int idx = random.nextInt(PREPOSITIONS.length);
      return PREPOSITIONS[idx];
    }
  }

  private static class Conjunction extends Word {
    /* Words may be repeated to increase probability of being selected */
    private static final String[] CONJUNCTIONS = {
      "for", "and", "and", "and", "but", "or", "and", "for", "and"
    };

    private static final Conjunction _instance = new Conjunction();

    public static Conjunction getInstance() {
      return _instance;
    }

    @Override
    public String getRandomWord() {
      Random random = new Random();
      int idx = random.nextInt(CONJUNCTIONS.length);
      return CONJUNCTIONS[idx];
    }
  }

  private static class Noun extends Word {
    private static final int CONSONANT_NOUNS_INDEX = 0;
    private static final int VOWEL_NOUNS_INDEX = 1;
    /* Words may be repeated to increase probability of being selected */
    private static final String[][] NOUNS = {
      // starts with consonant
      {
        "Man",
        "Woman",
        "Kid",
        "Dog",
        "War",
        "House",
        "Family",
        "Race",
        "Person",
        "Water",
        "Fire",
        "Music",
        "Baker",
        "Cook",
        "Food",
        "Doctor",
        "Lawyer",
        "Number",
        "Singer",
        "Programmer",
        "Criminal",
        "Crime",
        "Cop",
        "Car",
        "Truck",
        "Boat",
        "River",
        "Ocean",
        "Country",
        "Smile",
        "Face",
        "Horse",
        "Penguin",
        "Pig",
        "Citizen",
        "Missiles",
        "Death",
        "Life",
        "Boss",
        "He",
        "She",
        "You",
        "They",
        "Michael",
        "Paul",
        "Peter",
        "Ruth",
        "John",
        "John",
        "Jane",
        "Revolution",
        "Magic",
        "Cloud",
        "Mother",
        "Father",
        "Desk",
        "Balloon",
        "Machine",
        "Politics",
        "Education",
        "Robot",
        "Zombie",
        "Monster",
        "Future",
        "Past",
        "Day",
        "Night",
        "School",
        "Dawn",
        "Stair",
        "Ladder",
        "Circus",
        "Clown",
        "Street",
        "Road",
        "City",
        "Sound",
        "Town",
        "Film",
        "Countryside",
        "Hotel",
        "Motorcycle",
        "Company",
        "Corporation",
        "Water",
        "Money",
        "Baseball",
        "Football",
        "Hockey",
        "Rope",
        "Tree",
        "Wind",
        "Insect",
        "Raven",
        "Bird",
        "Camel",
        "Smoke",
        "Spider",
        "Snake",
        "We",
        "Radio",
        "Shark",
        "Sea",
        "California",
        "Brooklyn",
        "Lake",
        "Sun",
        "Child",
        "Blood",
        "Fish",
        "Mountain",
        "Hill",
        "Mission",
        "Dad",
        "Mom",
        "Brother",
        "Sister",
        "Husband",
        "Wife",
        "Soul",
        "Art",
        "Glass",
        "Heart",
        "Time",
        "Moon",
        "Sun",
        "Star",
        "Planet",
        "Jet",
        "Sky",
        "Fence",
        "Neighborhood",
        "Door",
        "Window",
        "Opportunity",
        "Salesman",
        "Force"
      },
      // starts with vowel
      {
        "Earth",
        "Idea",
        "It",
        "I",
        "Everything",
        "Eve",
        "Esther",
        "Alien",
        "Index",
        "Animal",
        "Elephant",
        "Albatross",
        "Ice",
        "Eagle",
        "Onyx",
        "Orange",
        "Apple",
        "Angel",
        "Angle",
        "Upstairs",
        "Understanding",
        "Overture",
        "Opening",
        "Ox",
        "Appointment",
        "Attitude",
        "Employee",
        "Eye",
        "Item",
        "Oregon",
        "Air",
        "Airplane",
        "Operation",
        "Office",
        "Uncle",
        "Aunt",
        "Ant",
        "Ape",
        "Ending",
        "End",
        "Exit",
        "Everyone",
        "Ether",
        "Airport",
        "Igloo",
        "Eskimo",
        "Iceland",
        "Alaska",
        "Italy",
        "Escape",
        "Immigrant",
        "Hour",
        "Advice",
        "Arrival"
      }
    };

    private static final Noun _instance = new Noun();

    public static Noun getInstance() {
      return _instance;
    }

    @Override
    public String getRandomWord() {
      Random random = new Random();
      int nounsIdx = random.nextInt(2);
      if (nounsIdx == CONSONANT_NOUNS_INDEX) {
        return getRandomConsonantNoun();
      }
      return getRandomVowelNoun();
    }

    public String getRandomConsonantNoun() {
      Random random = new Random();
      int idx = random.nextInt(NOUNS[CONSONANT_NOUNS_INDEX].length);
      return NOUNS[CONSONANT_NOUNS_INDEX][idx];
    }

    public String getRandomVowelNoun() {
      Random random = new Random();
      int idx = random.nextInt(NOUNS[VOWEL_NOUNS_INDEX].length);
      return NOUNS[VOWEL_NOUNS_INDEX][idx];
    }
  }

  private static class Verb extends Word {
    private static final int PRESENT_VERBS_INDEX = 0;
    private static final int PAST_VERBS_INDEX = 1;
    /* Words may be repeated to increase probability of being selected */
    private static final String[][] VERBS = {
      // present
      {
        "is", "Runs", "Goes", "Walks", "Says", "Touches", "Speaks", "Drifts", "Screams", "Wanders",
        "Hears", "Sees", "Believes", "Loves", "Stays", "Leaves", "Pays", "Leads", "Laughs",
            "Smiles",
        "Speaks", "Hurts", "Kills", "Saves", "Grows", "Drives", "Flies", "Swims", "Takes", "Loves",
        "is", "Writes", "Makes", "Cooks", "Sells", "Steals", "Falls", "Calls", "Travels", "is",
        "Loves", "is", "Sings", "Put", "Wonders", "Thinks", "Escapes", "Likes", "Likes", "Climbs",
        "Awakens", "Fight"
      },
      // past
      {
        "Was",
        "Ran",
        "Went",
        "Walked",
        "Said",
        "Touched",
        "Heard",
        "Saw",
        "Believed",
        "Loved",
        "Stayed",
        "Left",
        "Paid",
        "Led",
        "Laughed",
        "Smiled",
        "Fell",
        "Won",
        "Battled",
        "Lost",
        "Spoke",
        "Hurt",
        "Killed",
        "Saved",
        "Grew",
        "Drove",
        "Flew",
        "Swam",
        "Took",
        "Wrote",
        "Made",
        "Cooked",
        "Sold",
        "Sang",
        "Stole",
        "Fell",
        "Called",
        "Traveled",
        "Put",
        "Thought",
        "Escaped",
        "Liked",
        "Quit",
        "Died",
        "Drifted",
        "Floated",
        "Shot",
        "Arrived",
        "Banned",
        "Answered",
        "Cut",
        "Froze",
        "Taught",
        "Shut"
      }
    };

    private static final Verb _instance = new Verb();

    public static Verb getInstance() {
      return _instance;
    }

    @Override
    public String getRandomWord() {
      Random random = new Random();
      int nounsIdx = random.nextInt(2);
      if (nounsIdx == PRESENT_VERBS_INDEX) {
        return getRandomPresentVerb();
      }
      return getRandomPastVerb();
    }

    public String getRandomPresentVerb() {
      Random random = new Random();
      int idx = random.nextInt(VERBS[PRESENT_VERBS_INDEX].length);
      return VERBS[PRESENT_VERBS_INDEX][idx];
    }

    public String getRandomPastVerb() {
      Random random = new Random();
      int idx = random.nextInt(VERBS[PAST_VERBS_INDEX].length);
      return VERBS[PAST_VERBS_INDEX][idx];
    }
  }

  private static class Adjective extends Word {
    private static final int CONSONANT_ADJECTIVES_INDEX = 0;
    private static final int VOWEL_ADJECTIVES_INDEX = 1;
    /* Words may be repeated to increase probability of being selected */
    private static final String[][] ADJECTIVES = {
      // starts with consonant
      {
        "Beautiful",
        "Weird",
        "Heavy",
        "Loud",
        "Light",
        "Crazy",
        "Mean",
        "Wonderful",
        "Magical",
        "Scary",
        "Horrible",
        "Stellar",
        "Broken",
        "Funny",
        "Narrow",
        "Dry",
        "Big",
        "Small",
        "Wide",
        "Black",
        "White",
        "Red",
        "Blue",
        "Green",
        "Yellow",
        "Gray",
        "Brown",
        "Purple",
        "Dark",
        "Sharp",
        "Bloody",
        "High",
        "Low",
        "Terrible",
        "Lovely",
        "Nice",
        "Wild",
        "Hard",
        "Long",
        "Short",
        "Frozen",
        "Sad",
        "Sweet",
        "Bitter",
        "Clear",
        "Quiet"
      },
      // starts with vowel
      {
        "Incredile",
        "Amazing",
        "Easy",
        "Ugly",
        "Orange",
        "Inner",
        "Inside",
        "Impossible",
        "Indescribable",
        "Undisputed",
        "Inconvenient",
        "Unbroken",
        "Undefeated",
        "Adventurous",
        "Awesome",
        "Azure",
        "Ultimate",
        "Ultraviolet",
        "Unbearable",
        "Unbreakable",
        "Unchanged",
        "Uncomfortable",
        "Obtrusive",
        "Obtuse",
        "Obese",
        "Offensive",
        "Opaque",
        "Obnoxious",
        "Outstanding",
        "Open",
        "Accidental",
        "Official",
        "Omniscient",
        "Innocent",
        "Illegal"
      }
    };

    private static final Adjective _instance = new Adjective();

    public static Adjective getInstance() {
      return _instance;
    }

    @Override
    public String getRandomWord() {
      Random random = new Random();
      int nounsIdx = random.nextInt(2);
      if (nounsIdx == CONSONANT_ADJECTIVES_INDEX) {
        return getRandomConsonantAdjective();
      }
      return getRandomVowelAdjective();
    }

    public String getRandomConsonantAdjective() {
      Random random = new Random();
      int idx = random.nextInt(ADJECTIVES[CONSONANT_ADJECTIVES_INDEX].length);
      return ADJECTIVES[CONSONANT_ADJECTIVES_INDEX][idx];
    }

    public String getRandomVowelAdjective() {
      Random random = new Random();
      int idx = random.nextInt(ADJECTIVES[VOWEL_ADJECTIVES_INDEX].length);
      return ADJECTIVES[VOWEL_ADJECTIVES_INDEX][idx];
    }
  }
}