Merging Diego Martinelli's improvements and simplifications of morse

db4
Alex Chapman 2009-04-20 11:00:38 +10:00
parent c2a35ecf33
commit 0f82f4af87
4 changed files with 134 additions and 113 deletions

View File

@ -1 +1,2 @@
Alex Chapman Alex Chapman
Diego Martinelli

View File

@ -6,12 +6,12 @@ IN: morse
HELP: ch>morse HELP: ch>morse
{ $values { $values
{ "ch" "A character that has a morse code translation" } { "str" "A string consisting of zero or more dots and dashes" } } { "ch" "A character that has a morse code translation" } { "str" "A string consisting of zero or more dots and dashes" } }
{ $description "If the given character has a morse code translation, then return that translation, otherwise return an empty string." } ; { $description "If the given character has a morse code translation, then return that translation, otherwise return a ? character." } ;
HELP: morse>ch HELP: morse>ch
{ $values { $values
{ "str" "A string of dots and dashes that represents a single character in morse code" } { "ch" "The translated character" } } { "str" "A string of dots and dashes that represents a single character in morse code" } { "ch" "The translated character" } }
{ $description "If the given string represents a morse code character, then return that character, otherwise return f" } ; { $description "If the given string represents a morse code character, then return that character, otherwise return a space character." } ;
HELP: >morse HELP: >morse
{ $values { $values

View File

@ -1,13 +1,43 @@
! Copyright (C) 2007 Alex Chapman ! Copyright (C) 2007 Alex Chapman
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays morse strings tools.test ; USING: arrays morse strings tools.test ;
IN: morse.tests
[ "" ] [ CHAR: \\ ch>morse ] unit-test [ CHAR: ? ] [ CHAR: \\ ch>morse ] unit-test
[ "..." ] [ CHAR: s ch>morse ] unit-test [ "..." ] [ CHAR: s ch>morse ] unit-test
[ CHAR: s ] [ "..." morse>ch ] unit-test [ CHAR: s ] [ "..." morse>ch ] unit-test
[ f ] [ "..--..--.." morse>ch ] unit-test [ CHAR: \s ] [ "..--..--.." morse>ch ] unit-test
[ "-- --- .-. ... . / -.-. --- -.. ." ] [ "morse code" >morse ] unit-test [ "-- --- .-. ... . / -.-. --- -.. ." ] [ "morse code" >morse ] unit-test
[ "morse code" ] [ "-- --- .-. ... . / -.-. --- -.. ." morse> ] unit-test [ "morse code" ] [ "-- --- .-. ... . / -.-. --- -.. ." morse> ] unit-test
[ "hello, world!" ] [ "Hello, World!" >morse morse> ] unit-test [ "hello, world!" ] [ "Hello, World!" >morse morse> ] unit-test
[ ".- -... -.-." ] [ "abc" >morse ] unit-test
[ "abc" ] [ ".- -... -.-." morse> ] unit-test
[ "morse code" ] [
[MORSE
-- --- .-. ... . /
-.-. --- -.. .
MORSE] >morse morse> ] unit-test
[ "morse code 123" ] [
[MORSE
__ ___ ._. ... . /
_._. ___ _.. . /
.____ ..___ ...__
MORSE] ] unit-test
[ [MORSE
-- --- .-. ... . /
-.-. --- -.. .
MORSE] ] [
"morse code" >morse morse>
] unit-test
[ "factor rocks!" ] [
[MORSE
..-. .- -.-. - --- .-. /
.-. --- -.-. -.- ... -.-.--
MORSE] ] unit-test
! [ ] [ "sos" 0.075 play-as-morse* ] unit-test ! [ ] [ "sos" 0.075 play-as-morse* ] unit-test
! [ ] [ "Factor rocks!" play-as-morse ] unit-test ! [ ] [ "Factor rocks!" play-as-morse ] unit-test

View File

@ -1,13 +1,22 @@
! Copyright (C) 2007, 2008 Alex Chapman ! Copyright (C) 2007, 2008, 2009 Alex Chapman, 2009 Diego Martinelli
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors ascii assocs combinators hashtables kernel lists math USING: accessors ascii assocs biassocs combinators hashtables kernel lists math
namespaces make openal parser-combinators promises sequences namespaces make multiline openal parser sequences splitting strings synth synth.buffers ;
strings synth synth.buffers unicode.case ;
IN: morse IN: morse
<PRIVATE <PRIVATE
: morse-codes ( -- array )
{ CONSTANT: dot-char CHAR: .
CONSTANT: dash-char CHAR: -
CONSTANT: char-gap-char CHAR: \s
CONSTANT: word-gap-char CHAR: /
CONSTANT: unknown-char CHAR: ?
PRIVATE>
DEFER: morse-code-table
H{
{ CHAR: a ".-" } { CHAR: a ".-" }
{ CHAR: b "-..." } { CHAR: b "-..." }
{ CHAR: c "-.-." } { CHAR: c "-.-." }
@ -63,68 +72,49 @@ IN: morse
{ CHAR: $ "...-..-" } { CHAR: $ "...-..-" }
{ CHAR: @ ".--.-." } { CHAR: @ ".--.-." }
{ CHAR: \s "/" } { CHAR: \s "/" }
} ; } >biassoc \ morse-code-table set-global
: ch>morse-assoc ( -- assoc ) : morse-code-table ( -- biassoc )
morse-codes >hashtable ; \ morse-code-table get-global ;
: morse>ch-assoc ( -- assoc ) : ch>morse ( ch -- morse )
morse-codes [ reverse ] map >hashtable ; ch>lower morse-code-table at [ unknown-char ] unless* ;
PRIVATE>
: ch>morse ( ch -- str )
ch>lower ch>morse-assoc at* swap "" ? ;
: morse>ch ( str -- ch ) : morse>ch ( str -- ch )
morse>ch-assoc at* swap f ? ; morse-code-table value-at [ char-gap-char ] unless* ;
: >morse ( str -- str )
[
[ CHAR: \s , ] [ ch>morse % ] interleave
] "" make ;
<PRIVATE <PRIVATE
: dot-char ( -- ch ) CHAR: . ; : word>morse ( str -- morse )
: dash-char ( -- ch ) CHAR: - ; [ ch>morse ] { } map-as " " join ;
: char-gap-char ( -- ch ) CHAR: \s ;
: word-gap-char ( -- ch ) CHAR: / ;
: =parser ( obj -- parser ) : sentence>morse ( str -- morse )
[ = ] curry satisfy ; " " split [ word>morse ] map " / " join ;
LAZY: 'dot' ( -- parser ) : trim-blanks ( str -- newstr )
dot-char =parser ; [ blank? ] trim ; inline
LAZY: 'dash' ( -- parser ) : morse>word ( morse -- str )
dash-char =parser ; " " split [ morse>ch ] "" map-as ;
LAZY: 'char-gap' ( -- parser ) : morse>sentence ( morse -- sentence )
char-gap-char =parser ; "/" split [ trim-blanks morse>word ] map " " join ;
LAZY: 'word-gap' ( -- parser ) : replace-underscores ( str -- str' )
word-gap-char =parser ; [ dup CHAR: _ = [ drop CHAR: - ] when ] map ;
LAZY: 'morse-char' ( -- parser )
'dot' 'dash' <|> <+> ;
LAZY: 'morse-word' ( -- parser )
'morse-char' 'char-gap' list-of ;
LAZY: 'morse-words' ( -- parser )
'morse-word' 'word-gap' list-of ;
PRIVATE> PRIVATE>
: morse> ( str -- str ) : >morse ( str -- newstr )
'morse-words' parse car parsed>> [ trim-blanks sentence>morse ;
[
>string morse>ch : morse> ( morse -- plain )
] map >string replace-underscores morse>sentence ;
] map [ [ CHAR: \s , ] [ % ] interleave ] "" make ;
SYNTAX: [MORSE "MORSE]" parse-multiline-string morse> parsed ;
<PRIVATE <PRIVATE
SYMBOLS: source dot-buffer dash-buffer intra-char-gap-buffer letter-gap-buffer ; SYMBOLS: source dot-buffer dash-buffer intra-char-gap-buffer letter-gap-buffer ;
: queue ( symbol -- ) : queue ( symbol -- )
@ -135,7 +125,7 @@ SYMBOLS: source dot-buffer dash-buffer intra-char-gap-buffer letter-gap-buffer ;
: intra-char-gap ( -- ) intra-char-gap-buffer queue ; : intra-char-gap ( -- ) intra-char-gap-buffer queue ;
: letter-gap ( -- ) letter-gap-buffer queue ; : letter-gap ( -- ) letter-gap-buffer queue ;
: beep-freq ( -- n ) 880 ; CONSTANT: beep-freq 880
: <morse-buffer> ( -- buffer ) : <morse-buffer> ( -- buffer )
half-sample-freq <8bit-mono-buffer> ; half-sample-freq <8bit-mono-buffer> ;