Java中的字符串数组并解析它们

时间:2012-09-26 19:46:01

标签: java string

好的我有两个字符串。 First String是一个单词,第二个字符串是一个sentance。现在,句子包含单词,也是单词的定义。请参阅下面的示例。

单词字符串: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]);
                    }
                }
            }
        }
    }
}

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

6 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

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

您可以使用字符串contains方法

sentence.contains(word)

如果返回true,则确实包含它。请注意,这是一个案例敏感。如果你希望它不区分大小写,那么你可能想要

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

答案 2 :(得分: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

答案 3 :(得分: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"));
    }


}

答案 4 :(得分: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"));
    }


}

答案 5 :(得分: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();
}

}