-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.fs
More file actions
63 lines (53 loc) · 2.17 KB
/
Copy pathCore.fs
File metadata and controls
63 lines (53 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Corpora parsing facilities
module Core
open System.IO
open System.Text
open System.Text.RegularExpressions
//A tagged word is a pair (word, part of speech tag)
type PosTaggedWord = string * string
//A corpora is a sequence of pairs (sentence, tagged sentence)
type Corpora = (string * seq<PosTaggedWord>) []
//This allows us to build neat matching rules
//involving regular expressions
//thus providing a nice API.
let (|Regex|_|) pattern input =
let m = Regex.Match(input, pattern)
if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ])
else None
//A function that returns each token in the sentence
let Tokenize sentence =
Regex.Split(sentence, "\\s+")
//Given a term delimited by the "/" separator returns a tagged pair.
let Term str : Option<PosTaggedWord> =
match str with
| Regex "(.*)/(.+)" [a; b] -> Some(a, b)
| _ -> None
//That's all folks!
//Where the magic happens
//The function to parse a sentence returning the tagged words of the sentence.
let Parse sentence =
sentence
|> Tokenize
|> Seq.choose Term
//Programatically obtained part-of-speech tags from the corpora
//used to speed up some computations.
//P.S. There is the special tag <start> added.
let tagsets = [|"#"; "$"; "''"; "("; ")"; ","; "."; ":"; "<start>"; "CC"; "CD"; "DT"; "EX"; "FW"; "IN";
"IN|RB"; "JJ"; "JJR"; "JJS"; "JJ|NN"; "JJ|NNP"; "JJ|RB"; "LS"; "MD"; "MD|VB"; "NN"; "NNP"; "NNPS";
"NNS"; "NNS|NN"; "NNS|VBZ"; "NN|CD"; "NN|JJ"; "NN|NNS"; "NN|VBG"; "PDT"; "POS"; "PRP"; "PRP$"; "RB";
"RBR"; "RBR|JJR"; "RBS"; "RBS|JJS"; "RB|DT"; "RB|IN"; "RB|JJ"; "RB|RP"; "RP"; "SYM"; "TO"; "UH"; "VB"; "VBD";
"VBD|VBP"; "VBG"; "VBG|JJ"; "VBG|NN"; "VBG|NN|JJ"; "VBN"; "VBN|JJ"; "VBN|VBD"; "VBP"; "VBP|VB"; "VBP|VBD";
"VBZ"; "VB|IN"; "VB|NN"; "WDT"; "WP"; "WP$"; "WRB"; "``" |]
//I/O Facilities
open System.IO
//Given a string returns a sequence containing its lines.
let Lines (content:string) = seq {
let ascii = new ASCIIEncoding()
use sr =
content
|> ascii.GetBytes
|> MemoryStream
|> StreamReader
while not sr.EndOfStream do
yield sr.ReadLine ()
}