Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I've understood you correctly, this will search for the initialism you specify in the sentence you specify then locate the phrase that matches that initialism and return them. Note that this will fail if there are multiple possibilities and an incorrect one appears before the correct one. I just can't think of a way to avoid that (although you could reduce it by working out which is closer to where the acronym appears).</p> <pre><code>public static String makeInitialism(String[] words) { StringBuilder initialism = new StringBuilder(); for(String word : words) { initialism.append(word.toUpperCase().charAt(0)); } return initialism.toString(); } public static String buildPhrase(String[] words) { StringBuilder phrase = new StringBuilder(); for(int i = 0; i &lt; words.length; i++) { phrase.append(words[i].toUpperCase().charAt(0)); if(words[i].length() &gt; 1) { phrase.append(words[i].substring(1)); } if((i + 1) &lt; words.length) { phrase.append(" "); } } return phrase.toString(); } public static String getDefinition(String word, String sentence) throws DefinitionNotFoundException { //StackOverflow removes double spaces, you can replace " "+" " with a double space in your code. sentence = sentence.replace(" "+" ", " "); String[] words = sentence.split(" "); int wordsToJoin = word.length(); word = word.toUpperCase(); for(int i = 0; i &lt; words.length - (wordsToJoin - 1); i++) { String[] tryingWords = Arrays.copyOfRange(words, i, i + wordsToJoin); if(word.equals(makeInitialism(tryingWords))) { return word + ": " + buildPhrase(tryingWords); } } throw new DefinitionNotFoundException(); } </code></pre> <p>Running:</p> <pre><code>System.out.println(getDefinition("LVPD", "I have a good friend at the Las Vegas police department")); </code></pre> <p>Produces Output:</p> <pre><code>LVPD: Las Vegas Police Department </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload