返回首页


{S0}简介
这是一个非常简单的伊丽莎喜欢聊天BOT。我把它称为Meliza夏普。我写了有趣的学习F#语言与Visual Studio 2010附带的。有BOT在Lisp写有一些非常好的实现艾丽莎聊天,如果你想使自己的聊天BOT。搜索它。Meliza思考代码
Meliza算法是根据规则和模式匹配。这是对F#强大的功能之一。
下面的函数定义模式标识符:愿望,正,负,和"无",也当输入字符串匹配这些模式标识符。
如果输入字符串匹配与quot; wantquot;,"desirequot;,或quot; needquot;,然后输入将与希望的模式标识符匹配。
如果输入字符串匹配与quot; certainlyquot;,"yesquot;,"okquot,或quot; rightquot;,然后输入将配合正面图案标识。对其余的模式标识符。
这是F#的开发人员呼吁积极识别。作为排序甄别,将在不同类别划分的输入,在这种情况下,在不同的"臣民"的谈话,主动识别。

// Define an active recognizer for keywords

// that express desire, positive and negative statements.

let (|Wish|Positive|Negative|None|) input =

    match input with

        | "want" | "desire" | "need"

                -> Wish

        | "certainly" | "yes" | "true" | "ok" | "right"

                -> Positive

        | "not" | "no" | "don't" | "false" | "wrong"

                -> Negative

        | _ -> None

在此的其他功能,异常处理try ...建设作为条件的行为。只有转换Convert.ToDouble(输入)成功模式识别号码输入字符串匹配的将有可能的。{C}
一旦我们要匹配的字符串标记的所有主动识别的定义,我们创建的递归响应函数,它接受的第一个标记的字符串作为参数,它的其余部分。
我们寻找与上面定义的令牌,并积极识别匹配。也就是说,我们发现,在其中定义的类别或"主体"的令牌分为。
如果我们发现令牌落在预定义的类别之一,然后返回该类别通过调用相应的功能响应响应。这将结束递归过程。
如果匹配|没有(str.IndexOf("")> 0)时,我们递归地传递下一个标记和字符串的其余部分的响应函数。也就是说,我们没有发现一类为当前令牌,还有更多的令牌字符串,所以我们称之为下一个标记,其余的字符串str的响应函数,递归处理。
如果比赛时发现有|没有(str.IndexOf("")<0),也就是说,我们没有发现一类为当前令牌,并没有在令牌字符串,我们所说的下一个标记,其余字符串空的响应函数。
时语句设置一个后卫或条件限制令牌没有可能的匹配。
// main recursive response function

// find the first match with a key token

// and return a response acordingly 

let rec response (token: string) (str: string) =

   match token with

     | Hello

            -> hello_response ()

     | Bye

            -> good_bye_response ()

     | Personal

            -> personal_response str

     | Question

            -> question_response str

     | Answer

            -> answer_response str

     | Wish 

            -> wish_response str

     | Negative 

            -> negative_response str

     | Positive 

            -> positive_response str

     | Math

            -> math_response str

     | "" 

            -> none_response str

     | None when (str.IndexOf(" ") > 0) 

            -> response (str.Substring(0,str.IndexOf(" "))) 

                        (str.Substring(str.IndexOf(" ")+1))

     | None when (str.IndexOf(" ") < 0) 

            -> response str ""

     | None 

            -> math_response str

最后,我们创建了一个功能,随机选择上的"主体"的响应函数和活动模式认可。
下面是其中两个:
// Select possible responses based on random number for hello 'subject'

let hello_response () =

    let n = rand.Next(10) 

    match n with

        | 0 -> "How do you do."

        | 1 -> "Is nice talking to you."

        | 2 -> "Tell me something new."

        | 3 -> "Nice to meet you."

        | 4 -> "My pleasure."

        | 5 -> "Hi."

        | 6 -> "Hello."

        | 7 -> "Good day."

        | 8 -> "Salutation!"

        | 9 -> "Welcome!"





// Select possible responses based on random number for Good bye 'subject'

let good_bye_response () =

    let n = rand.Next(10) 

    match n with

        | 0 -> "Talk to you soon."

        | 1 -> "It was nice talking to you."

        | 2 -> "Good bye."

        | 3 -> "Stay a bit longer."

        | 4 -> "Adios amigo."

        | 5 -> "Bye."

        | 6 -> "Adios."

        | 7 -> "See you."



        | 8 -> "Please don't go"

        | 9 -> "Why are you leaving me alone?"
如何创建Visual F#的应用程序和Windows窗体文件- GT,GT;项目
创建一个新的艾丽莎的Visual F#的应用程序。
添加Windows组件的引用:在解决方案资源管理器中,右cick参考- GT;添加引用...。NET选项卡上单击并选择System.Drawing并System.Windows.Form从列表中。单击"确定"按钮。设计表单
没有在Visual Studio中的窗体设计器的F#,所以我的建议是用于其他设计师。NET语言(如VB.NET)。关闭的解决方案,或打开Visual Studio的另一个实例。文件- GT,GT;项目
创建一个新的时间的Visual Basic,Windows窗体应用。从工具箱拖动到表单中的一个TextBox。右键单击内形成- GT的TextBox属性。设置text_box名称属性。设置底部的Dock属性。从工具箱拖动到表单另一个文本框。 右键单击内形成- GT的TextBox属性。设置conversation_box名称属性。将Dock属性设置为Fill。Multiline属性设置为True。保存时间的项目。
转换到F#代码:打开艾丽莎项目。前往文件GT公开赛- GT;文件... 浏览项目文件夹,临时工程,打开Form1.Designer.vb文件。浏览到的InitializeComponent(); Form1.Designer.vb,有属性,我们必须为我们的表格。
为函数添加代码来创建我们的形式:
// Create a main form, you can create a form using

// the form designer, in another language like Vb .Net

// then open the file Form1.Designer.vb and voila!

// there is the code you can translate into F#

let main_form title =

  // text box

  let text_box = new TextBox()

  text_box.Dock <- DockStyle.Bottom 

  // conversation box

  let conversation_text = new TextBox()

  conversation_text.Dock <- DockStyle.Fill

  conversation_text.Multiline <- true

  conversation_text.WordWrap <- false

  conversation_text.Font <- new Font(string("Arial"),float32(10))

  conversation_text.ReadOnly <- true

  // main form

  let form = new Form(Text=title, Visible=true)

  form.Size <- new Size(512, 512)

  form.Controls.Add(conversation_text)

  form.Controls.Add(text_box)

  // Message handler for Key Down event

  text_box.KeyDown.Add(enter_message text_box conversation_text)

  //

  let gotfocus = text_box.Focus()

  //

  form.KeyDown.Add(fun e -> if e.KeyCode = Keys.Escape then form.Close());

  form

生成并运行应用程序。打开艾丽莎项目。项目- GT;伊丽莎属性...在"应用程序"选项卡,设置输出类型,Windows应用程序。按F5键;-) 兴趣点
这就是它!历史
刚刚发布。

回答

评论会员:JosephHamilton1 时间:2012/01/26
我的意思是,我将如何扩展这个代码,包括嵌套的解析或决策树

所以代码识别问题的输入字符串,然后我怎么解析令牌休息,发现它是什么样的问题呢?

任何帮助将非常感谢
评论会员:! 时间:2012/01/26