I'm working on a project where I want to be able to be able to parse some text and find nouns and a lot of the text I want to parse has pronouns in it for Example => "Emma the parrot was a bird. She lived in a tall tree".
I don't want to work with "She's" etc. as they aren't seen as nouns in the dictionary I'm working with so I've been working on a method to replace She etc with the previous occurrence of a name. So the above example would output to => "Emma the parrot was a bird. Emma lived in a tall tree".
The method is working fine when I have a small sample however when I'm working with 3-4 different people in one text it doesn't work.
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("\\]", "");
}
It takes in my text
String text = "Karla was a bird and she had beautifully colorful feathers. She lived in a tall tree.
And a "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");
But it always outputs Karla in this example, even if we had "The hunter shot his gun" in the same string. Any help on why this isn't working would be appreciated