forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov_util.ml
61 lines (57 loc) · 1.53 KB
/
markov_util.ml
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
open Sqlite_util
open Sqlite
let db =
let dbf = Sqlite.db_open "./markov.db" in
if not (result_bool dbf
"SELECT name FROM SQLITE_MASTER WHERE type='table' AND name='words'")
then
(try exec dbf
"CREATE TABLE words (word1 varchar(256), word2 varchar(256), counter int);"
with Sqlite_error s ->
raise (Failure "error while creating table"));
dbf
let add words =
let rec cycle w1 lst =
match lst with
| [] ->
let cond = ("word1=" ^ escape w1 ^ " AND word2=''") in
if result_bool db
("SELECT counter FROM words WHERE " ^ cond) then
exec db
("UPDATE words SET counter=counter+1 WHERE " ^ cond)
else
exec db ("INSERT INTO words VALUES(" ^
escape w1 ^ ",'',1)");
| w2 :: tail ->
if w1 = w2 then
cycle w2 tail
else begin
let cond = ("word1=" ^ escape w1 ^ " AND word2=" ^
escape w2) in
if result_bool db
("SELECT counter FROM words WHERE " ^ cond) then
exec db
("UPDATE words SET counter=counter+1 WHERE " ^ cond)
else
exec db ("INSERT INTO words VALUES(" ^
escape w1 ^ "," ^ escape w2 ^ ",1)");
cycle w2 tail
end
in
cycle "" words
let split_words body =
Pcre.split ~pat:"[ \t\n]+" body
let _ =
let file = Sys.argv.(1) in
let fin = open_in file in
let () =
try
while true do
let line = input_line fin in
let words = split_words line in
if words <> [] then
add words
done
with End_of_file -> ()
in
db_close db