please edit and change my program to do the Following. You don't need to explain just change my program to do the following please. I need help with my program. It needs to: Use the context-free grammar to develop a language model that can be used to generate coherent and grammatically correct sentences in American English. Incorporating semantic information into the context-free grammar: Expand the context- free grammar to incorporate semantic information, such as word meaning and context, to generate more meaningful sentences in American English. import java.util.*; import java.util.stream.Collectors; public class CFG { private static Map> rules; public static void main(String[] args) { rules = new HashMap<>(); initializeGrammarRules(); String sentence = generateSentence("S"); System.out.println(sentence); } private static void initializeGrammarRules() { rules.put("S", Arrays.asList("NP VP")); rules.put("NP", Arrays.asList("Det N", "Det AP N")); rules.put("AP", Arrays.asList("Adj", "Adj AP")); rules.put("VP", Arrays.asList("V NP", "V NP PP")); rules.put("PP", Arrays.asList("P NP")); rules.put("Det", Arrays.asList("the", "a", "an")); rules.put("N", Arrays.asList("Animal", "Object")); rules.put("Animal", Arrays.asList("cat", "dog")); rules.put("Object", Arrays.asList("book", "tree")); rules.put("Adj", Arrays.asList("Size", "Color")); rules.put("Size", Arrays.asList("big", "small")); rules.put("Color", Arrays.asList("brown", "green")); rules.put("V", Arrays.asList("AnimalAction", "ObjectAction")); rules.put("AnimalAction", Arrays.asList("chased", "ate")); rules.put("ObjectAction", Arrays.asList("read", "climbed")); rules.put("P", Arrays.asList("in", "on", "under", "above")); } private static String generateSentence(String symbol) { List alternatives = rules.get(symbol); if (alternatives == null) { return symbol; } String expansion = alternatives.get(new Random().nextInt(alternatives.size())); String[] symbols = expansion.split("\\s+"); String sentence = Arrays.stream(symbols) .map(CFG::generateSentence) .collect(Collectors.joining(" ")); // Apply basic capitalization and punctuation rules. return sentence.substring(0, 1).toUpperCase() + sentence.substring(1) + "."; } }
please edit and change my program to do the
Following. You don't need to explain just change my program to do the following please.
I need help with my program. It needs to:
Use the context-free grammar to develop a language model that can be used to generate coherent and grammatically correct sentences in American English.
Incorporating semantic information into the context-free grammar: Expand the context- free grammar to incorporate semantic information, such as word meaning and context, to generate more meaningful sentences in American English.
import java.util.*;
import java.util.stream.Collectors;
public class CFG {
private static Map<String, List<String>> rules;
public static void main(String[] args) {
rules = new HashMap<>();
initializeGrammarRules();
String sentence = generateSentence("S");
System.out.println(sentence);
}
private static void initializeGrammarRules() {
rules.put("S", Arrays.asList("NP VP"));
rules.put("NP", Arrays.asList("Det N", "Det AP N"));
rules.put("AP", Arrays.asList("Adj", "Adj AP"));
rules.put("VP", Arrays.asList("V NP", "V NP PP"));
rules.put("PP", Arrays.asList("P NP"));
rules.put("Det", Arrays.asList("the", "a", "an"));
rules.put("N", Arrays.asList("Animal", "Object"));
rules.put("Animal", Arrays.asList("cat", "dog"));
rules.put("Object", Arrays.asList("book", "tree"));
rules.put("Adj", Arrays.asList("Size", "Color"));
rules.put("Size", Arrays.asList("big", "small"));
rules.put("Color", Arrays.asList("brown", "green"));
rules.put("V", Arrays.asList("AnimalAction", "ObjectAction"));
rules.put("AnimalAction", Arrays.asList("chased", "ate"));
rules.put("ObjectAction", Arrays.asList("read", "climbed"));
rules.put("P", Arrays.asList("in", "on", "under", "above"));
}
private static String generateSentence(String symbol) {
List<String> alternatives = rules.get(symbol);
if (alternatives == null) {
return symbol;
}
String expansion = alternatives.get(new Random().nextInt(alternatives.size()));
String[] symbols = expansion.split("\\s+");
String sentence = Arrays.stream(symbols)
.map(CFG::generateSentence)
.collect(Collectors.joining(" "));
// Apply basic capitalization and punctuation rules.
return sentence.substring(0, 1).toUpperCase() + sentence.substring(1) + ".";
}
}
Step by step
Solved in 4 steps with 3 images