(しりとり)
力昼(バス乗り場)(りきびる)
留寿都(バス乗り場)(るすつ)
築城(ついき)
紀伊(きい)
飯井(いい)
飯岡(いいおか)
甲斐岩間(かいいわま)
舞岡(まいおか)
甲斐上野(かいうえの)
ノーフォーク広場(のーふぉーくひろば)
梅津寺(ばいしんじ)
JR尼崎(バス乗り場)(じぇいあーるあまがさき)
紀伊有田(きいありた)
大栄(バス乗り場)(たいえい)
飯岡(バス乗り場)(いいおか)
開運町2丁目(バス乗り場)(かいうんちょうにちょうめ)
女池IC前(バス乗り場)(めいけいんたーちぇんじまえ)
エースポート(バス乗り場)(えーすぽーと)
遠浅(とあさ)
西院(京福線)(さい)
飯岡保健福祉センター前(バス乗り場)(いいおかほけんふくしせんたーまえ)
Last active
December 24, 2015 01:38
-
-
Save ngsw-taro/6724716 to your computer and use it in GitHub Desktop.
駅すぱあとのWebAPIを使って駅名しりとり
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.taroid.ekisample | |
import scala.collection.mutable | |
import scala.xml.XML | |
import java.net.URLEncoder | |
object Main { | |
def main(args: Array[String]) { | |
val shiritori = new Shiritori() | |
var lastStation: Option[Station] = Some(Station(name="", yomi="しりとり")) | |
while(lastStation != None) { | |
assert(lastStation.isDefined) | |
val s = lastStation.get | |
println(s) | |
lastStation = shiritori.getNextWord(s) | |
} | |
} | |
} | |
class Shiritori { | |
private val done = mutable.HashSet[Station]() | |
def getNextWord(station: Station): Option[Station] = { | |
val lastChar = station.yomi.last | |
val url = "http://api.ekispert.com/v1/xml/station?key=MY_API_KEY&name=" + URLEncoder.encode(lastChar.toString, "utf-8") | |
val candidates = (XML.load(url) \ "Point") | |
.map { p => | |
val s = p \ "Station" | |
Station((s \ "Name").text, (s \ "Yomi").text) | |
} | |
.filter(_.yomi.head == lastChar) | |
.filter(_.yomi.size > 1) | |
.filter(_.yomi.last != 'ん') | |
.filter(s => !done.contains(s)) | |
if(candidates.size > 0) { | |
val answer = candidates.head | |
done += answer | |
Some(answer) | |
} else { | |
None | |
} | |
} | |
} | |
case class Station(name: String, yomi: String) { | |
override def toString: String = s"$name($yomi)" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment