1

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

输入输入后运行程序时出现异常:“未处理格式异常”“输入字符串的格式不正确。” 在“爱丽丝”课程中:

Result result = myBot.Chat(request);

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

下面是主类:

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

下面是“爱丽丝”类代码:

//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>
4

1 回答 1

0

您好,我已经根据提供的代码运行了您的程序。您的问题与 wpf 无关。这是关注 alice.Initialize() 方法。它根本没有被调用。如果您在“getOutput”之前调用它一切正常。出于测试目的,我已将其放入 alice ctor 中。

    public alice()
    {
        myBot = new Bot();
        myUser = new User("consoleUser", myBot);
        Initialize();
    }
于 2013-09-06T16:50:39.770 回答