在我的代码中应用语法规则

时间:2014-02-12 12:29:47

标签: java arrays

我开始编写一个有趣的程序,它返回有趣的语句,但我无法继续,因为我不知道如何在我的代码中实现语法规则:

:: = []

<simple_sentence> ::= <noun_phrase> <verb_phrase>

<noun_phrase> ::= <proper_noun> | 
                  <determiner> [ <adjective> ]... <common_noun> [ who <verb_phrase> ]

<verb_phrase> ::= <intransitive_verb> | 
                  <transitive_verb> <noun_phrase> |
                  is <adjective> |
                  believes that <simple_sentence>

我有一个线索,我应该在编码规则时使用if / while语句,但我不能在代码中编写::: = []

这是我开始的代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class RecursiveSyntax2 {

static final String[] conjunction = {"and", "or", "but", "because"};

static final String[] proper_noun = {"Fred", "Jane", "Richard Nixon", "Miss America"};

static final String[] common_noun = {"man", "woman", "fish", "elephant", "unicorn"};

static final String[] determiner = {"a", "the", "every", "some"};

static final String[] adjective = {"big", "tiny", "pretty", "bald"};

static final String[] intransitive_verb = {"runs", "jumps", "talks", "sleeps"};

static final String[] transitive_verb = {"loves", "hates", "sees", "knows", "looks for"};



  public static void main(String[] args) { 

      List<String[]> arrayList = new ArrayList<>();
        arrayList.add(conjunction);
        arrayList.add(proper_noun);
        arrayList.add(common_noun);
        arrayList.add(determiner);
        arrayList.add(adjective);
        arrayList.add(intransitive_verb);
        arrayList.add(transitive_verb);

      Random random = new Random();
      for(String[] currentArray : arrayList){
          String chosenString = currentArray[random.nextInt(currentArray.length)];
          System.out.println(chosenString);
        }


      while (true) {
         randomSentence();
         System.out.println(".\n\n");
         try {
             Thread.sleep(3000);
         }
         catch (InterruptedException e) {
         }
      }

}

   static void randomSentence() {

}

1 个答案:

答案 0 :(得分:0)

static void randomSentence() {
        if (Math.random() > 0.2) 
            randomNounPhrase();
                randomVerbPhrase();
        if (Math.random() > 0.75){
            System.out.print("" + randomItem(conjunction));
            randomSentence();
        }
    }

    static void randomNounPhrase() {
        if (Math.random()> 0.75)
            System.out.print(""+ randomItem(proper_noun));
        else
        {
            System.out.print(""+ randomItem(determiner));
            if (Math.random()> 0.5)
        System.out.print(""+ randomItem(adjective)+".");
            System.out.print(""+ randomItem(common_noun));
            if (Math.random() > 0.5){
                System.out.print(" who");
                randomVerbPhrase();
            }
        }
    }

    static void randomVerbPhrase() {

        if (Math.random() > 0.75)
            System.out.print(""+ randomItem(intransitive_verb));
        else if (Math.random() > 0.50){
            System.out.print("" + randomItem(transitive_verb));
            randomNounPhrase();
        }
        else if (Math.random() > 0.25)
            System.out.print("is" + randomItem(adjective));
        else{
            System.out.print("believes that");
            randomNounPhrase();
            randomVerbPhrase();
        }
    }
    static String randomItem(String[] listOfStrings){
        return listOfStrings[(int)(Math.random()*listOfStrings.length)];
    }
}