You plan to write a program that processes a large text file and given a word in
ID: 3772909 • Letter: Y
Question
You plan to write a program that processes a large text file and given a word in the file will return a list of words that follow that word, sorted from most likely to least likely to follow. You will then use this capability to make up random “sentences” that are likely correct grammatically but almost completely meaningless, such as the literary gem below:
their shoulders immodesty unchained a child in our souls were in france who do whatever
The main skeleton of the code has been given to you in Project6.java. Please fill out the given methods and add any other data members or private methods you may need.
Hints:
- We plan to use the Java library HashMap class for this project.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
public class Project6 {
// any data members you will need here
public Project6(String filename) {
// Process the text file and populate the data structure that contains
// the mapping of words to the words that follow the word and their count.
// Make sure you convert everything to lowercase.
// you might find useful the code below to split a string at white spaces or punctuation.
// String[] words = line.split("\W+");
// You can use as many data structures as you need.
// The getTopNFollowingWords method below should have to do almost no work other than getting the
// already prepared data.
}
/**
* Return a list of the top n words following the given word.
* The list is in order by frequency of words: i.e. the first
* word in the list is the most common word that follows the given word,
* the second word, the second most common word, and so on.
*
* If there are less than n words that follow the given word,
* return only the existing.
*
* @param word The word to find following words for
* @param n The size of the common words list
* @return The list of the top n common words
*/
public List<String> getTopNFollowingWords(String word, int n) {
// Use an already populated data structure to make a list of the n most common following words.
// This should be fast and just get all it needs from the data structure and put that into a list.
return new ArrayList<String>();
}
/**
* Construct a "sentence" with likely word sequences that has numWordsInSentence words.
* The sentence starts with the starting word and each word thereafter is picked randomly
* from the topNFollowingWords of the previous word in the sentence.
*
* The sentence MUST NOT contain equal consecutive words: e.g. they the the same is
*
* @param startingWord The starting word for the generated sentence.
* @param numWordsInSentence The number of words in the generated sentence
* @param pickFromTopN The number of most common words following a word, from which we will pick at one at random
*
* @return A generated "sentence" with likely word sequences
*/
public String makeRandomSentence(String startingWord, int numWordsInSentence, int pickFromTopN) {
// repatedly use the getTopNFollowingWords(prevWord, pickFromTopN) method above and
// pick a word at random from that to add to the sentence
// you may find this code useful to get a random element from a list
// Random r = new Random();
// ...
// int randIndex = r.nextInt(n); // will return an int in the interval [0, n)
return "";
}
/**
* Sample client code
*/
public static void main(String[] args) {
String[] commonWords = { "a", "all", "an", "and", "are", "as", "at",
"be", "been", "but", "by", "for", "from", "had", "has", "have",
"he", "her", "him", "his", "i", "in", "is", "it", "jean",
"man", "my", "no", "not", "of", "on", "one", "said", "she",
"so", "that", "the", "their", "there", "they", "this", "to",
"two", "was", "we", "were", "what", "when", "which", "who",
"will", "with", "would", "you" };
Project6 wordMapper = new Project6("LesMiserables.txt");
final int N = 10;
for (String word : commonWords) {
System.out.println("Top " + N + " words following "" + word
+ "": " + wordMapper.getTopNFollowingWords(word, N));
}
System.out.println();
System.out.println("Some random "sentences" that start with these words: ");
for (String word : commonWords) {
System.out.println(wordMapper.makeRandomSentence(word, 15, 50));
}
}
/*
Would print something like:
Top 10 words following "a": [man, little, sort, few, very, good, certain, moment, great, single]
Top 10 words following "all": [the, that, this, at, these, his, those, sorts, is, was]
...
...
Some random "sentences" that start with these words:
a year in despair until it in my name may happen next how you as
all covered him into innumerable enigmas which made on one morning you wish a year
an instant from being is impossible and of man burst in his way back then
...
...
*/
}
Explanation / Answer
Hi ,Implemented code according to your need.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Stringfrom {
public static void main(String ako[]) throws IOException{
Scanner scnr=new Scanner(System.in);
System.out.println("please enter word:");
String tp=scnr.next();
FileReader ff=new FileReader("C://Users//Infomite//Desktop//credit.txt");
String line;
String tot = "";
BufferedReader br = null;
br = new BufferedReader(ff);
while ((line = br.readLine()) != null&&line==tp) {
tot+=line;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.