[更新]以下是我正在处理的程序,而不是像我需要的那样读出文件。图像是预期的输出。
String generate()的辅助方法应该有助于递归文件:
这是我的计划:
import java.util.*;
import java.io.*;
public class GrammarProcessor {
/* Rules for each nonterminal is stored in String[] assoc. with key
TreeMap will store the keys in sorted order */
private Map<String, String[]> rules = new TreeMap<>();
private static final Random rnd = new Random();
/* Constructor */
public GrammarProcessor(List<String> grammar) {
if (grammar == null || grammar.isEmpty()) {
throw new IllegalArgumentException();
} else {
for (String element : grammar) {
if (grammar.contains("::=")) {
String[] token = element.split("::="); // splits to key and value
String left = token[0].trim();
String right = token[1].trim();
String[] split = right.split("[|]");
rules.put(left, split);
}
}
}
} // End of GrammarProcessor()
/* Method returns TRUE if the condition inside the if statement if symbol is equal
to any of the specified strings, otherwise return FALSE */
public boolean grammarContains(String symbol) { // nonterminal symbols
return rules.containsKey(symbol); // return TRUE if symbol is in rules else FALSE
} // End of grammarContains() - Done
/* Helper method for String generate() */
private String generate(String symbol, int n, String generated) {
if (grammarContains(symbol) == false) {
symbol += " ";
} else {
String[] array = rules.get(symbol);
for (String element : array[rnd.nextInt(array.length)].trim().split("[ \t]+")) {
if (n != 0) {
generated += element + " ";
return generate(symbol, n-1, generated);
} else {
break;
}
}
}
return generated;
}
/* Method returns nonterminal symbol */
public String getSymbols() {
return rules.keySet().toString();
} // End of getSymbols()
public String generate(String symbol) {
String generated = "";
if (grammarContains(symbol) == false) {
throw new IllegalArgumentException();
} else {
String[] array = rules.get(symbol);
for (String element : array[rnd.nextInt(array.length)].trim().split("[ \t]+")) {
generated += element + "";
symbol = element;
}
}
return generated;
}
public String[] generate(String symbol, int times) {
if (grammarContains(symbol) == false && times < 0) {
throw new IllegalArgumentException();
}
String line = generate(symbol);
String[] array = line.split(" ");
return null ;
}
} // End of GrammarProcessor
这是我的测试程序:
import java.io.*;
import java.util.*;
public class GrammarTest {
public static void main(String[] args) throws IOException {
Scanner console = new Scanner(System.in);
// open grammar file
Scanner input = new Scanner(new File("sentence1.txt"));
// read the grammar file and construct the grammarProcessor
List<String> grammar = new ArrayList<String>();
while (input.hasNextLine()) {
String nextRule = input.nextLine().trim();
if (nextRule.length() > 0)
grammar.add(nextRule);
}
GrammarProcessor processor = new GrammarProcessor(grammar);
System.out.println(processor.getSymbols()); //test getSymbols()
System.out.print("Which symbol do you want? ");
String symbol = console.nextLine();
if ( processor.grammarContains(symbol) ) //test grammarContains()
System.out.println(processor.generate(symbol)); //test
else
System.out.println(symbol + " not in grammar!");
}
}