-
Notifications
You must be signed in to change notification settings - Fork 1
/
poeta.rb
executable file
·131 lines (112 loc) · 3.67 KB
/
poeta.rb
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env ruby
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
require 'optparse'
require './poem'
require './poem_files'
require './smart_random_dictionary'
require './configuration'
require './preprocessor'
require './dictionary_statistics'
require './sentence_manager'
include Grammar
include Poeta
version = '4.0 pre-alpha'
dictionary = nil
language = 'pl'
debug = false
forced_seed = nil
info = false
show_stats = false
stat_opts = {}
conf = PoetryConfiguration.new
GRAMMAR_FOR_LANGS = {'de' => GermanGrammar, 'en' => EnglishGrammar, 'pl' => PolishGrammar}
GRAMMAR_FOR_LANGS.default = GenericGrammar
OptionParser.new do |opts|
opts.banner = "Usage: poeta.rb [options] [dictionary]"
opts.on("-l", "--language LANGUAGE", "Use language (defaults to 'pl')") do |l|
language = l
end
opts.separator ""
opts.on("-d", "--debug", "Run in debug mode") do |d|
debug = true
conf.debug = true
end
opts.on("-i", "--info", "Print additional information like used rand seed") do |d|
info = true
end
opts.on('-s', '--seed SEED', "Feed the random generator with given rand seed") do |s|
forced_seed = s.to_i
end
opts.on('--stats [SORTING]', "Just print statistics of the used dictionary",
"You can define sorting like 'freq,desc'") do |sort|
show_stats = true
if sort && sort !~ /^\w+,\w+$/
$stderr.puts "Wrong stats option: '#{sort}'; expected something like 'freq,desc'"
elsif sort
key,order = sort.split(',')
stat_opts = {:sort_key => key.to_sym, :sort_order => order.to_sym}
end
end
opts.separator ""
opts.separator "Common options:"
opts.on_tail('-h','--help','Show full help') do
puts opts
exit
end
opts.on_tail('-v','--version','Show program version') do
puts "Poeta v#{version}"
exit
end
end.parse!
raise "expects none or one argument" if ARGV.size > 1
if forced_seed
srand(forced_seed)
else
srand
end
poem_files = PoemFiles.new(language, ARGV[0])
poem_files.resolve!
grammar = GRAMMAR_FOR_LANGS[language].new
preprocessor = Preprocessor.new(conf)
File.open(poem_files.grammar_file) { |f| grammar.read_rules(f) }
dictionary = SmartRandomDictionary.new(5)
File.open(poem_files.dictionary_file) { |f| dictionary.read(preprocessor.process(f)) }
sentence_mgr = SentenceManager.new(dictionary,grammar,conf)
File.open(poem_files.sentences_file) { |f| sentence_mgr.read(preprocessor.process(f)) }
title_sentence_mgr = SentenceManager.new(dictionary,grammar,conf)
File.open(poem_files.title_sentences_file) { |f| title_sentence_mgr.read(f) }
used_config_files = []
[poem_files.general_config_file, poem_files.dictionary_config_file].each do |file|
next unless File.exist?(file)
File.open(file) { |f| conf.read(f) && used_config_files << file }
end
errors = dictionary.validate_with_grammar(grammar)
unless errors.empty?
errors.each { |err| conf.logger.warn "warn: #{err[:message]}" }
end
if show_stats
puts "dictionary: #{poem_files.dictionary_file}"
DictionaryStatistics.new.print(dictionary, stat_opts)
else
begin
poem = Poem.new(sentence_mgr,title_sentence_mgr,conf)
puts poem.text
rescue
puts 'Error: ', $!.inspect, $@
end
used_seed = srand
if info
checksum = `cat *.rb #{poem_files.grammar_file} #{poem_files.dictionary_file} #{poem_files.sentences_file} #{poem_files.title_sentences_file} | md5sum -`
checksum.gsub!(/ +- *$/, '')
puts "# rand_seed=#{used_seed} checksum=#{checksum}"
end
if debug
puts
puts "dictionary: #{poem_files.dictionary_file} sentences: #{poem_files.sentences_file} titles: #{poem_files.title_sentences_file}"
puts "grammar: #{grammar.class}"
puts "config files: #{used_config_files.join(' ')}"
puts "configuration: #{conf.summary}"
puts "rand seed: #{used_seed}"
end
end