dimanche 1 août 2021

Checking Multiple Keywords in a HashSet, and generating and outcome based on that

I have a String which contains a long paragraph. This paragraph contains various keywords, like "happy", "sad", "disappointed", "satisfied" etc. After searching for this keywords in the paragraph I am trying to generate an outcome.

public static String fileReader() {
        JSONParser parser = new JSONParser();
        String paragraph = "";
        try {
            Object obj = parser.parse(new FileReader("file_Path"));
            JSONObject jsonobject = (JSONObject) obj;
            paragraph = (String) jsonobject.get("paragraph");
            return paragraph;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return paragraph;
    }

// adding the words of the paragraph into a hashset.

public static String sentiment() {
String input20 = fileReader();
Set<String> items = new HashSet<String>(Arrays.asList(input20.split(" ")));

//Array of Keywords
String arr[] = {"happy", "sad", "disappointed", "satisfied"};

if ((items.contains("happy")) || (items.contains("satisfied")) {
return "Positive";
} else if ((items.contains("sad")) || (items.contains("disappointed"))) {
return "Negative";
} else if ((items.contains("happy")) || (items.contains("satisfied")) && (items.contains("sad")) || (items.contains("disappointed"))) {
return "Mixed";
else {
return "Unknown";
}
}

Now, I want to write an if-else loop so that I can give output based on various conditions. Conditions: (1) If contains "Happy", 'Satisfied" or(any 1 on those), positive as output. (2) if contains "Sad", "disappointed" or(any 1 on those), negative asoutput. (3) If contains "Sad", "Happy", i.e(mixture of words), mixed as output. (4) else unknown.

Can someone help me out with this logic? I always get the output as the first condition that I have kept.

Aucun commentaire:

Enregistrer un commentaire