有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在字符串中替换代词

我正在做一个项目,我希望能够解析一些文本并找到名词,我想解析的很多文本中都有代词,例如=>;“鹦鹉艾玛是一只鸟。她住在一棵大树上。”

我不想使用“She’s”等,因为在我使用的词典中,它们不被视为名词,所以我一直在研究一种方法,用之前出现的一个名称替换She’s等。因此,上面的示例将输出到=>;“鹦鹉艾玛是一只鸟。艾玛住在一棵大树上。”

当我有一个小样本时,这种方法很有效,但是当我在一篇文本中与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.

还有一本“字典”

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

但在这个例子中,它总是输出卡拉,即使我们让“猎人开枪”在同一条线上。 如果您能帮我解释一下为什么这不起作用,我们将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    这不起作用,因为即使在字典中找到匹配项后,仍会继续在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("\\]", "");
        }
    

    如果以更标准的方式放置}字符,这种错误将更容易看到