在整个字符串中替换代词

时间:2017-10-17 18:28:53

标签: java text-processing

我正在开展一个项目,我希望能够解析一些文本并找到名词,我要解析的很多文本中都有代词= Example => "艾玛鹦鹉是一只鸟。她住在一棵高大的树上,#34。

我不想和她一起工作#34;她"因为它们在我所使用的词典中不被视为名词,所以我一直在研究一种方法,用以前出现的名字取代She等。所以上面的例子将输出到=> "艾玛鹦鹉是一只鸟。艾玛住在一棵高大的树上,#34;。

当我有一个小样本时,该方法工作正常但是当我在一个文本中与3-4个不同的人一起工作时,它无法正常工作。

public static String replacePronouns(String text, ArrayList<String> dictionary) {
        String[] strArray = text.replaceAll("\\.", " .").replaceAll("\\,", "").split("\\s+");
        String previousName = "";
        for(int i = 0; i < strArray.length; i++ ) {
            //we'll have to set this to be more dynamic -> change to pronouns in dicitonary
            if(strArray[i].equals("His") || strArray[i].equals("She") || strArray[i].equals("she") || strArray[i].equals("him") || strArray[i].equals("he") || strArray[i].equals("her")) {
                for(int j = (i-1); j>=0; j--) {
                    int count = dictionary.size()-1;
                    boolean flag = false;
                    while(count>=0 && flag==false) {
                        if(strArray[j].equals(dictionary.get(count).split(": ")[1]) && dictionary.get(count).split(": ")[0].equals("Name")) {
                            previousName = strArray[j];
                            flag = true; }
                        count--;
                    } }
                strArray[i] = previousName; } }
        return Arrays.toString(strArray).replaceAll("\\[", "").replaceAll("\\,", "").replaceAll("\\]", "");
    }

它接收我的文字

String text = "Karla was a bird and she had beautifully colorful feathers. She lived in a tall tree.

和#34;字典&#34;

ArrayList<String> dictionary = new ArrayList<>();
        dictionary.add("Name: hunter");
        dictionary.add("Name: Karla");
        dictionary.add("Noun: hawk");
        dictionary.add("Noun: feathers");
        dictionary.add("Noun: tree");
        dictionary.add("Noun: arrows");
        dictionary.add("Verb: was a");
        dictionary.add("Verb: had");
        dictionary.add("Verb: missed");
        dictionary.add("Verb: knew");
        dictionary.add("Verb: offered");
        dictionary.add("Verb: pledged");
        dictionary.add("Verb: shoot");

但是在这个例子中它总是输出Karla,即使我们有&#34;猎人射击他的枪&#34;在同一个字符串中。 任何关于为什么这不起作用的帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

这不起作用,因为即使在词典中找到匹配项后,您仍继续循环objc_boxable。那就是 - 你继续回头看弦乐的开头,并最终找到“Karla”,即使你已经匹配了“猎人”。

有很多方法可以解决这个问题。一个非常简单的方法是将j移至boolean flag = false;循环for之前,并将条件从j更改为j >= 0,以便您j >= 0 && !flag成立后立即停止循环。像这样:

flag

如果您以更标准的方式放置public static String replacePronouns(String text, ArrayList<String> dictionary) { String[] strArray = text.replaceAll("\\.", " .").replaceAll("\\,", "").split("\\s+"); String previousName = ""; for (int i = 0; i < strArray.length; i++) { boolean flag = false; // we'll have to set this to be more dynamic -> change to pronouns in dicitonary if (strArray[i].equals("His") || strArray[i].equals("She") || strArray[i].equals("she") || strArray[i].equals("him") || strArray[i].equals("he") || strArray[i].equals("her")) { for (int j = (i - 1); j >= 0 && flag == false; j--) { int count = dictionary.size() - 1; while (count >= 0) { if (strArray[j].equals(dictionary.get(count).split(": ")[1]) && dictionary.get(count).split(": ")[0].equals("Name")) { previousName = strArray[j]; flag = true; } count--; } } strArray[i] = previousName; } } return Arrays.toString(strArray).replaceAll("\\[", "").replaceAll("\\,", "").replaceAll("\\]", ""); } 个字符,则会更容易看到此类错误。

相关问题