Directory:Derek Elder/Programs/Sentence
MyWikiBiz, Author Your Legacy — Sunday December 22, 2024
Jump to navigationJump to search//Program 1a //Professor Pattis, ICS-23 Lab 1 //Programmers: Cameron Ruatta, Derek Elder import edu.uci.ics.pattis.ics23.collections.*; import edu.uci.ics.pattis.introlib.*; import java.util.StringTokenizer; import java.io.EOFException; import java.util.Arrays; public class Sentence { private static String randomWord(List<String> l) { return l.get((int)(l.size()*Math.random())); } public static void main(String[] args) { Map<String,List<String>> sentenceMap = new ArrayMap<String,List<String>>(); TypedBufferReader tbr = new TypedBufferReader("Enter Category Word file name"); //Repeatedly read lines from a file until the EOFException is thrown. for(;;) { try { String line = tbr.readLine(); StringTokenizer st = new StringTokenizer(line, ";"); String categoryWord = st.nextToken(); while(st.hasMoreTokens()) { String token = st.nextToken(); //Update Map List<String> sentenceArray = sentenceMap.get(categoryWord); if(sentenceArray == null) { sentenceArray = new ArrayList<String>(); sentenceMap.put(categoryWord,sentenceArray); } sentenceArray.add(token); Collections.sort(sentenceArray); //Sort the exemplar words in each list } } catch(EOFException e) {break;} } List<String> sentenceList = new ArrayList<String>(sentenceMap.keys()); Collections.sort(sentenceList); //Sort the keys in the list System.out.println("\nMap sorted alphabetically by Category and Word"); for(String c : sentenceList) { List<String> sentenceArray = sentenceMap.get(c); System.out.println(c + " can be replaced by " + sentenceArray); } tbr = new TypedBufferReader("\nEnter sentence generation file name"); List<String> sentenceFromFileList = new ArrayList<String>(); String pattern = null; for(;;) { try { String line = tbr.readLine(); pattern = line; StringTokenizer st = new StringTokenizer(line, ";"); while(st.hasMoreTokens()) { String token = st.nextToken(); sentenceFromFileList.add(token); } } catch(EOFException e) {break;} } List<String> randomList = new ArrayList<String>(); System.out.println("\nGenerated Sentence using the pattern: " + pattern); for(String c : sentenceFromFileList) { randomList.add(randomWord(sentenceMap.get(c))); } System.out.println(randomList); } }