Question: Java, Challenging Question, Hi ive been attempteing to complete this question for almost 2 weeks and have had no success. If anyone could give me

Java, Challenging Question,

Hi ive been attempteing to complete this question for almost 2 weeks and have had no success. If anyone could give me the correct answer to this I would truly appreciate it.

Question: Given a String with multiple lines, return its rhyme scheme with the first line labeled A and each new ending syllable is B, then C, etc. You don't have to identify if the scheme repeats so if the input has 10 lines that follow an AB rhyming scheme the output will be "ABABABABAB" // In case this doesn't make sense, see Wikipedia's description of rhyme schemes which is more clear and // and in depth than the above description: https://en.wikipedia.org/wiki/Rhyme_scheme // // You may assume that the input has at most 9 different rhymes (ie. will use the letters A-I)

static String Q3(String lyrics){

return ""; }

here is the class words that must be used to complete this question

public class Words{

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> dictionary = null; private static Map> partOfSpeech = null;

/** * Return the number of syllables in the input word. */ public static int numberOfSyllables(String word){ int syllables = 0; List 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 getPartsOfSpeech(String word){ ArrayList 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 sounds1 = Words.wordsToSounds(word1); List 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 sounds1 = Words.wordsToSounds(word1); List 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 wordsToSounds(String word){

Map> 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> getDictionary(){

if(Words.dictionary == null){

Words.dictionary = new HashMap<>();

Set 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 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> 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 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)); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!