0

Ok So I have two strings. The First String is a word, the second string is a sentance. Now the sentence contains the word, and also a definition for the word. See the example below.

Word String : AED Sentence String : This will be much like the “Kindle” or automated external defibrillator (AED).

So I need to find the definition: automated external defibrillator of the word: AED.

What I need to do is parse and find the Definition. I am currently stuck and I need a little help on this. The logic below breaks the word into an array and the sentence into an array. Unfortunately this isn't complete. And also when the logic is looking at the first letter of the word it won't really work as A in AED is uppercase and a in automatic is lowercase.

private void getDefinitions(String word, String sentence) {
    if (sentence.contains(word)) {
        String[] wordStrAry = word.split("");
        String[] sentStr = sentence.split(" ");
        for (int sentInt = 0; sentInt < sentStr.length; sentInt++){
            for (int wordInt = 0; wordInt < wordStrAry.length; wordInt++) {
            wordStrAry[wordInt].trim();
                if (!wordStrAry[wordInt].equals("")) {
                    if (sentStr[sentInt].startsWith(wordStrAry[wordInt])){
                        System.out.println(sentStr[sentInt]);
                    }
                }
            }
        }
    }
}

One little bit of information I forgot is I need to pull the definition out of the sentence and display it in a text box.

4
  • So, you're wanting to take in a sentence and find any acronyms or initialisms, then look for what they stand for in the sentence? Commented Sep 26, 2012 at 19:53
  • Using this code, you can get any arbitrary acronyms for your word.. It would be better if you keep some character to connect your word with its meaning in your sentence, so that you don't have to do so much of comparisons.. Commented Sep 26, 2012 at 20:05
  • What should happen when you get "Word String : AED Sentence String : This will be much like an electronic device such as the “Kindle” or automated external defibrillator (AED)."? Commented Sep 26, 2012 at 21:00
  • Automated External Defibrillator Commented Sep 27, 2012 at 19:20

6 Answers 6

1
public static String getDefinition(String acronym, String sentence) {
    if (!sentence.toLowerCase().contains(acronym.toLowerCase())) {
        return null;
    }

    StringBuilder patternBuilder = new StringBuilder();
    for (char letter : acronym.toCharArray()) {
        patternBuilder.append("[");
        patternBuilder.append(Character.toLowerCase(letter));
        patternBuilder.append(Character.toUpperCase(letter));
        patternBuilder.append("]");
        patternBuilder.append("\\w*\\s+");
    }
    patternBuilder.delete(patternBuilder.length() - 3, patternBuilder.length());

    Pattern pattern = Pattern.compile(patternBuilder.toString());
    Matcher matcher = pattern.matcher(sentence);
    if (!matcher.find()) {
        return null;
    }

    return matcher.group();
}

public static void main(String[] args) {
    String acronym = "AED";
    String sentence = "This will be much like the \"Kindle\" or Automated External Defibrillator (AED)";
    String definition = getDefinition(acronym, sentence);
    if (definition != null) {
        System.out.println(acronym + " = " + definition);
    } else {
        System.out.println("There is no definition for " + acronym);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I think that may be what I am looking for. I had to use RegEx to get the original Acronym. Let me check it out.
I tried this out and it returns the lower case letters. I need the method to return the original string.
Modified the code and now it returns original string, not lower case.
MateuszB I have a question for ya if you could contact me.
0

why break it into an array?

you can just use the String contains method

sentence.contains(word)

If this returns true it does contain it. Note this is case senstive. if you want it to be case insensitive then you may want to do

sentence.toLowerCase().contains(word.toLowerCase())

8 Comments

He already does this (first line). But I don't know why he needs the code below...
Cool so what if it is found then what. I need to pull the definition out of the sentence. That is why I put it into an array. If you have a better idea that would be great.
See what I need is to pull the definition out of the sentence and display it on a text box.
If sentence contains AED then you know it exists so you can just populate AED in the text box cant you? You dont need to parse the sentence again
I already pull AED out of the sentence but I need to pull the definition "automated external defibrillator" out of the sentence as well. So I am asking how do I do that.
|
0

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).

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 < words.length; i++)
    {
        phrase.append(words[i].toUpperCase().charAt(0));
        if(words[i].length() > 1)
        {
            phrase.append(words[i].substring(1));
        }
        if((i + 1) < 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 < 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();
}

Running:

System.out.println(getDefinition("LVPD", "I have a good friend at the Las Vegas police department"));

Produces Output:

LVPD: Las Vegas Police Department

8 Comments

Ok this works but I need to return the definition from the sentence in it's original form not the lower case form.
The definition returned is always exactly as it appears in the text. If it's showing lower case, then that's because you used a sentence where the definition is in lower case. My example was mixed case to indicate this.
If you're wanting to change the case of the definition you could always replace phrase.append(words[i]); with phrase.append(words[i].toUpperCase().charAt(0)); if(words[i].length() > 1) phrase.append(words[i].substring(1));
I am getting a array out of bounds on initialism.append(word.toUpperCase().charAt(0));
In fact that exception would be StringIndexOutOfBoundsException, I can't work out anything that gives ArrayIndexOutOfBoundsException.
|
0
package acronym;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Acronym {

    // any sequence of "word character"(\w) between "word boundaries" (\b) that starts with two to-be-defined characters (%c) - String.format(...)
    private static final String aWordPatternFormat = "\\b[%c%c]\\w*\\b";

    public Acronym() {
        super();
    }

    public String getDefinition(String word, String sentence) {

        String regex = buildRegex(word);

        return findDefinition(regex, sentence);
    }

    private String buildRegex(String word) {

        StringBuilder builder = new StringBuilder();

        builder.append("(");

        for (int i = 0; i < word.length(); i++) {

            char ch = word.charAt(i);

            String aWordPatternRegex = String.format(aWordPatternFormat, Character.toUpperCase(ch), Character.toLowerCase(ch));

            // ignore any spaces before the first word
            if(i != 0) {
                builder.append("\\s");
            }

            // add the word regex to the phrase regex we are building
            builder.append(aWordPatternRegex);
        }

        builder.append(")");

        return builder.toString();
    }

    private String findDefinition(String regex, String sentence) {

        Pattern pattern = Pattern.compile(regex);

        Matcher matcher = pattern.matcher(sentence);

        boolean matches = matcher.find();

        if(!matches) {
            throw new RuntimeException("The sentence does not contains the definition of the word");
        }

        return matcher.group();
    }

}

--- JUnit test ---

package acronym;

import static org.junit.Assert.assertEquals;

import org.junit.Test;


public class AcronymTest {

    @Test
    public void testGetDefinitions() {

        assertEquals("automated external defibrillator", new Acronym().getDefinition("AED", "This will be much like the “Kindle” or automated external defibrillator (AED)"));
        assertEquals("Las Vegas police department", new Acronym().getDefinition("LVPD", "I have a good friend at the Las Vegas police department shall"));
    }


}

3 Comments

Actually This has been the best solution so far. But I need to consider words with special characters like - between them to be one word. How would I do that.
kinda like Save-On should be considered one word not two
How do you want it to work? How do you want "Save-On" to be considered? As one or two words? just add a failing test case to the one above!
0
package acronym;

public class Acronym {

    public String getDefinition(String word, String sentence) {
        sentence = sentence.trim();
        word = word.trim();

        final int sLength = sentence.length();
        final int wLength = word.length();

        int startPos = 0, endPos = sLength;
        int w = 0, s = 0;

        if(equalsIgnoringCase(sentence, s, word, w)) {
            startPos = s;
            w++; s++;
        }

        for (; s < sLength; s++) {
            if(sentence.charAt(s) == ' ') {
                if(w == 0) {
                    startPos = s + 1;
                }
                if(w == wLength) {
                    endPos = s;
                    break;
                }
                if(equalsIgnoringCase(sentence, s + 1, word, w)) {
                    w = (w < wLength) ? w + 1 : wLength;
                }
                else {
                    w = 0;
                }
            }
        }
        return sentence.substring(startPos, endPos);
    }

    private boolean equalsIgnoringCase(String sentence, int s, String word, int w) {
        return equalsIgnoringCase(sentence.charAt(s), word.charAt(w));
    }

    private boolean equalsIgnoringCase(char sCharAt, char wCharAt) {
        return Character.toLowerCase(sCharAt) == Character.toLowerCase(wCharAt);
    }

}

JUnit test for the previous example:

package acronym;

import static org.junit.Assert.assertEquals;

import org.junit.Test;


public class AcronymTest {

    @Test
    public void testGetDefinitions() {

        assertEquals("automated external defibrillator", new Acronym().getDefinition("AED", "This will be much like the “Kindle” or automated external defibrillator (AED)"));
        assertEquals("Las Vegas police department", new Acronym().getDefinition("LVPD", "I have a good friend at the Las Vegas police department shall"));
    }


}

Comments

0
public class AcronymSplit implements Acronym {

public AcronymSplit() {
    super();
}

@Override
public String getDefinition(String word, String sentence) {
    String[] split = sentence.replaceAll("[^A-Za-z\\s]", "").split("[^\\w]", -1);

    StringBuilder builder = new StringBuilder();
    for (String string : split) {
        builder.append(string.charAt(0));
    }
    int index = builder.toString().toLowerCase().indexOf(word.toLowerCase());
    builder = new StringBuilder();
    for (int i = index; i < (index + word.length()); i++) {
        if(i != index) {
            builder.append(' ');
        }
        builder.append(split[i]);
    }
    return builder.toString();
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.