Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Simple implementation of the "Kölner Phonetik" in Scala by Manuel Giesa
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- object KoelnerPhonetic {
- def calculatePhonetic(s: String) = {
- val codes = (None :: s.toUpperCase.toList.map(Some(_)) ::: List(None)).sliding(3) map { list => mapCharacter(list(0), list(1).get, list(2)) }
- codes.foldLeft("")(filterDoubleSequence) filter { _ != '0' }
- }
- private def mapCharacter(before: Option[Char], current: Char, next: Option[Char]) = current match {
- case 'A' | 'E' | 'I' | 'J' | 'O' | 'U' | 'Ü' | 'Ö' | 'Ä' | 'Y' => "0"
- case 'B' => "1"
- case 'F' | 'V' | 'W' => "3"
- case 'G' | 'K' | 'Q' => "4"
- case 'L' => "5"
- case 'M' | 'N' => "6"
- case 'R' => "7"
- case 'S' | 'Z' | 'ß' => "8"
- case 'H' => ""
- // Complex stuff
- case 'P' => next match {
- case Some('H') => "3"
- case _ => "1"
- }
- case 'D' | 'T' => next match {
- case Some('C' | 'S' | 'Z') => "8"
- case _ => "2"
- }
- case 'X' => before match {
- case Some('C' | 'K' | 'Q') => "8"
- case _ => "48"
- }
- case 'C' => before match {
- case None => next match {
- case Some('A' | 'H' | 'K' | 'L' | 'O' | 'Q' | 'R' | 'U' | 'X') => "4"
- case _ => "8"
- }
- case Some('S' | 'Z') => "8"
- case _ => next match {
- case Some('A' | 'H' | 'K' | 'O' | 'Q' | 'U' | 'X') => "4"
- case _ => "" // Will not happen
- }
- }
- case _ => ""
- }
- private def filterDoubleSequence(acc: String, value: String) = {
- if(acc.length > 0 && value.length > 0 && acc.last == value.head) {
- acc
- } else {
- acc + value
- }
- }
- }
- println(KoelnerPhonetic.calculatePhonetic(readLine("Input: ")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement