2017-05-12 35 views
0

這是我第三次今天運行,這個時髦詞語函數式語言F#是快把我逼瘋然而,當我得到的某些位權的感覺很不錯表達預計將有一個類型,但是這裏的類型爲字符串

早些時候,我有一個遞歸循環的問題有人提出了一個前進的方向,現在我得到了上述錯誤,問題是我知道我的類型將是一個字符串,所以爲什麼編譯器抱怨?

目的是把學習實踐12周,所以我想在基本的聊天機器人工作至今我能堅持一個基本層面的對話,但是,也有幾件事情,仍然是我的範圍,例如

爲什麼我不能打電話給我的類型? IUserError傳遞用戶輸入,並檢查他們是否說過某些不在我的主題列表中,然後用無效輸入迴應。

我的其他問題是保持簡單我想將所有輸入轉換爲小寫字符串,這也被證明是一個挑戰。

然後有數字在對話的某個階段,用戶由於某種原因到達房間的位置,我可以找到一種方法,而不是做這個檢查。

網上很少有教程,我正在使用的書不能很好地解釋。我很高興與我有什麼迄今取得

,如果有人可以解釋我在哪裏這一點,因爲在C#這一切都已經走了,並撒去錯了。:(

這裏是我的整個代碼隨意討論:

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 

任何幫助/指針/任何東西,因爲這是我發瘋

我要發佈一個直接鏈接到項目文件,如果你想下載它,並有一個仔細看看,多appericate您支持。

鏈接到project file

+1

在哪一行,你得到的錯誤? –

+1

原始代碼中的縮進是可以的,但是你問題中的縮進全部搞砸了,這使得我們很難回答你的問題,因爲我們看不到代碼實際上在做什麼。將代碼粘貼到堆棧溢出以便縮進將*正確*的方法是使用輸入框中的「{}」按鈕。你粘貼你的代碼,然後你選擇整個事物並按下'{}'按鈕,並且整個選定塊被縮進4個空格(這就是Stack Overflow中的代碼塊)。 – rmunn

+0

如果你可以編輯你的問題並修正縮進,那對那些試圖幫助你的人來說是一個很大的幫助 - 我們實際上可以看到你的代碼是應該做的。 – rmunn

回答

3

我不確定使用接口是你想在這裏做什麼。您沒有爲IUserError定義任何抽象方法,但是稍後您可能會保存該方法。此外,你有一個懸掛在那裏沒有功能的塊匹配。

這裏是我的,你可以做什麼解釋:

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

使用FSI評估printResponse,它應該是這樣的:

> printResponse error;; 
Code 250 
Message: Incorrect input please enter a subject phase 
val it : unit =() 
+0

謝謝你回來,我將在今天晚些時候嘗試這個,回到你身邊。我的代碼看起來完全混亂嗎? – Wazzie

相關問題