-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataScience_happiest_state.fsx
37 lines (30 loc) · 1.18 KB
/
dataScience_happiest_state.fsx
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
#load "Setup.fsx"
open System
open System.IO
open System.Collections.Generic
open FSharp.Data
open FSharp.Data.Json.Extensions
System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
let scores = Dictionary<_,_>()
for line in File.ReadLines "data/AFINN-111.txt" do
let [|term; score|] = line.Split '\t'
scores.[term] <- Int32.Parse score
let asOption = function
| true, value -> Some value
| false, _ -> None
let states = Dictionary<_,_>()
for line in File.ReadLines "data/output.json" do
let twitter = JsonProvider<"data/sample.json", SampleList=true>.Parse line
match twitter.Text, twitter.Place with
| Some text, Some place ->
if place.CountryCode = "US" then
let state = place.FullName.Substring(place.FullName.Length-3)
let score =
scores
|> Seq.sumBy (fun (KeyValue(key,value)) ->
if text.Contains key then value else 0)
let current = defaultArg (states.TryGetValue state |> asOption) 0
states.[state] <- current + score
| _ -> ()
for KeyValue(key, value) in states |> Seq.sortBy (fun (KeyValue(_, value)) -> -value) do
printfn "%s %d" key value