This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
116 lines (87 loc) · 2.63 KB
/
Main.elm
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (id, type', value, class, autocomplete, placeholder)
import Html.Events exposing (onClick)
import Html.App
import Http
import Task exposing (Task)
import Json.Decode exposing ((:=), Decoder, string, object4)
import Keyboard exposing (..)
import Char exposing (..)
import Dom
-- MODEL
type alias Model =
{ query: String
, result: ResultRecord
, class: String
}
init : ( Model, Cmd Msg )
init =
( { query = "", result = {p = "", n = "", s = "", t = ""}, class = "hidden" }, Cmd.none )
-- MESSAGES
type Msg
= Fetch
| FetchSuccess ResultRecord
| FetchError Http.Error
| Query String
-- VIEW
view : Model -> Html Msg
view model =
div [ class "h-align-center"]
[ div [ class "block" ]
[ div [ class "v-align-center search-bar" ]
[ form [ Html.Events.onSubmit Fetch, autocomplete False ]
[ input [ id "search", type' "text", Html.Events.onInput Query, placeholder "Enter a word or phrase to analyze", value model.query ] []
]
]
, div [ class ("term " ++ model.class) ] [ text model.result.t ]
, div [ class ("results " ++ model.class) ]
[ div [class "positivity"] [ text ("Positive Tweets: " ++ model.result.p ++ "%") ]
, div [class "negativity"] [ text ("Negative Tweets: " ++ model.result.n ++ "%") ]
, div [class "subjectivity"] [ text ("Subjectivity Score: " ++ model.result.s ++ " / 100") ]
]
]
]
type alias ResultRecord =
{ p : String
, n : String
, s : String
, t : String
}
decode : Decoder ResultRecord
decode =
object4 ResultRecord
("pos" := string)
("neg" := string)
("subjectivity" := string)
("term" := string)
fetchTask : Model -> Task Http.Error ResultRecord
fetchTask model =
Http.post
decode
"/api"
(Http.string model.query)
fetchCmd : Model -> Cmd Msg
fetchCmd model =
Task.perform FetchError FetchSuccess (fetchTask model)
-- UPDATE
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Query query ->
( { model | query = query }, Cmd.none )
Fetch ->
( { model | query = "" }, fetchCmd model )
FetchSuccess result ->
( { model | result = { p = result.p, n = result.n, s = result.s, t = result.t }, class = "" }, Cmd.none )
FetchError error ->
( model, Cmd.none )
-- MAIN
main : Program Never
main =
Html.App.program
{ init = init
, view = view
, update = update
, subscriptions = (always Sub.none)
}