如何打印语句然后删除F#中的项目

时间:2017-05-08 21:36:17

标签: f#

我有这个用F#创建的程序。我需要接受一个句子,然后返回句子,说明他们的每个单词/字符的词性和单词/字符。我如何读取内容,与mapStrToken匹配并打印出单词/字符和词性?

我这里有这个代码:

type tokens = Art of string | Noun of string | Adj of string 
                | Adv of string |
                | Verb of string | Comma of string | Conj of string
                |Prep of string | EOS of string | NONE;;
let mapStrToken str = 
    match str with
        | "dog" -> Noun str | "cat" -> Noun str | "mouse" -> Noun str | "tree" -> Noun str | "cheese" -> Noun str
        | "a" -> Art str | "the" -> Art str
        | "likes" -> Verb str | "hates" -> Verb str | "eats" -> Verb str | "chases" -> Verb str
        | "," -> Comma str
        | "." -> EOS str | "!" -> EOS str
        | "fast" -> Adj str | "slow" -> Adj str | "furry" -> Adj str | "yellow" -> Adj str
        | "quickly" -> Adv str | "slowly" -> Adv str | "quietly" -> Adv str
        | "and" -> Conj str | "or" -> Conj str
        | "of" -> Prep str | "around" -> Prep str
        | _ -> NONE

1 个答案:

答案 0 :(得分:0)

不确定这是不是你问的问题,但试试这个。

let tokenArray = "a fast , yellow dog".Split(' ') |> Array.map mapStrToken 
let doSomethingWithTokens = function
    | [Art; Adj; Comma; Adj; Noun] -> Some("article adjective comma adjective noune")
    | _ -> None
tokenArray |> doSomethingWithtokens