Created
April 8, 2013 07:51
-
-
Save v6ak/5335004 to your computer and use it in GitHub Desktop.
Trigram frequencies in Scala. Can you make it even shorted?
This file contains hidden or 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
| io.Source.fromFile("/tmp/text").mkString.split("""[\?\.\!",\- \s]+""").sliding(3).toSeq.groupBy(_.toSeq).toSeq.map{x=>x._2.size->x._1.mkString(" ")}.sorted.reverse |
This file contains hidden or 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
| val s = io.Source.fromFile("/tmp/text").mkString | |
| val words = s.split("""[\?\.\!",\- \s]+""") | |
| val ngrams = words.sliding(3).toSeq | |
| val frequencies = for((words, occurences) <- ngrams.groupBy(_.toSeq).toSeq) yield occurences.size -> words.mkString(" ") | |
| val sortedFrequencies = frequencies.sorted(Ordering.by((_: (Int, String))._1).reverse) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment