-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.jl
80 lines (67 loc) · 2.16 KB
/
parse.jl
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Pkg
foreach(Pkg.add, ["CSV", "ParserCombinator", "JSON"])
using ParserCombinator, JSON, CSV
# Start date
start_date = 1665408893
# Change exchange rates if necessary
exchange_rates = Dict(
"AMD" => 1,
"€" => 400,
"\$" => 400,
"BTC" => 20000 * 400
)
# Parsing the currency
# (yes, it is unbelievably silly to resolve it to rate right there)
# (I'll need to change that later to (date -> rate))
currency_parser = exchange_rates |>
pairs |>
collect |>
x -> map(pair ->
let name = pair[1], rate = pair[2]
Equal(name) > _ -> rate
end, x) |>
x -> reduce((a, b) -> a | b, x, init=(E"" | Space()))
# Parsing number (with optional k for kilo) and a direction (+ (default) /-/±)
sumparser =
(e"±" | e"-" | (e"+" | e"" > _ -> "+")) +
((PFloat32() + ((e"k" > _ -> 1000) | (e"" > _ -> 1)) + currency_parser) > (*))
# Yah it works
map(x -> parse_one(x, sumparser), [
"123k AMD"
"-9\$"
])
# Extracting mentions
function get_mentions(text)
let
index = findfirst(x -> x isa Dict && x["type"] == "mention", text)
isnothing(index) ? "anon" : text[index]["text"]
end
end
function normalize_to_array(x); (x isa Array{Any} ? x : [x]) end
# Checks that it's a valid #costs message
function is_costs(msg)
msg["type"] == "message" &&
let text = normalize_to_array(msg["text"]), author = msg["from_id"], ts = parse(Int, msg["date_unixtime"])
# I AM THE AUTHOR
author == "user315069089" &&
# Not expired
ts > start_date &&
# and there is a hashtag there
filter(x -> x isa Dict && x["text"] == "#costs" && x["type"] == "hashtag", text) |> length > 0
end
end
x = JSON.parsefile("result.json")["messages"] |>
x -> filter(is_costs, x) |>
x -> map(msg -> normalize_to_array(msg["text"]), x) |>
x -> map(text -> (
text[2] |>
strip |>
x -> parse_one(x, sumparser),
get_mentions(text)
),
x
) |>
x -> filter(p -> p[1][1] in ["+", "±"], x) |>
x -> map(p -> p[2] => p[1][2], x) |>
x -> CSV.write("mow.csv", x, header=["Hooman", "Sum money"]) |>
x -> x