bordle/src/dictionary.cr
2022-08-21 13:43:39 +02:00

41 lines
882 B
Crystal

class Bordle
class Dictionary
DICTIONARY_FILE = "/usr/share/dict/french"
property length : UInt8
property data : Array(String)
def initialize(length : UInt8)
@length = length
@data = [] of String
load_data
end
def load_data
# use french dictionary from wfrench package
if ! File.exists? DICTIONARY_FILE
STDERR.puts "ERROR: dictionary file missing! (#{DICTIONARY_FILE})"
STDERR.puts " Please install then wfrench package on your system"
exit 1
end
lines = File.read_lines(DICTIONARY_FILE)
@data =
lines
.select {|word| word.size == @length }
.map { |word| word.tr( TR_DIACRITICS, TR_ASCII ) }
end
def includes?(word)
@data.includes? word
end
def choose_word
@data
.sample(1)
.first
end
end
end