Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Last active December 14, 2015 17:48
Show Gist options
  • Save AndrewRadev/5124573 to your computer and use it in GitHub Desktop.
Save AndrewRadev/5124573 to your computer and use it in GitHub Desktop.
Get the gender of a German noun.
#! /usr/bin/env ruby
# encoding: utf-8
#
# A dictionary can be found from
# http://www1.dict.cc/translation_file_request.php. Needs to be grepped for
# "noun$" before usage.
#
if ARGV.length < 1 or ARGV.all? { |arg| arg =~ /^-/ }
puts 'USAGE: gender <Noun> [options]'
exit 1
end
noun = ARGV.first.
gsub('ae', 'ä').
gsub('oe', 'ö').
gsub('ue', 'ü').
gsub('Ae', 'Ä').
gsub('Oe', 'Ö').
gsub('Ue', 'Ü').
gsub('_ss', 'ß')
verbose = ARGV.find { |arg| arg == '-v' || arg == '--verbose' }
results = {}
%x[egrep '^#{noun} ' ~/lib/german_nouns.txt].lines.each do |line|
word, meaning, _ = line.split("\t")
results[word] ||= []
results[word] << meaning
end
results.each do |word, meanings|
puts word
if verbose
meanings.each do |meaning|
puts " #{meaning}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment