Advertisement
Guest User

Untitled

a guest
Nov 21st, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. /*
  2. Simple implementation of the "Kölner Phonetik" in Scala by Manuel Giesa
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17.  
  18. object KoelnerPhonetic {
  19.  
  20. def calculatePhonetic(s: String) = {
  21. val codes = (None :: s.toUpperCase.toList.map(Some(_)) ::: List(None)).sliding(3) map { list => mapCharacter(list(0), list(1).get, list(2)) }
  22. codes.foldLeft("")(filterDoubleSequence) filter { _ != '0' }
  23. }
  24.  
  25. private def mapCharacter(before: Option[Char], current: Char, next: Option[Char]) = current match {
  26. case 'A' | 'E' | 'I' | 'J' | 'O' | 'U' | 'Ü' | 'Ö' | 'Ä' | 'Y' => "0"
  27. case 'B' => "1"
  28. case 'F' | 'V' | 'W' => "3"
  29. case 'G' | 'K' | 'Q' => "4"
  30. case 'L' => "5"
  31. case 'M' | 'N' => "6"
  32. case 'R' => "7"
  33. case 'S' | 'Z' | 'ß' => "8"
  34. case 'H' => ""
  35.  
  36. // Complex stuff
  37. case 'P' => next match {
  38. case Some('H') => "3"
  39. case _ => "1"
  40. }
  41.  
  42. case 'D' | 'T' => next match {
  43. case Some('C' | 'S' | 'Z') => "8"
  44. case _ => "2"
  45. }
  46.  
  47. case 'X' => before match {
  48. case Some('C' | 'K' | 'Q') => "8"
  49. case _ => "48"
  50. }
  51.  
  52. case 'C' => before match {
  53. case None => next match {
  54. case Some('A' | 'H' | 'K' | 'L' | 'O' | 'Q' | 'R' | 'U' | 'X') => "4"
  55. case _ => "8"
  56. }
  57.  
  58. case Some('S' | 'Z') => "8"
  59.  
  60. case _ => next match {
  61. case Some('A' | 'H' | 'K' | 'O' | 'Q' | 'U' | 'X') => "4"
  62. case _ => "" // Will not happen
  63. }
  64. }
  65.  
  66. case _ => ""
  67. }
  68.  
  69. private def filterDoubleSequence(acc: String, value: String) = {
  70. if(acc.length > 0 && value.length > 0 && acc.last == value.head) {
  71. acc
  72. } else {
  73. acc + value
  74. }
  75. }
  76. }
  77.  
  78.  
  79. println(KoelnerPhonetic.calculatePhonetic(readLine("Input: ")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement