Created
February 13, 2015 08:28
-
-
Save mnd999/a032dea118a2c20de322 to your computer and use it in GitHub Desktop.
Cards Against Humanity, played by Twitter - West London Hack Night 13/2/15
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 stuff | |
import scala.io.Source | |
import scala.util.Random | |
import twitter4j.TwitterFactory | |
import twitter4j.Query | |
import twitter4j.conf.Configuration | |
import twitter4j.auth.AuthorizationConfiguration | |
import twitter4j.auth.AccessToken | |
import scala.collection.JavaConversions._ | |
object Cards extends App { | |
val twitter = new TwitterFactory().getInstance() | |
//println(twitter) | |
val answers = Source.fromInputStream(getClass().getResourceAsStream("answers.txt")).getLines.toList | |
val questions = Source.fromInputStream(getClass().getResourceAsStream("questions.txt")).getLines.toList | |
val answered = questions.map(q => answerQ(q, getAnswerRandom)) | |
println(answerQ(questions(0), getAnswerFromTwitter)) | |
def answerQ(s : String, f: List[String] => String )= { | |
if (s.contains("_")) answerUnderscore(s,f) | |
else answerPlain(s,f) | |
} | |
def answerPlain(q: String, f: List[String] => String ) = q + " " + f(answers) | |
def answerUnderscore(q: String, f: List[String] => String ) = { | |
q.split("_").mkString(f(answers)) | |
} | |
def getAnswerRandom(answers: List[String]): String = answers(Random.nextInt(answers.length - 1)) | |
def getAnswerFromTwitter(answers: List[String]): String = { | |
val ans = for { x <- (0 to 4 ) | |
val answer = getAnswerRandom(answers) | |
} yield answer | |
println(ans) | |
val res = ans.map(a => { a | |
val q = new Query() | |
q.query("\"" + a + "\"") | |
val res = twitter.search().search(q).getTweets().map(t => t.getText()) | |
(a,res) | |
} | |
) | |
val sortedResults = res.sortBy(x => x._2.size() ) | |
println(sortedResults.map(x => s"${x._1}( ${x._2.size} tweets ): "+ x._2.mkString(", ")).mkString("\n")) | |
val answer = sortedResults.last._1 | |
val tweets = sortedResults.last._2 | |
answer | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note, you need a twitter4j.properties with a twitter API key on the classpath to run this, see http://twitter4j.org/en/configuration.html