Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 41 lines (33 sloc) 1.137 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
package com.richardlog.spellcheck

import java.io.*
import java.io.File
import java.util.Set

class KotlinSpellChecker {

    private val DICTIONARY_WORDS = File("/usr/share/dict/words");

    fun checkSpelling(input : String) : Boolean {
        val words = readDictionary()
        for (word in input.toLowerCase().split(" ")) {
            if (!words.contains(word)) {
                println("$word is not in the dictionary");
                return false;
            }
        }
        return true;
    }

    private fun readDictionary() : Set<String> {
        val words = hashSet<String>("kotlin") // add kotlin to dictionary
        val stream = FileInputStream(DICTIONARY_WORDS).buffered();
        try {
            val reader = InputStreamReader(stream, "UTF-8");
            reader.forEachLine( { words.add(it)} )
        } finally {
            stream.close();
        }
        return words;
    }
}

fun main(args : Array<String>) {
    val defaultInput = "Kotlin is an island"
    val valid = KotlinSpellChecker().checkSpelling(if (args.size > 0) args[0] else defaultInput)
    println("Is the text valid? $valid")
}

Something went wrong with that request. Please try again.