This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
/
visualize.rb
executable file
·92 lines (79 loc) · 2.21 KB
/
visualize.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
#!/usr/bin/env ruby
fn = ARGV.first
if fn.nil? || fn == ""
puts "usage: visualize.rb FILENAME"
exit(1)
end
def sentence(lines)
prev_guess = nil
prev_truth = nil
guess_parts = []
truth_parts = []
lines.map do |line|
cols = line.split("\t")
token = cols.first
guess = cols[-1]
truth = cols[-2]
# remove B/I prefix
guess.sub!(/[BI]\-/, "")
truth.sub!(/[BI]\-/, "")
if prev_guess.nil? || prev_guess != guess
guess_parts.push([guess])
prev_guess = guess
end
if prev_truth.nil? || prev_truth != truth
truth_parts.push([truth])
prev_truth = truth
end
guess_parts.last.push(token)
truth_parts.last.push(token)
end
g = "<div clas='guess'><strong>Guess: </strong>" + guess_parts.map do |p|
type, *rest = *p
str = rest.join(" ")
%Q[<span class="#{type.downcase}">#{str}<span>#{type[0,2]}</span></span>]
end.join("") + "</div>"
t = "<div class='truth'><strong>Truth: </strong>" + truth_parts.map do |p|
type, *rest = *p
str = rest.join(" ")
%Q[<span class="#{type.downcase}">#{str}<span>#{type[0,2]}</span></span>]
end.join("") + "</div>"
s = (guess_parts == truth_parts) ? "right" : "wrong"
"<section class='#{s}'>#{t}#{g}</section>"
end
puts <<-EOT
<html>
<head>
<style>
body { font-family: sans-serif; font-size: 20px; line-height: 16px; text-align: center; }
section { padding: 80px 0; border-top: 2px solid #eee; opacity: 0.4; }
section.wrong { opacity: 1; }
div:not(:last-child) { margin-bottom: 25px; }
div.guess { }
div.truth { }
strong { font-weight: normal; text-transform: uppercase; font-size: 10px; color: #888; }
span { color: #333; padding: 0 5px; position: relative; }
span.name { background: #fcc; }
span.qty { background: #cfc; }
span.unit { background: #ccf; }
span.comment { background: #ffc; }
span.other { background: #eff; }
span span { padding: 2px; position: absolute; top: -14px; left: 0; line-height: 10px; font-size: 8px; color: #000; background-color: inherit; }
</style>
<body>
EOT
File.open(fn) do |f|
s = []
f.each_with_index do |line, i|
if line.match(/^\s+$/)
puts sentence(s)
s = []
else
s.push(line.strip)
end
end
end
puts <<-EOT
</body>
</html>
EOT