Проблемы с Chatbot - PullRequest
       5

Проблемы с Chatbot

0 голосов
/ 25 сентября 2018

По некоторым причинам я получаю ошибки нулевого указателя в коде.Я работаю с книгой примеров и пытаюсь сделать что-то свое, но похоже, что есть изменение в Java или ошибка в книге.Я делаю чатбота (своего рода ИИ) и не могу заставить его работать в Java.Когда я запускаю программу, она позволяет мне писать только Hi, а затем воспроизводится.Когда я пытаюсь дать ему другое слово вроде engi, он действует так, как я всегда говорю hi.Если я перезагружаюсь и пишу сначала engi, то всплывает ошибка, по некоторым причинам нулевой указательКто-нибудь может помочь?

import java.io.IOException;
import java.util.Scanner;

import org.apache.http.client.ClientProtocolException;

import com.google.gson.*;

public class engibot 
{
    JsonObject context;

    public static void main (String[] args) {
        engibot c= new engibot();
        Scanner scanner= new Scanner(System.in);
        String userUtterance;

        do {
            System.out.print("User:");
            userUtterance=scanner.nextLine();
            //end conversation
            if (userUtterance.equals("QUIT")) {break;}

            JsonObject userInput=new JsonObject();
            userInput.add("userUtterance", new JsonPrimitive(userUtterance));
            JsonObject botOutput = c.process(userInput);
            String botUtterance = "";
            if(botOutput !=null && botOutput.has("botUtterance")) {
                botUtterance = botOutput.get("botUtterance").getAsString();
            }
            System.out.println("Bot:" + botUtterance);
            }while(true);

        scanner.close();
        }
    public engibot() {
        context =new JsonObject();
        }
    public JsonObject process(JsonObject userInput) {
    //step 1: process user input

        JsonObject userAction = processUserInput(userInput);

    //step 2: update context
        updateContext(userAction);

    //step 3: identify bot intent
        identifyBotIntent();

    //step 4: structure output
        JsonObject out = getBotOutput();

    return out;
    }
public JsonObject processUserInput(JsonObject userInput) {
    String userUtterance = null;
    JsonObject userAction = new JsonObject();

    //default case
    userAction.add("userIntent", new JsonPrimitive(""));

    if(userInput.has("userUtterance")) {
        userUtterance= userInput.get("userUtterance").getAsString();
        userUtterance=userUtterance.replaceAll("%2C", ",");
    }
    if (userUtterance.matches("(hi)|(hi | hello)( there)?")) {
        userAction.add("userIntent", new JsonPrimitive("greet"));
    }
    else if (userUtterance.matches("(thanks)|(thank you)")) {
        userAction.add("userIntent", new JsonPrimitive("thank"));
    }
    else if(userUtterance.matches("(engi) | (engagment)")) {
        userAction.add("userIntent", new JsonPrimitive("request_engi"));
    }
    else {

        //contextual processing

        String currentTask = context.get("currentTask").getAsString();
        String botIntent = context.get("botIntent").getAsString();
        if(currentTask.equals("requestEngi") && botIntent.equals("requestUserName")) {
            userAction.add("userIntent", new JsonPrimitive("inform_name"));
            userAction.add("userName", new JsonPrimitive(userUtterance));
        }
    }

    return userAction;
}
public void updateContext(JsonObject userAction) {
    //copy userIntent
    context.add("userIntent", userAction.get("userIntent"));

    String userIntent = context.get("userIntent").getAsString();
    if(userIntent.equals("greet")) {
        context.add("currentTask", new JsonPrimitive("greetUser"));
    }else if (userIntent.equals("request_engi")) {
        context.add("currentTask", new JsonPrimitive("requestEngi"));
    }else if (userIntent.equals("infrom_city")) {
        String userName = userAction.get("userName").getAsString();
        Engi userInfo = DB.getUserInfo(userName);
        if(!userInfo.get("userName").isJsonNull()) {
            context.add("placeOfWeather", userInfo.get("cityCode"));
            context.add("placeName", userInfo.get("cityName"));
        }
    }else if (userIntent.equals("thank")) {
        context.add("currenTask", new JsonPrimitive("thankUser"));
    }
}
public void identifyBotIntent() {
    String currentTask = context.get("currentTask").getAsString();
    if(currentTask.equals("greetUser")) {
        context.add("botIntent", new JsonPrimitive("greetUser"));
    }else if(currentTask.equals("thankUser")) {
        context.add("botIntent", new JsonPrimitive("thankUser"));
    }else if (currentTask.equals("requestEngi")) {
        if (context.get("userName").getAsString().equals("unknown")) {
            context.add("botIntent", new JsonPrimitive("requestUserName"));
        }
        else {
            Integer time = -1;
            if (context.get("").getAsString().equals("current")) {
                time=0;
            }
            Engi userReport =null;
            userReport = DB.getUserInfo(
             context.get("userName").getAsString());
            if(userReport !=null) {
                context.add("userReport", new JsonPrimitive("userReport"));
                context.add("botIntent", new JsonPrimitive("informUser"));
            }
        }
    }else {
        context.add("botIntent", null);
    }
}
public JsonObject getBotOutput() {
    JsonObject out=new JsonObject();
    String botIntent = context.get("botIntent").getAsString();
    String botUtterance="";
    if(botIntent.equals("greetUser")) {
        botUtterance= "Hi there! I am EngiMan, your engagement bot! " + "What would you like to do? Engage someone or something else?";
        }else if(botIntent.equals("thankUser")) {
            botUtterance="Thanks for talking to me! Have a great day!!";
        }else if (botIntent.equals("requestName")) {
            botUtterance="Ok. What's his name?";
        }else if(botIntent.equals("informUser")) {
            String userDescription= getUserDescription(context.get("userName").getAsString());
            String engiIndex= getEngiIndex();
            botUtterance = "Ok. "+"Engagment index of "+userDescription+" is"+engiIndex+".";
        }
    out.add("botIntent", context.get("botIntent"));
    out.add("botUtterance", new JsonPrimitive(botUtterance));
    return out;
}
private String getEngiIndex() {
    return context.get("engiIndex").getAsString();
}
private String getUserDescription(String userName) {
    if (userName.equals("current")) {
        return "current";
    }
    return null;
}
}






    import com.google.gson.JsonElement;

public class Engi {
private String Name;
private String LastName;
private int Age;
private double EngiIndex;
private String Firm;
private String Team;

public JsonElement get(String string) {
    // TODO Auto-generated method stub
    return null;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getLastName() {
    return LastName;
}

public void setLastName(String lasteName) {
    LastName = lasteName;
}

public int getAge() {
    return Age;
}

public void setAge(int age) {
    Age = age;
}

public double getEngiIndex() {
    return EngiIndex;
}

public void setEngiIndex(double engiIndex) {
    EngiIndex = engiIndex;
}

public String getFirm() {
    return Firm;
}

public void setFirm(String firm) {
    Firm = firm;
}

public String getTeam() {
    return Team;
}

public void setTeam(String team) {
    Team = team;
}
}
...