1
機能的アプローチを使用して正規表現を行うにはどうすればよいですか?現在のところ、ユーザーは入力をしたいと思いますが、大文字で入力しても応答はありますか?私はこれを実装する方法がわかりません。正規表現f#
open System
open System.Drawing
open System.Windows.Forms
let (|Hello|Bye|None|) input =
match input with
| "hello" | "hi" | "morning"
-> Hello
| "Goodbye" | "bye" | "go"
-> Bye
| _
-> None
let rand = new Random()
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!"
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?"
let none_response (str:string) =
let n = rand.Next(10)
match n with
| 0 -> "Maybe."
| 1 -> "Perhaps " + str
| 2 -> "Yes."
| 3 -> "Ah!"
| 4 -> "Whatever."
| 5 -> "Sorry, the chat closed unexpectedly. What was your last
question?"
| 6 -> "Where were we? I losed track of the conversation."
| 7 -> "Very interesting"
| 8 -> "Wow!"
| 9 -> "Mmmmmm!"
let rec response (token: string) (str: string) =
match token with
| Hello
-> hello_response()
| Bye
-> good_bye_response()
| ""
-> 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
-> str
let marketbot_resp (str: string) =
if (str.IndexOf(" ") > 0) then
response (str.Substring(0,str.IndexOf(" ")))
(str.Substring(str.IndexOf(" ")+1)) + "\n"
else
response str "" + "\n"
MSDNでドットネットの正規表現の例をチェックして、f#タブがあるかどうかを確認します。これについてはわかりませんが、ドットネット正規表現はすべての言語で(つまり、そういう意味で)使われるべきです。 – sln