I need help with my program. It needs to: Developing a language model based on the context-free grammar: 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) + ".";     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need help with my program. It needs to:

  1. Developing a language model based on the context-free grammar: Use the context-free grammar to develop a language model that can be used to generate coherent and grammatically correct sentences in American English.

  2. 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) + ".";
    }
}
 

Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Sorting
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education