Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/SuK-IT/ChatBot into NF
Browse files Browse the repository at this point in the history
  • Loading branch information
Corvex-2 committed Sep 9, 2021
2 parents d332cf4 + 44358fa commit 432e98e
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 35 deletions.
48 changes: 48 additions & 0 deletions backend/main/java/de/sukit/chatbot/DictionaryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* DictionaryHandler reads the <code>data/dictionary.json</code>-file into a String,String HashMap.
Expand All @@ -43,6 +45,7 @@ public class DictionaryHandler {
private final File FILE_DICTIONARY = new File("./data/dictionary.json");

private HashMap<String, String> dictionary;
private List<DictionaryModel> dictionaryModelList;

/**
* Constructor for our Dictionary-class. This makes a call to {@link #setDictionary()}, thus ensuring we have a hashmap
Expand All @@ -52,6 +55,7 @@ public class DictionaryHandler {
@Autowired
public DictionaryHandler() {
setDictionary();
setDictionaryList();
}

/**
Expand Down Expand Up @@ -124,6 +128,50 @@ public void setDictionary() {
this.dictionary = dic;
}

/**
* Creates a list of {@link DictionaryModel} with priority, keyword and response for each object in the dictionary.
* @author Griefed
*/
public void setDictionaryList() {

dictionaryModelList = new ArrayList<>();

try {

JsonNode dictionaryJson = getJson(getFILE_DICTIONARY());

JsonNode keywords = dictionaryJson.get("keywords");

for (JsonNode pair : keywords) {
if (pair.get("priority").asInt() == 1) {
dictionaryModelList.add(new DictionaryModel(pair.get("priority").asInt(), pair.get("keyword").asText(), pair.get("response").asText()));
}
}
for (JsonNode pair : keywords) {
if (pair.get("priority").asInt() == 2) {
dictionaryModelList.add(new DictionaryModel(pair.get("priority").asInt(), pair.get("keyword").asText(), pair.get("response").asText()));
}
}
for (JsonNode pair : keywords) {
if (pair.get("priority").asInt() == 3) {
dictionaryModelList.add(new DictionaryModel(pair.get("priority").asInt(), pair.get("keyword").asText(), pair.get("response").asText()));
}
}

} catch (NullPointerException | IOException ex) {
LOG.error("Error reading json.", ex);
}
}

/**
* Getter for the {@link DictionaryModel} list.
* @author Griefed
* @return List DictionaryMode. Returns the list containing our dictionary.
*/
public List<DictionaryModel> getDictionaryList() {
return dictionaryModelList;
}

/**
* Getter for the dictionary used for keyword-recognition.
* @author Griefed
Expand Down
87 changes: 87 additions & 0 deletions backend/main/java/de/sukit/chatbot/DictionaryModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package de.sukit.chatbot;

/**
* Represents an entry in the dictionary list. Each entry in the dictionary-list contains one of:<br>
* {@link #priority}<br>
* {@link #keyword}<br>
* {@link #response}<br>
* which will then be able by iterating through the list and using either<br>
* {@link #getPriority()}<br>
* {@link #getKeyword()} and<br>
* {@link #getResponse()}
* @author Griefed
*/
public class DictionaryModel {

/**
* Constructor. In order to create a new entry in the dictionary list, you need to pass the priority as int, keyword
* as string and reponse as string.
* @author Griefed
* @param priority Int. The priority of the dictionary keyword/response.
* @param keyword String. The keyword for the response.
* @param response String. The response.
*/
public DictionaryModel(int priority, String keyword, String response) {
setPriority(priority);
setKeyword(keyword);
setResponse(response);
}

private int priority;
private String keyword;
private String response;

/**
* Getter for the priority of the keyword/response-pair.
* @author Griefed
* @return Int. Returns the priority of the keyword/response-pair.
*/
public int getPriority() {
return priority;
}

/**
* Setter for the priority of the keyword/response-pair.
* @author Griefed
* @param priority Int. The priority of the keyword/response-pair. Must be 1, 2 or 3.
*/
public void setPriority(int priority) {
this.priority = priority;
}

/**
* Getter for the keyword of the keyword/response-pair.
* @author Griefed
* @return String. Returns the keyword of the keyword/response-pair.
*/
public String getKeyword() {
return keyword;
}

/**
* Setter for the keyword of the keyword/response-pair.
* @author Griefed
* @param keyword String. The keyword in the keyword/response-pair.
*/
public void setKeyword(String keyword) {
this.keyword = keyword;
}

/**
* Getter for the response of the keyword/response-pair.
* @author Griefed
* @return String. Returns the response of the keyword/response-pair.
*/
public String getResponse() {
return response;
}

/**
* Setter for the response of the keyword/response-pair.
* @author Griefed
* @param response String. The reponse in the keyword/response-pair.
*/
public void setResponse(String response) {
this.response = response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.springframework.web.bind.annotation.*;

/**
* RestController for responses from the dictionary based on the user-input recieved from the frontend.
* RestController for responses from the dictionary based on the user-input received from the frontend.
* @author Griefed
*/
@RestController
Expand Down Expand Up @@ -54,11 +54,18 @@ public ResponseController(ResponseHandler injectedResponseHandler, DictionaryHan
this.FILEWATCHER = injectedFileWatcher;
}

@CrossOrigin(origins = {"*"})
@GetMapping("")
public String getConcatenatedResponses(@RequestParam(value = "input", defaultValue = "hello") String input) {
LOG.info("Requested " + input);
return RESPONSEHANDLER.getConcatenatedResponses(input);
}

/**
* Getter the response from the dictionary based on the user-input recieved from the frontend.
* Getter the response from the dictionary based on the user-input received from the frontend.
* @author Griefed
* @param input User-input recieved from the frontend.
* @return String. Returns the response from the dictionary based on the user-input recieved from the frontend.
* @param input User-input received from the frontend.
* @return String. Returns the response from the dictionary based on the user-input received from the frontend.
*/
@CrossOrigin(origins = {"*"})
@GetMapping("/getResponse") // /api/talk/getResponse?input=wort0
Expand All @@ -68,10 +75,10 @@ public String getResponse(@RequestParam(value = "input", defaultValue = "hello")
}

/**
* Alternative to {@link #getResponse(String)}. Returns the reponse as json.
* Alternative to {@link #getResponse(String)}. Returns the response as json.
* @author Griefed
* @param input User-input recieved from the frontend.
* @return String. Returns the response from the dictionary based on the user-input recieved from the frontend.
* @param input User-input received from the frontend.
* @return String. Returns the response from the dictionary based on the user-input received from the frontend.
*/
@CrossOrigin(origins = {"*"})
@GetMapping("/get") // /api/talk/get?input=wort0
Expand All @@ -83,8 +90,8 @@ public String get(@RequestParam(value = "input", defaultValue = "hello") String
/**
* Alternative to {@link #getResponse(String)}. Returns the input and response as json.
* @author Griefed
* @param input User-input recieved from the frontend.
* @return String. Returns the response from the dictionary based on the user-input recieved from the frontend as well
* @param input User-input received from the frontend.
* @return String. Returns the response from the dictionary based on the user-input received from the frontend as well
* as the input.
*/
@CrossOrigin(origins = {"*"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package de.sukit.chatbot.rest.handlers;

import de.sukit.chatbot.DictionaryHandler;
import de.sukit.chatbot.DictionaryModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -34,6 +35,8 @@ public class ResponseHandler {

private final DictionaryHandler DICTIONARYHANDLER;

private String concatenatedResponses;

/**
* Constructor for the ResponseHandler which makes an instance of {@link DictionaryHandler} available to us, so we
* have access to our dictionary-hashmap.
Expand Down Expand Up @@ -63,4 +66,46 @@ public String getReponse(String input) {
}
return "No match found!";
}

/**
* Get all possible reponses from user input and concatenated them into a single String, in order of priority set in
* the dictionary.
* @author Griefed
* @param input String. The input recieved from the user-input in the frontend.
* @return String. Returns the response matching the input to the dictionary. If no match is found <code>No match found!</code>
* is returned.
*/
public String getConcatenatedResponses(String input) {
concatenatedResponses = "";

for (DictionaryModel modelEntry : DICTIONARYHANDLER.getDictionaryList()) {
if (modelEntry.getPriority() == 1 && input.replace("%20", " ").toLowerCase().contains(modelEntry.getKeyword().toLowerCase())) {

LOG.info("Match found for " + modelEntry.getKeyword());
concatenatedResponses = concatenatedResponses + " " + modelEntry.getResponse();
}
}

for (DictionaryModel modelEntry : DICTIONARYHANDLER.getDictionaryList()) {
if (modelEntry.getPriority() == 2 && input.replace("%20", " ").toLowerCase().contains(modelEntry.getKeyword().toLowerCase())) {

LOG.info("Match found for " + modelEntry.getKeyword());
concatenatedResponses = concatenatedResponses + " " + modelEntry.getResponse();
}
}

for (DictionaryModel modelEntry : DICTIONARYHANDLER.getDictionaryList()) {
if (modelEntry.getPriority() == 3 && input.replace("%20", " ").toLowerCase().contains(modelEntry.getKeyword().toLowerCase())) {

LOG.info("Match found for " + modelEntry.getKeyword());
concatenatedResponses = concatenatedResponses + " " + modelEntry.getResponse();
}
}

if (!concatenatedResponses.equals("")) {
return concatenatedResponses;
} else {
return "No match found!";
}
}
}
2 changes: 1 addition & 1 deletion backend/main/resources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This is a small project for vocational school.
To build the project and receive an executable exe/jar, execute
`./gradlew about installQuasar cleanFrontend assembleFrontend copyDist build createExe --info`
from the main directory of this repository. After the execution finishes, you'll have your `ChatBot.exe` and `ChatBot-x.x.x-SNAPSHOT.jar`
available in the `build/libs`-directory. 3
available in the `build/libs`-directory.

## 1.3 Deploying

Expand Down
Loading

0 comments on commit 432e98e

Please sign in to comment.