Question

In: Computer Science

Working in a technical field can be confusing because of all the acronyms. For example, CPU...

Working in a technical field can be confusing because of all the acronyms. For example, CPU means Central Processing Unit, HDMI means High-Definition Multimedia Interface, and SMART means Self-Monitoring Analysis and Reporting Technology. Wouldn't it be nice if there was a way to translate all these acronyms automatically?

Create a new file named Translate.java that contains the following method:

public static String expandAll(String[] acronyms, String[] definitions, String text)

This method should replace every instance of an acronym in the text with its corresponding definition. For example, expandAll({"JMU", "CS"}, {"James Madison University", "Computer Science"}, "JMU CS rocks!") would return the string "James Madison University Computer Science rocks!".

You may assume that the two arrays will be the same length. You may also assume that the text will be reasonable English with correct grammar. In particular, there will be only one space between each word. Each sentence will either end with a space or a newline character. Punctuation not at the end of the sentence will always be followed by whitespace (i.e., you should be able to handle "Hi, Mom" but you won't have to handle "Hi,Mom").

Be careful not to replace acronyms in the middle of a word. For example, the string "CS uses MACS!" should expand to "Computer Science uses MACS!", not "Computer Science uses MAComputer Science!". You might find it helpful to use a Scanner to process the string one word at a time.

Solutions

Expert Solution

Program Code to Copy:


public class Translate {

   public static String expandAll(String[] acronyms, String[] definitions, String text) {
      
   String result = ""; // the final processed text to return
       String word = ""; // a single word in text
       char lastChar = ' '; // last character of a word in text
       boolean isPunctuation;
       boolean acronymFound; // true only if a word in text is found in acronyms
     
       String[] wordsInText = text.split(" "); // get all words in text
      
       for(int i=0;i< wordsInText.length;i++) {
          
           acronymFound = false; //a word in text is not an acronym until proven otherwise
           isPunctuation = false;
          
           word = wordsInText[i]; // pick a word from text
           lastChar = word.charAt(word.length()-1); // get the last character of the word form text
          
           if(!Character.isLetter(lastChar)) { // true only if last character of word is not a letter [a-z,A-Z]
               // remove the last character from word
               word = word.split(String.valueOf(lastChar))[0]; // by splitting word at that character and picking the first part
               isPunctuation = true;
           }
           // compare word with all acronyms to find a match if any
           for(int j=0; j<acronyms.length;j++) {
               if(word.equals(acronyms[j])) { // match is found
                   // update result with acronym's corresponding definition appended with a space
                   if(isPunctuation)
                       result += definitions[j]+lastChar+" "; // add punctuation (lastChar)
                   else
                       result += definitions[j]+" ";
                  
                   acronymFound = true;
                   break; // for this word, stop the search
               }
                  
           }
           // case when none of the acronyms matched the word in text
           if(!acronymFound)
               result += wordsInText[i]+" "; // update result with the actual word in text appended with a space
          
       }
       return result; // finally when every word in text has been compared with all acronyms
      
   }
  
   // testing the method
   public static void main(String[] args) {
      
       String[] acronyms = {"JMU", "CS"};
       String[] definitions = {"James Madison University", "Computer Science"};
       String text = "JMU CS rocks!";
       String result = expandAll(acronyms,definitions,text); // call the method
       System.out.println(result);      
   }

}

------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~


Related Solutions

3. In the Solow model with technical progress, capital shallowing can occur because of Question 3...
3. In the Solow model with technical progress, capital shallowing can occur because of Question 3 options: depreciation. technological growth. population growth. all of the above. 4. According to the Solow model with the technical progress, the expected co-efficient for saving is Question 4 options: 1.47 0.5 -0.5 1.91
how can normal distribution be applied in the field of medicine. with example
how can normal distribution be applied in the field of medicine. with example
IT IS FOR MT TECHNICAL COMMUNICATION CLASS. CAN YOU PLEASE ANSWER ALL TEHSE QUASTION AS A...
IT IS FOR MT TECHNICAL COMMUNICATION CLASS. CAN YOU PLEASE ANSWER ALL TEHSE QUASTION AS A SHORT ANSWER QUATION 1. What is the difference between academic research and workplace research? Give an example of each. 2. What is a logical fallacy? Provide 3 logical fallacies and their definitions. 3. What are four cultural variables to consider when communicating with an international company? Name the significance of one of these variables. Kindly answer these as a short answer type question
Imagine many technical workers can now earn more income working from home for themselves over the...
Imagine many technical workers can now earn more income working from home for themselves over the internet rather than working for a firm. Which of the following is mostly likely to be a consequence for the remaining labor market for technical workers (the market in which firms hire workers)? Group of answer choices A decrease in the wage of technical workers employed by firms and an increase in the number of employed technical workers. An increase in the wage of...
What are all the different types of rock bodies that can be found in Space? Example:...
What are all the different types of rock bodies that can be found in Space? Example: Meteors, Asteriods, Comets, planetiods. I will thumbs up all answers. You don't have to mention all the rock bodies that exist, just as much as you can.
Can you all show me an specialization and production possibilities model and example?
Can you all show me an specialization and production possibilities model and example?
Can you make an example of those issues as follows: A clear list of all compensation...
Can you make an example of those issues as follows: A clear list of all compensation on an annual basis (presented in a spreadsheet format). Compensation in tax, and label each item as salary, taxable fringe, non-taxable fringe.
Can someone go over and explain all the intermolecular forces? Please give an example of each.
Can someone go over and explain all the intermolecular forces? Please give an example of each.
A) What is the float and how can a company use it for working capital? Provide an example that illustrates how it happens. B) If the company records indicate the following:
 A) What is the float and how can a company use it for working capital? Provide an example that illustrates how it happens.B) If the company records indicate the following:Opening amount $10,000Deposits 30,000Cheques -22,000Closing balance $18,000 The bank statement indicates that the opening amount is $10,000, the deposits are 22,000 and the cheques that have cleared are 2,000, what is the amount of the float?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT