Question

I am trying to create a Chatter Bot based on the code for a console bot located here

I am getting an exception when I run the program after I enter an input: "Format Exception was not handled" "Input string was not in a correct format." in the "alice" class at line:

Result result = myBot.Chat(request);

It seems like the request is returning null. Could someone please help me figure out why my input is not being found in the AIML files and/or why it is returning null?

Below is the Main class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AIMLbot;

namespace Chatbot
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        String userInput = null;
        alice myAlice = new alice();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void inputButtonClick(object sender, RoutedEventArgs e)
        {
            userInput = inputTextBox.Text;
            outPutTextBlock.Text = myAlice.getOutput(userInput);
            inputTextBox.Text = "";
            userInput = null;
        }
    } 
}

Below is the "alice" class code:

//Written by Matt Gonzalez
//with help from Nicholas H.Tollervey's ConsoleBot2.5
//and AIML base files from the A.L.I.C.E Bot project

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AIMLbot;

namespace Chatbot
{
    class alice
    {
        private Bot myBot;
        private User myUser;

        /// <summary> 
        /// Create a new instance of the ALICE object 
        /// </summary>

        public alice()
        {
            myBot = new Bot();
            myUser = new User("consoleUser", myBot);
        }

        /// <summary> 
        /// This initialization can be put in the alice() method 
        /// but I kept it seperate due to the nature of my program. 
        /// This method loads all the AIML files located in the \AIML folder 
        /// </summary>

        public void Initialize()
        {
            myBot.loadSettings();
            myBot.isAcceptingUserInput = false;
            myBot.loadAIMLFromFiles();
            myBot.isAcceptingUserInput = true;
        }

        /// <summary> 
        /// This method takes an input string, then finds a response using the the AIMLbot
        /// library and returns it 
        /// </summary> 
        /// <param name="input">Input Text</param> 
        /// <returns>Response</returns>

        public String getOutput(String input)
        {
            Request request = new Request(input, myUser, myBot);
            Result result = myBot.Chat(request);
            return (result.Output);
        }
    }

}

And Below is the XAML:

<Window x:Name="chatBotWindow" x:Class="Chatbot.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Chat Bot" Height="480" Width="640" Background="White">
    <Grid x:Name="chatBotMainGrid" Background="#FFE6E6E6">
        <TextBox x:Name="inputTextBox" HorizontalAlignment="Left" Height="25"
             Margin="149,83,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="353" 
             BorderBrush="Black"/>
        <Label x:Name="inputTextLabel" Content="Input Text: " HorizontalAlignment="Left"
             Height="25" Margin="53,83,0,0" VerticalAlignment="Top" Width="96" 
             FontFamily="Neuropol" FontSize="14"/>
        <TextBlock x:Name="outPutTextBlock" HorizontalAlignment="Left" Height="290"
             Margin="149,138,0,0" TextWrapping="Wrap" VerticalAlignment="Top" 
               Width="358"><InlineUIContainer>
                <Border x:Name="outputBorder" BorderBrush="Red" BorderThickness="1"
                          Height="272" Width="355" Background="White">
                <ScrollBar x:Name="outPutScrollBar" HorizontalAlignment="Left"
                                Height="272" Margin="337,-1,-1,-1" VerticalAlignment="Top"
                                Width="10" BorderBrush="Red"/>
                </Border>
            </InlineUIContainer></TextBlock>
        <Button x:Name="inputButton" Content="Enter" HorizontalAlignment="Left" Height="25"
             Margin="507,83,0,0" VerticalAlignment="Top" Width="76" 
             FontFamily="Neuropol" FontSize="16" Click="inputButtonClick">
            <Button.Background>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="White" Offset="0"/>
                        <GradientStop Color="#FFEBEBEB" Offset="0.5"/>
                        <GradientStop Color="#FFC5BCBC" Offset="0.5"/>
                        <GradientStop Color="Red" Offset="1"/>
                    </LinearGradientBrush>
            </Button.Background>
        </Button>
        <Label x:Name="titleLabel" Content="Tony's Chat Bot" HorizontalAlignment="Left"
             Height="49" Margin="221,29,0,0" VerticalAlignment="Top" 
            Width="203" FontFamily="Arnprior" FontSize="20"/>
    </Grid>
</Window>
Was it helpful?

Solution

Hi I have ran your program based on the provided code. Your issues is not related to wpf. It is concern alice.Initialize() method. it is not invoked at all. if you invoke it before "getOutput" all works. For the test purpose I have put it into the alice ctor.

    public alice()
    {
        myBot = new Bot();
        myUser = new User("consoleUser", myBot);
        Initialize();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top