Java Question 2: Hey I\'ve been struggling with this question for almost two wee
ID: 3833293 • Letter: J
Question
Java Question 2:
Hey I've been struggling with this question for almost two weeks now and am in need of some help. If anyone can give me the correct answer to this I would greatly appreciate it.
static boolean Q2(String possiblePhrase){
// return true if the input could be a phrase in the format "<adjective> <noun> <verb> <adverb>",
// false otherwise. Since many words have multiple parts of speech depending on their usage, return
// true if it is possible that the input can be in this format by any choice of each word's part of speech.
// You may assume the input contains 4 words separated by spaces and you may use word class to help but it must be done with static boolean Q2(String possiblePhrase){
Word Class:
private static final String DIRECTORY = "/data/";
private static final String DICTIONARY_FILENAME = DIRECTORY + "cmudict-0.7b";
private static final String ADJECTIVES_FILENAME = DIRECTORY + "index.adj";
private static final String ADVERBS_FILENAME = DIRECTORY + "index.adv";
private static final String NOUNS_FILENAME = DIRECTORY + "index.noun";
private static final String SENSES_FILENAME = DIRECTORY + "index.sense";
private static final String VERB_FILENAME = DIRECTORY + "index.verb";
private static Map<String, List<String>> dictionary = null;
private static Map<String, Set<String>> partOfSpeech = null;
/**
* Return the number of syllables in the input word.
*/
public static int numberOfSyllables(String word){
int syllables = 0;
List<String> sounds = Words.wordsToSounds(word);
if(sounds == null){
return syllables;
}
for(String sound : sounds){
if(Words.isVowel(sound)){
syllables++;
}
}
return syllables;
}
/**
* Returns an ArrayList containing all the possible parts of speech of the input word. An empty ArrayList
* is returned if the part of speech is unknown.
*/
public static ArrayList<String> getPartsOfSpeech(String word){
ArrayList<String> partsOfSpeech = new ArrayList<>();
for(String partOfSpeech : Words.getSpeechMap().keySet()){
if(Words.getSpeechMap().get(partOfSpeech).contains(word.toLowerCase())){
partsOfSpeech.add(partOfSpeech);
}
}
return partsOfSpeech;
}
/**
* returns the number of rhyming syllables between the two input words. Returns 0 if either word in not found in
* the dictionary.
*/
public static int rhymeLength(String word1, String word2){
List<String> sounds1 = Words.wordsToSounds(word1);
List<String> sounds2 = Words.wordsToSounds(word2);
if(sounds1 == null || sounds2 == null){
return 0;
}
int pointer1 = sounds1.size() - 1;
int pointer2 = sounds2.size() - 1;
int rhymingSyllables = 0;
while(pointer1 >= 0 && pointer2 >= 0){
if(!sounds1.get(pointer1).equals(sounds2.get(pointer2))){
break;
}
if(isVowel(sounds1.get(pointer1))){
rhymingSyllables++;
}
pointer1--;
pointer2--;
}
return rhymingSyllables;
}
/**
* returns the number of alliterating sounds between the two input words. Returns 0 if either word in not found in
* the dictionary.
*/
public static int alliterationLength(String word1, String word2){
List<String> sounds1 = Words.wordsToSounds(word1);
List<String> sounds2 = Words.wordsToSounds(word2);
if(sounds1 == null || sounds2 == null){
return 0;
}
int alliteratingSounds = 0;
while(alliteratingSounds < sounds1.size() && alliteratingSounds < sounds2.size()){
if(!sounds1.get(alliteratingSounds).equals(sounds2.get(alliteratingSounds))){
break;
}
alliteratingSounds++;
}
return alliteratingSounds;
}
// private methods
private static List<String> wordsToSounds(String word){
Map<String, List<String>> dictionary = Words.getDictionary();
if(dictionary.containsKey(word.toLowerCase())){
return dictionary.get(word.toLowerCase());
}else{
return null;
}
}
private static boolean isVowel(String sound){
return sound.length() > 2;
}
private static Map<String, List<String>> getDictionary(){
if(Words.dictionary == null){
Words.dictionary = new HashMap<>();
Set<String> words = new HashSet<>();
InputStream in = Words.class.getResourceAsStream(Words.DICTIONARY_FILENAME);
BufferedReader input = new BufferedReader(new InputStreamReader(in));
String line;
try{
while((line = input.readLine()) != null){
// Check if the line is valid. Also ignore multiple pronunciations for the same spelling
if(line.length() == 0 || !Character.isLetter(line.charAt(0)) || line.indexOf('(') != -1){
continue;
}
String[] split = line.split(" ");
if(split.length != 2){
System.out.println("Something went horribly wrong (parsing dictionary)");
continue;
}
String word = split[0].toLowerCase();
List<String> pronunciation = new ArrayList<>();
pronunciation.addAll(Arrays.asList(split[1].toLowerCase().replace('1', '0').replace('2', '0').split(" ")));
Words.dictionary.put(word, pronunciation);
}
}catch(Exception e){
System.out.println(e);
}
}
return Words.dictionary;
}
private static Map<String, Set<String>> getSpeechMap(){
if(Words.partOfSpeech == null){
Words.partOfSpeech = new HashMap<>();
parsePartsOfSpeechFile("adjective", Words.ADJECTIVES_FILENAME);
parsePartsOfSpeechFile("adverb", Words.ADVERBS_FILENAME);
parsePartsOfSpeechFile("noun", Words.NOUNS_FILENAME);
parsePartsOfSpeechFile("sense", Words.SENSES_FILENAME);
parsePartsOfSpeechFile("verb", Words.VERB_FILENAME);
}
return Words.partOfSpeech;
}
private static void parsePartsOfSpeechFile(String partOfSpeech, String filename){
Set<String> words = new HashSet<>();
InputStream in = Words.class.getResourceAsStream(filename);
BufferedReader input = new BufferedReader(new InputStreamReader(in));
String line;
try{
while((line = input.readLine()) != null){
if(line.length() == 0 || !Character.isLetter(line.charAt(0)) || line.indexOf('_') != -1){
continue;
}
String[] splits = line.split(" ");
if(splits.length == 0){
continue;
}else{
words.add(splits[0]);
}
}
Words.partOfSpeech.put(partOfSpeech, words);
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){
String word = "orange";
System.out.println(getPartsOfSpeech(word));
System.out.println(numberOfSyllables(word));
}
}
Explanation / Answer
hope it might get solution for you..........
private static final String DIRECTORY = "/data/";
private static final String DICTIONARY_FILENAME = DIRECTORY + "cmudict-0.7b";
private static final String ADJECTIVES_FILENAME = DIRECTORY + "index.adj";
private static final String ADVERBS_FILENAME = DIRECTORY + "index.adv";
private static final String NOUNS_FILENAME = DIRECTORY + "index.noun";
private static final String SENSES_FILENAME = DIRECTORY + "index.sense";
private static final String VERB_FILENAME = DIRECTORY + "index.verb";
private static Map<String, List<String>> dictionary = null;
private static Map<String, Set<String>> partOfSpeech = null;
/**
* Return the number of syllables in the input word.
*/
public static int numberOfSyllables(String word){
int syllables = 0;
List<String> sounds = Words.wordsToSounds(word);
if(sounds == null){
return syllables;
}
for(String sound : sounds){
if(Words.isVowel(sound)){
syllables++;
}
}
return syllables;
}
/**
* Returns an ArrayList containing all the possible parts of speech of the input word. An empty ArrayList
* is returned if the part of speech is unknown.
*/
public static ArrayList<String> getPartsOfSpeech(String word){
ArrayList<String> partsOfSpeech = new ArrayList<>();
for(String partOfSpeech : Words.getSpeechMap().keySet()){
if(Words.getSpeechMap().get(partOfSpeech).contains(word.toLowerCase())){
partsOfSpeech.add(partOfSpeech);
}
}
return partsOfSpeech;
}
/**
* returns the number of rhyming syllables between the two input words. Returns 0 if either word in not found in
* the dictionary.
*/
public static int rhymeLength(String word1, String word2){
List<String> sounds1 = Words.wordsToSounds(word1);
List<String> sounds2 = Words.wordsToSounds(word2);
if(sounds1 == null || sounds2 == null){
return 0;
}
int pointer1 = sounds1.size() - 1;
int pointer2 = sounds2.size() - 1;
int rhymingSyllables = 0;
while(pointer1 >= 0 && pointer2 >= 0){
if(!sounds1.get(pointer1).equals(sounds2.get(pointer2))){
break;
}
if(isVowel(sounds1.get(pointer1))){
rhymingSyllables++;
}
pointer1--;
pointer2--;
}
return rhymingSyllables;
}
/**
* returns the number of alliterating sounds between the two input words. Returns 0 if either word in not found in
* the dictionary.
*/
public static int alliterationLength(String word1, String word2){
List<String> sounds1 = Words.wordsToSounds(word1);
List<String> sounds2 = Words.wordsToSounds(word2);
if(sounds1 == null || sounds2 == null){
return 0;
}
int alliteratingSounds = 0;
while(alliteratingSounds < sounds1.size() && alliteratingSounds < sounds2.size()){
if(!sounds1.get(alliteratingSounds).equals(sounds2.get(alliteratingSounds))){
break;
}
alliteratingSounds++;
}
return alliteratingSounds;
}
// private methods
private static List<String> wordsToSounds(String word){
Map<String, List<String>> dictionary = Words.getDictionary();
if(dictionary.containsKey(word.toLowerCase())){
return dictionary.get(word.toLowerCase());
}else{
return null;
}
}
private static boolean isVowel(String sound){
return sound.length() > 2;
}
private static Map<String, List<String>> getDictionary(){
if(Words.dictionary == null){
Words.dictionary = new HashMap<>();
Set<String> words = new HashSet<>();
InputStream in = Words.class.getResourceAsStream(Words.DICTIONARY_FILENAME);
BufferedReader input = new BufferedReader(new InputStreamReader(in));
String line;
try{
while((line = input.readLine()) != null){
// Check if the line is valid. Also ignore multiple pronunciations for the same spelling
if(line.length() == 0 || !Character.isLetter(line.charAt(0)) || line.indexOf('(') != -1){
continue;
}
String[] split = line.split(" ");
if(split.length != 2){
System.out.println("Something went horribly wrong (parsing dictionary)");
continue;
}
String word = split[0].toLowerCase();
List<String> pronunciation = new ArrayList<>();
pronunciation.addAll(Arrays.asList(split[1].toLowerCase().replace('1', '0').replace('2', '0').split(" ")));
Words.dictionary.put(word, pronunciation);
}
}catch(Exception e){
System.out.println(e);
}
}
return Words.dictionary;
}
private static Map<String, Set<String>> getSpeechMap(){
if(Words.partOfSpeech == null){
Words.partOfSpeech = new HashMap<>();
parsePartsOfSpeechFile("adjective", Words.ADJECTIVES_FILENAME);
parsePartsOfSpeechFile("adverb", Words.ADVERBS_FILENAME);
parsePartsOfSpeechFile("noun", Words.NOUNS_FILENAME);
parsePartsOfSpeechFile("sense", Words.SENSES_FILENAME);
parsePartsOfSpeechFile("verb", Words.VERB_FILENAME);
}
return Words.partOfSpeech;
}
private static void parsePartsOfSpeechFile(String partOfSpeech, String filename){
Set<String> words = new HashSet<>();
InputStream in = Words.class.getResourceAsStream(filename);
BufferedReader input = new BufferedReader(new InputStreamReader(in));
String line;
try{
while((line = input.readLine()) != null){
if(line.length() == 0 || !Character.isLetter(line.charAt(0)) || line.indexOf('_') != -1){
continue;
}
String[] splits = line.split(" ");
if(splits.length == 0){
continue;
}else{
words.add(splits[0]);
}
}
Words.partOfSpeech.put(partOfSpeech, words);
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){
String word = "orange";
System.out.println(getPartsOfSpeech(word));
System.out.println(numberOfSyllables(word));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.