Skip to content

Instantly share code, notes, and snippets.

@fasiha
Last active April 14, 2020 23:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fasiha/2658d380a92c98e1af46 to your computer and use it in GitHub Desktop.
Save fasiha/2658d380a92c98e1af46 to your computer and use it in GitHub Desktop.
Super-simple Elm app to read JSON from a REST API, convert it to a nested Elm data structure, and print the result in the browser.
[
{
"japanese": "ボブは魚が好きだ。",
"english": "Bob likes fish.",
"tags": [],
"ve": [
{
"word": "ボブ",
"lemma": "ボブ",
"part_of_speech": "proper noun",
"reading": "ボブ",
"transcription": "ボブ",
"tokens": [
{
"literal": "ボブ",
"lemma": "ボブ",
"pos": "名詞"
}
]
},
{
"word": "は",
"lemma": "は",
"part_of_speech": "postposition",
"reading": "ハ",
"transcription": "ワ",
"tokens": [
{
"literal": "は",
"lemma": "は",
"pos": "助詞"
}
]
},
{
"word": "魚",
"lemma": "魚",
"part_of_speech": "noun",
"reading": "サカナ",
"transcription": "サカナ",
"tokens": [
{
"literal": "魚",
"lemma": "魚",
"pos": "名詞"
}
]
},
{
"word": "が",
"lemma": "が",
"part_of_speech": "postposition",
"reading": "ガ",
"transcription": "ガ",
"tokens": [
{
"literal": "が",
"lemma": "が",
"pos": "助詞"
}
]
},
{
"word": "好き",
"lemma": "好き",
"part_of_speech": "adjective",
"reading": "スキ",
"transcription": "スキ",
"tokens": [
{
"literal": "好き",
"lemma": "好き",
"pos": "名詞"
}
]
},
{
"word": "だ",
"lemma": "だ",
"part_of_speech": "verb",
"reading": "ダ",
"transcription": "ダ",
"tokens": [
{
"literal": "だ",
"lemma": "だ",
"pos": "助動詞"
}
]
},
{
"word": "。",
"lemma": "。",
"part_of_speech": "symbol",
"reading": "。",
"transcription": "。",
"tokens": [
{
"literal": "。",
"lemma": "。",
"pos": "記号"
}
]
}
]
}
]
import Json.Decode as Json exposing ((:=))
import Http
import Html exposing (Html)
import Task exposing (Task, andThen)
import Graphics.Element as Element
main =
Signal.map Element.show sentenceMailbox.signal
-- This is just a plain variable
-- sentenceMailbox : Signal.Mailbox (List String)
sentenceMailbox =
Signal.mailbox []
-- This is a function
-- report : (List String) -> Task x ()
report input =
Signal.send sentenceMailbox.address input
-- This is a port, which NEEDS its type signature—can't be inferred
port fetch : Task Http.Error ()
port fetch =
getSentence 23 `andThen` report
-- This is a function too
--getSentence : Number -> Task Http.Error (List Sentence)
getSentence number =
Http.get (Json.list sentenceDecoder) ("http://localhost:3003/" ++ toString number)
-- These are my awesome record types ya!
type alias MeCabMorpheme = { literal : String
, lemma : String
, pos : String}
type alias VeLemma = { word : String
, lemma : String
, pos : String
, reading : String
, transcription : String
, morphemes : (List MeCabMorpheme)}
type alias Sentence = { japanese : String
, english : String
, lemmas : (List VeLemma)}
-- This is just a function, see http://www.troikatech.com/blog/2015/08/17/decoding-larger-json-objects-in-elm
apply func value =
Json.object2 (<|) func value
-- These here are variables, building up a nested set of JSON decoders
meCabDecoder =
Json.map MeCabMorpheme ("literal" := Json.string)
`apply` ("lemma" := Json.string)
`apply` ("pos" := Json.string)
veDecoder =
Json.map VeLemma ("word" := Json.string)
`apply` ("lemma" := Json.string)
`apply` ("part_of_speech" := Json.string)
`apply` ("reading" := Json.string)
`apply` ("transcription" := Json.string)
`apply` ("tokens" := (Json.list meCabDecoder))
sentenceDecoder =
Json.map Sentence ("japanese" := Json.string)
`apply` ("english" := Json.string)
`apply` ("ve" := (Json.list veDecoder))
[{ english = "Bob likes fish.", japanese = "ボブは魚が好きだ。", lemmas = [{ lemma = "ボブ", morphemes = [{ lemma = "ボブ", literal = "ボブ", pos = "名詞" }], pos = "proper noun", reading = "ボブ", transcription = "ボブ", word = "ボブ" },{ lemma = "は", morphemes = [{ lemma = "は", literal = "は", pos = "助詞" }], pos = "postposition", reading = "ハ", transcription = "ワ", word = "は" },{ lemma = "魚", morphemes = [{ lemma = "魚", literal = "魚", pos = "名詞" }], pos = "noun", reading = "サカナ", transcription = "サカナ", word = "魚" },{ lemma = "が", morphemes = [{ lemma = "が", literal = "が", pos = "助詞" }], pos = "postposition", reading = "ガ", transcription = "ガ", word = "が" },{ lemma = "好き", morphemes = [{ lemma = "好き", literal = "好き", pos = "名詞" }], pos = "adjective", reading = "スキ", transcription = "スキ", word = "好き" },{ lemma = "だ", morphemes = [{ lemma = "だ", literal = "だ", pos = "助動詞" }], pos = "verb", reading = "ダ", transcription = "ダ", word = "だ" },{ lemma = "。", morphemes = [{ lemma = "。", literal = "。", pos = "記号" }], pos = "symbol", reading = "。", transcription = "。", word = "。" }] }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment