-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnurble.rb
executable file
·68 lines (55 loc) · 1.24 KB
/
nurble.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
#!/bin/ruby
require "engtagger"
require "optparse"
# Nurblefy a block of text
def nurblefy(raw_text, nurble_word="nurble", uppercase_nouns=true)
tagger = EngTagger.new
# split by newline
tagged_text = raw_text.split(/\r?\n/)
# tag words
tagged_text.map! { |line| tagger.get_readable(line) }
# split by word
tagged_text.map! { |line|
if line == nil
[""]
else
line.split(" ")
end
}
noun_regex = /(.*)\/N+/
# nurblefy non-nouns
tagged_text.map! { |line| line.map! { |word|
if word.length == 0
""
elsif (match_data = noun_regex.match(word)) != nil
if uppercase_nouns
match_data[1].upcase
else
match_data[1]
end
else
nurble_word
end
}}
tagged_text.map! { |line| line.join(" ") }
return tagged_text
end
# main()
if __FILE__ == $PROGRAM_NAME
nurble = "nurble"
lowercase = false
begin
OptionParser.new do |o|
o.on("--lowercase") { |l| lowercase = l }
o.on("--nurble WORD") { |word| nurble = word }
o.parse!
end
rescue
puts "Usage: nurble.rb [options] [file]"
puts " --lowercase"
puts " --nurble WORD"
exit
end
raw_text = ARGF.read
puts nurblefy(raw_text, nurble, !lowercase)
end