替换整个字符串中的代词
Replacing pronouns throughout a String
我正在做一个项目,我希望能够解析一些文本并找到名词,我想解析的很多文本中都有代词,例如 => "Emma the parrot was a bird. She lived in a tall tree".
我不想使用 "She's" 等,因为在我使用的词典中它们不被视为名词,所以我一直在研究一种方法来替换 She 等名字的前一次出现。所以上面的例子会输出到 => "Emma the parrot was a bird. Emma lived in a tall tree".
当我有一个小样本时,该方法工作正常,但是当我在一个文本中与 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.
还有一个"dictionary"
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,即使我们在同一个字符串中有 "The hunter shot his gun"。
任何关于为什么这不起作用的帮助将不胜感激
这是行不通的,因为即使在字典中找到匹配项后,您仍继续循环 j
。也就是说 - 你一直向后看字符串的开头,并最终找到 "Karla",即使你已经匹配了 "hunter".
您可以通过多种方式解决此问题。一个非常简单的方法是将 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("\]", "");
}
如果你以更标准的方式放置你的 }
个字符,这种错误会更容易被发现。
我正在做一个项目,我希望能够解析一些文本并找到名词,我想解析的很多文本中都有代词,例如 => "Emma the parrot was a bird. She lived in a tall tree".
我不想使用 "She's" 等,因为在我使用的词典中它们不被视为名词,所以我一直在研究一种方法来替换 She 等名字的前一次出现。所以上面的例子会输出到 => "Emma the parrot was a bird. Emma lived in a tall tree".
当我有一个小样本时,该方法工作正常,但是当我在一个文本中与 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.
还有一个"dictionary"
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,即使我们在同一个字符串中有 "The hunter shot his gun"。 任何关于为什么这不起作用的帮助将不胜感激
这是行不通的,因为即使在字典中找到匹配项后,您仍继续循环 j
。也就是说 - 你一直向后看字符串的开头,并最终找到 "Karla",即使你已经匹配了 "hunter".
您可以通过多种方式解决此问题。一个非常简单的方法是将 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("\]", "");
}
如果你以更标准的方式放置你的 }
个字符,这种错误会更容易被发现。