0

好的,所以我有两个字符串。第一个字符串是一个单词,第二个字符串是一个句子。现在句子包含了这个词,也包含了这个词的定义。请参见下面的示例。

字串:AED 句子串:这很像“Kindle”或自动体外除颤器 (AED)。

所以我需要找到定义:自动体外除颤器这个词:AED。

我需要做的是解析并找到定义。我目前陷入困境,我需要一些帮助。下面的逻辑将单词分解为数组,将句子分解为数组。不幸的是,这并不完整。而且,当逻辑查看单词的第一个字母时,它不会真正起作用,因为 AED 中的 A 是大写字母,而自动中的 a 是小写字母。

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

我忘记的一点信息是我需要从句子中提取定义并将其显示在文本框中。

4

6 回答 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);
    }
}
于 2012-09-26T21:33:02.540 回答
0

为什么要把它分成一个数组?

你可以只使用 String contains方法

sentence.contains(word)

如果返回 true,它确实包含它。请注意,这是区分大小写的。如果您希望它不区分大小写,那么您可能想要这样做

sentence.toLowerCase().contains(word.toLowerCase())
于 2012-09-26T19:49:47.053 回答
0

如果我对您的理解正确,这将搜索您在您指定的句子中指定的首字母缩写词,然后找到与该首字母缩写词匹配的短语并返回它们。请注意,如果有多种可能性并且错误的可能性出现在正确的可能性之前,这将失败。我只是想不出一种方法来避免这种情况(尽管您可以通过找出更接近首字母缩写词出现的位置来减少它)。

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

跑步:

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

产生输出:

LVPD: Las Vegas Police Department
于 2012-09-26T20:27:26.207 回答
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 测试 ---

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


}
于 2012-09-27T23:19:46.187 回答
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 测试:

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


}
于 2012-09-27T23:25:43.750 回答
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();
}

}

于 2013-10-25T18:50:39.003 回答