[REBOL] Re: Not-too-smart Table Parser (was: how to handle tables?)
From: joel:neely:fedex at: 24-Sep-2001 3:59
Ooops...
Forgot to attach the source file before hitting "Send"!
Sorry!
-jn-
--
If you tied buttered toast to the back of a cat and dropped it from
a height, what would happen?
-- Lora Canney
joel>FIX>PUNCTUATION>dot>neely>at>fedex>dot>com
-- Attached file included as plaintext by Listar --
-- File: columnizer.r
REBOL []
columnizer: make object! [
data: []
whitespace: charset [#" " #"^-"]
alpha: charset [#"a" - #"z" #"A" - #"Z"]
digit: charset [#"0" - #"9"]
special: complement union union whitespace alpha digit
IS_WSPACE: #" "
IS_ALPHA: #"A"
IS_DIGIT: #"9"
IS_SPECIAL: #"."
CHUNK-SCORES: [
" " 0.1
" A" 1.0
" 9" 0.5
" ." 0.4
"A " 0.1
"A A" 0.8
"A 9" 0.9
"A ." 0.3
"9 " 0.4
"9 A" 0.9
"9 9" 0.8
"9 ." 0.8
". " 0.1
". A" 0.7
". 9" 0.7
". ." 0.3
]
DEFAULT_SCORE: 0.01
combine-scores: function [
s1 [number!] s2 [number!]
][
][
;; s1 + s2
square-root s1 * s2
]
encode-line: function [
line [string!]
][
classes
][
either parse/all line [any whitespace] [
none
][
classes: make string! length? line
parse/all line [
some [
whitespace (append classes IS_WSPACE)
| alpha (append classes IS_ALPHA)
| digit (append classes IS_DIGIT)
| special (append classes IS_SPECIAL)
]
]
classes
]
]
hint: function [
scores [block!]
][
avg
][
avg: 0
foreach score scores [avg: avg + score]
avg: avg / length? line-scores
foreach score scores [
prin either score < avg ["-"]["+"]
]
print ""
]
score-one-line: function [
line [string!]
][
chunk score scores
][
scores: array/initial length? line DEFAULT_SCORE
repeat i (length? line) - 2 [
chunk: copy/part line 3
poke scores i + 1 any [
select CHUNK-SCORES chunk
DEFAULT_SCORE
]
line: next line
]
scores
]
all-scores: []
score-all-lines: function [
][
encoded line-scores
][
all-scores: array/initial length? data/1 DEFAULT_SCORE
foreach line data [
if encoded: encode-line line [
print line
line-scores: score-one-line encoded
while [
(length? all-scores) < length? line-scores
][
append all-scores DEFAULT_SCORE
]
repeat i length? line-scores [
poke all-scores i
combine-scores all-scores/:i line-scores/:i
]
hint line-scores
]
]
]
show-all-scores: function [
][
top bot span
][
top: bot: all-scores/1
foreach score all-scores [
top: max top score
bot: min bot score
]
span: 10.0 / (top - bot)
foreach score all-scores [
prin pick "0123456789!" 1 + to-integer score - bot * span
]
print ""
]
run: function [
ifile [file!]
][
encoded avg
][
data: read/lines ifile
score-all-lines
hint all-scores
show-all-scores
]
]