为什么我的输入没有在我的C#WPF Chatter Bot中找到?

时间:2013-09-06 16:15:25

标签: c# wpf formatexception chatbot aiml

我正在尝试根据位于here的控制台机器人代码创建一个Chatter Bot

我在输入输入后运行程序时遇到异常:“格式异常不是  处理“”输入字符串的格式不正确。“在”alice“类中的行:

Result result = myBot.Chat(request);

似乎请求返回null。有人可以帮我弄清楚为什么我的输入没有在AIML文件中找到和/或为什么它返回null?

以下是Main类:

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;
        }
    } 
}

下面是“alice”类代码:

//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);
        }
    }

}

以下是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>

1 个答案:

答案 0 :(得分:0)

您好我已根据提供的代码运行您的程序。您的问题与wpf无关。关注alice.Initialize()方法。它根本没有被调用。如果你在“getOutput”之前调用它就可以了。出于测试目的,我把它放入了alice ctor。

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