F# 表达式应具有类型,但此处具有类型字符串
这是我今天第三次跑步了,这个流行词函数式语言F#让我抓狂,但当我把某些地方弄对的时候,感觉很好 早些时候,我遇到了一个递归循环的问题,有人提出了一种前进的方法,现在我得到了上面的错误,问题是我知道我的类型将是一个字符串,那么为什么编译器会抱怨呢 目标是将12周的学习付诸实践,所以我想在基本聊天机器人上工作,到目前为止,我可以进行基本级别的对话,但是,有一些事情仍然超出了我的范围,例如 为什么我不能叫我的类型?IUserError传递用户输入并检查他们是否说了不在我的主题列表中的内容,然后用无效输入进行响应 我的另一个问题是保持简单我想把所有输入转换成小写字符串这也是一个挑战 然后,在对话的某个阶段,用户到达房间的位置,出于某种原因,我可以找到一种方法,而不是做这个检查 网上很少有教程,我正在使用的书也没有很好地解释。我对我迄今所取得的成就感到高兴 如果有人能解释我在这方面的错误,因为在C#中,这会被抹去灰尘 以下是我的全部代码,请随意讨论:F# 表达式应具有类型,但此处具有类型字符串,f#,functional-programming,F#,Functional Programming,这是我今天第三次跑步了,这个流行词函数式语言F#让我抓狂,但当我把某些地方弄对的时候,感觉很好 早些时候,我遇到了一个递归循环的问题,有人提出了一种前进的方法,现在我得到了上面的错误,问题是我知道我的类型将是一个字符串,那么为什么编译器会抱怨呢 目标是将12周的学习付诸实践,所以我想在基本聊天机器人上工作,到目前为止,我可以进行基本级别的对话,但是,有一些事情仍然超出了我的范围,例如 为什么我不能叫我的类型?IUserError传递用户输入并检查他们是否说了不在我的主题列表中的内容,然后用无效输
open System
open System.IO
open System.Speech.Synthesis
// required for regular expression
open System.Text.RegularExpressions
// init randomizer
let rand = new Random()
// recursive response function find the first match with a key token
// response back acordingly
// Initialise a new instance of SpeechSynthesizer
let voice (sentence: string) =
use speech = new SpeechSynthesizer(Rate= -3)
//speech.SelectVoice("Microsoft Huihui Desktop")
speech.Speak(sentence)
// define functions of set list campus area
let mpCampusArea = Set.ofList ["Cisco Labs"; "The Bridge"; "Security
Area";
"Mac Labs"; "Open Access"]
//active patterns
//method for checking room number
let chkroom () =
let roomNumbers = seq {
yield 158
yield 123
yield 333 }
printfn "Room not found could it be :"
for items in roomNumbers do
printfn "%A" items
let (|Campuses|None|) users =
if Regex.Match(users,".*(MP?|mp|Curzon|curzon|ParkSide?).*" ).Success
then Campuses
else
None
// Apply Active pattern
let(|Repair|None|) input = // any sentence with broken|break|damaged is
require repair
if Regex.Match(input , ".*(broken?|break|damaged?).*").Success
then Repair
else
None
let (|ParkSide|None|) input =
if Regex.Match(input , ".*(P158|P159|P160).*").Success
then ParkSide
else
None chkroom
let (|RoomLocation|None|) str2 =
if Regex.Match(str2, ".*(158|140|150).*").Success
then RoomLocation
else
None chkroom
// Define an active recognizer for keywords that express salutation.
let (|Bye|Answer|NoSubject|MyGirlFriend|Faulty|None|) input =
match input with
| "goodbye" | "bye" | "go" |"get lost"
-> Bye
| "who" | "how" | "when" |"where"
-> Answer
| "car" |"what" |"name" |"bcu"
-> NoSubject
| "lonely" |"love" | "friendship"
-> MyGirlFriend
| "device" |"software" |"phone"
-> Faulty
| _
-> None
let (|Computer|Other|) input =
match input with
|"goodbye"|"bye"|"go" -> Computer
|_ -> Other
// select possible likely hood response based on random number for hello
subject
// Interact with the user
// Subject faulty software and Hardware
let faulty_response (str:string) =
let x = rand.Next(5)
match x with
| 0 -> "My advice is to restart the software / hardware?"
| 1 -> "My advice is relax it will be sorted."
| 2 -> "My advice is bin your device / software."
| 3 -> "Please throw your software / hardware in the recycle bin"
| 5 -> "Kiss your device / software as this always works for me."
| _ -> ""
let good_bye_response () =
let b = rand.Next(5)
match b with
| 0 -> "Good bye Babe"
| 1 -> "Thank God "
| 2 -> "We have to be positive love BCU"
| 3 -> "Live is beautiful but you are a smelly poo little fella BYE!"
| 4 -> "Good bye and thanks for complainting"
| _ -> ""
let answer_response () =
let x = rand.Next(10)
match x with
| 0 -> "Please go and complait to Waheed Rafiq"
| 1 -> "Please go and see Emmett Cooper"
| 2 -> "So you want me to kick a fuss?"
| 3 -> "What a waste of time"
| 4 -> "Please go and see BCU tech department"
| 5 -> "OMG and so what"
| 6 -> "Jump of the roof it will most likely help us all"
| 7 -> "Let's talk about the toliet shall we"
| 8 -> "why don't you use pattern matching with regular expressions!"
| 9 -> "Speak to the Queen she will mostly likely deals with BCU
complaints"
| _ -> ""
let none_response (str:string) =
let n = rand.Next(10)
match n with
| 0 -> "What would you"+ str + "like to chat about ?"
| 1 -> "I do not understand please ask again !"
| 2 -> "How about you Speak english and I log your helpdesk call yeah?"
| 3 -> "Sorry to hear that. Are you sure you want to complaint ?"
| 4 -> "This is a complaint Chat bot where you log helpdesk calls.
Please Refer to Cortana for her services.!"
| 5 -> "Let just complaint yeah for the sake of complaining ?"
| 6 -> "OKay what is your complaint about ?"
| 7 -> "Are you a human because you certainly do not behave like one!"
| 8 -> "The moon is epic. What is broken ?"
| 9 -> "Do you always complaint? Try logging it like my PC is broken
yeah !"
| _ -> ""
type Day =
| Monday
| Tuesday
| Wednesday
| Thursday
| Friday
| Saturday
| Sunday
let isWeekend x =
match x with
|Saturday |Sunday -> true
|_-> false
// using regular expression to tokenisse line of text
let matchWords = Regex(@"\w+")
let token (text:string) =
text.ToLowerInvariant()
|> matchWords.Matches
// Crossing the stream
type IUserError =
interface
end
type Error = { ErrorMessage:string; ErrorCode:int}
interface IUserError
type Success = { Status:string }
interface IUserError
let error = {ErrorMessage = "Incorrect input please enter a subject
phase";
ErrorCode = 250} :> IUserError
match error with
| :? Error as e -> printfn "Code %i \n Message: %s" e.ErrorCode
e.ErrorMessage
| :? Success -> printfn "Success"
|_ -> failwith "Invalid option"
//printfn "%A" error
//recursive response function
let rec response (token: string) (str: string) =
match token with
| Bye
-> good_bye_response ()
| Answer
-> answer_response ()
| Faulty
-> faulty_response str
| Repair
->
sprintf "%s" "Which Campus is the device in?"
| Campuses
-> sprintf "%s" "Which room is the device in?"
| RoomLocation
-> sprintf "%s" "Your call is log. Do you wish to quit?"
|_ when token.Contains("yes") -> "Okay logging you out"
|_ when token.Contains("no") -> answer_response ()
| NoSubject
-> 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 ""
let rec chat () =
let valueInput = Console.ReadLine()
printf "Helpdesk-BCU Response --> %s \n" (response "" valueInput)
let keepRunning, message = response valueInput
printfn ">> %s" message
if keepRunning then chat()
//let rec chat () =
// if Break = false then
// let valueInput = Console.ReadLine()
// printf "Helpdesk-BCU Response --> %s \n" (response "" valueInput)
// if Break = false then
// chat()
// else
// ChatEnd()
let BCU_response (str: string) =
if (str.IndexOf(" ") > 0) then
response (str.Substring(0,str.IndexOf(" "))) (str.Substring(str.IndexOf("
")+1)) + "\n"
else
response str "" + "\n"
// call back feature for the chatbot
//[<EntryPoint>]
//let main argv = printfn "%A" argv
// Advance expression lamba [ Emmett helps required]
let ifancyHerList =
[
("Sara",1); ("Saima",2); ("Zoe",3); ("Scarlett",4);
("Jennifer",5);("Sandra Bullock",6)
]
let myGirlFriend () =
List.pick (fun funToNight ->
let n = rand.Next(10)
if (snd funToNight) = n
then Some (fst funToNight)
else None
) ifancyHerList
//match myGirlFriend with
//| Some name -> printfn "Your date for tonight is %A lucky fella" name
//| None -> printfn "You don't have a date tonight!"
//
printfn "Welcome to the BCU Complaint Chat Bot"
printf "Please enter your first name -->"
let data = Console.ReadLine()
//let rec complaints n =
// printf "%s what do you want to complain about? -->" data
//complaints()
chat()
printfn "The avaialbe areas at Millennium point are: %A" mpCampusArea
printfn "Which day is Weekend on?"
let x = Console.ReadLine()
0
开放系统
开放系统
开放系统语音合成
//正则表达式必需的
打开System.Text.RegularExpressions
//初始随机化器
设rand=newrandom()
//递归响应函数查找与密钥令牌的第一个匹配项
//回应一致
//初始化SpeechSynthesizer的新实例
让声音(句子:字符串)=
使用语音=新的语音合成器(速率=-3)
//speech.SelectVoice(“Microsoft慧慧桌面”)
说话,说话(句子)
//定义集合列表校园区的功能
让mpcampusrea=Set.ofList[“思科实验室”;“桥梁”;“安全性”
面积”;
“Mac实验室”;“开放存取”]
//活动模式
//检查房间号码的方法
让chkroom()=
让RoomNumber=seq{
产量158
产量123
产率333}
printfn“找不到房间,可能是:”
对于RoomNumber中的项目,请执行以下操作:
打印“%A”项
让(|校园|无|)用户=
如果正则表达式匹配(用户,“.*(MP?| MP | Curzon | Curzon | ParkSide?.*)成功
然后是校园
其他的
没有一个
//应用活动模式
let(| Repair | None |)input=//任何带有break | break |损坏的句子
需要修理
如果正则表达式匹配(输入“.*(断开?|断开|损坏?.*”)。成功
然后修理
其他的
没有一个
让(| ParkSide | None |)输入=
如果正则表达式匹配(输入“.*(P158 | P159 | P160)。*”)。成功
然后是公园边
其他的
无更衣室
出租(| RoomLocation | None |)str2=
if Regex.Match(str2,“.*(158 | 140 | 150)。*”)成功
然后是房间位置
其他的
无更衣室
//为表示称呼的关键字定义活动识别器。
让(|再见|回答|无主题|我的女朋友|有问题|无|)输入=
匹配输入
|“再见”|“再见”|“走”|“迷路”
->再见
|“谁”“如何”“何时”“何地”
->答复
|“汽车”|“什么”|“名字”|“bcu”
->无主体
|“孤独”|“爱情”|“友谊”
->我女朋友
|“设备”|“软件”|“电话”
->错误的
| _
->没有
让(|计算机|其他|)输入=
匹配输入
|“再见”|“再见”|“走”->电脑
|_->其他
//根据hello的随机数选择可能的发动机罩响应
主题
//与用户交互
//主题软件和硬件故障
let错误的_响应(str:string)=
设x=rand.Next(5)
将x与
|0->“我的建议是重新启动软件/硬件?”
|1->“我的建议是放松,一切都会好起来的。”
|2->“我的建议是将您的设备/软件放入垃圾箱。”
|3->“请将您的软件/硬件扔进回收站”
|5->“亲吻你的设备/软件,因为这对我来说总是很有用。”
| _ -> ""
让我们再见吧回答()=
设b=rand.Next(5)
匹配
|0->“再见,宝贝”
|1->“感谢上帝”
|2->“我们必须积极爱BCU”
|3->“生活是美好的,但你是一个臭狗屎小家伙,再见!”
|4->“再见,谢谢你的抱怨”
| _ -> ""
让我们回答你的回答()=
设x=rand.Next(10)
将x与
|0->“请向Waheed Rafiq投诉”
|1->“请去看看埃米特·库珀”
|2->“那么你想让我大惊小怪?”
|3->“真是浪费时间”
|4->“请去看BCU技术部门”
|5->“天哪,那又怎样”
|6->“屋顶的跳跃很可能会帮助我们所有人”
|7->“让我们谈谈托利埃,好吗”
|8->“为什么不使用正则表达式的模式匹配!”
|9->“与女王交谈,她很可能会与BCU打交道
“投诉”
| _ -> ""
let none_响应(str:string)=
设n=rand.Next(10)
匹配
|0->“您“+str+”想聊些什么?”
|1->“我不明白,请再问一次!”
|2->“你会说英语,我会记下你的帮助热线电话,好吗?”
|3->“听到这个消息很抱歉。您确定要投诉吗?”
|4->“这是一个投诉聊天机器人,您可以在其中记录帮助热线电话。
请向Cortana咨询她的服务。!”
|5->“让我们抱怨吧,是的,为了抱怨?”
|6->“好的,你的抱怨是什么?”
|7->“你是人类,因为你的行为肯定不像人类!”
|8->“月亮是史诗般的。什么是破碎的?”
|9->“你总是抱怨吗?试着像我的电脑坏了一样记录它
是啊!”
| _ -> ""
打字日=
|星期一
|星期二
|星期三
|星期四
|星期五
|星期六
|星期天
让我们一起过周末吧
// Crossing the stream
type Error = { ErrorMessage:string; ErrorCode:int}
type Success = { Status:string }
type UserError =
| Error of Error
| Success of Success
let printResponse (error:UserError) =
match error with
| Error (e) -> printfn "Code %i \n Message: %s" e.ErrorCode e.ErrorMessage
| Success _ -> printfn "Success"
|_ -> failwith "Invalid option"
let error = Error {ErrorMessage = "Incorrect input please enter a subject phase"; ErrorCode = 250}
> printResponse error;;
Code 250
Message: Incorrect input please enter a subject phase
val it : unit = ()