Abbreviations, simple: Difference between revisions

Content added Content deleted
(Added AutoHotkey)
(→‎{{header|J}}: rebuild implementation)
Line 2,273: Line 2,273:


=={{header|J}}==
=={{header|J}}==
<lang J>ctable=:|:(({.;~{:@(_,".)@;@}.);.1~ _2<nc) cut {{)n
Warning: http://rosettacode.org/wiki/Abbreviations,_easy#J uses this code without duplication. Must provide expand with the same signature. x is the command table string, y is the user sentence.
add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
<lang J>
range=:<.+[:i.>.-<.
assert 2 3 4 -: 2 range 5
assert (i.0) -: 3 range 3

abbreviate =: 3 :0
NB. input is the abbreviation table
NB. output are the valid abbreviations
y =. toupper CRLF -.~ y
y =. ;: y
y =. (([: (,3":#)L:_1 {)`[`]}~ ([: I. [: -. {.@e.&Num_j_&>)) y
y =. [&.:(;:inv) y
y =. _2 ({. , <./@:".&.>@:{:)\ y
y =. (<@] , ((range #) <@{."0 _ ]))&.>~/"1 y
ambiguities =. ;#~1<[:+/e.
(([: ~. {.@:[ , -.)L:_1 <@:ambiguities) y
)
assert ('ABC A AB' ,&(<@;:) 'ABCDE ABCD') -: abbreviate 'abc 1 abcde 3'
assert ('ABC A AB' ,&(<@;:) 'ABCDE') -: abbreviate 'abc 1 abcde'


expand =: dyad define
a =. abbreviate x
words =. <;._2 ' ' ,~ deb toupper y
interval_index =. <: +/\ #&> a
a =. a , <,<'*error*'
;:inv {.&> (interval_index I.(; a )i."_ 0 words){ a
)
</lang>

<pre>
command_table =: 0 :0
add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate
compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate
3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2
3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2
Line 2,314: Line 2,282:
refresh renum 3 repeat 3 replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left
refresh renum 3 repeat 3 replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left
2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1
2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1
}} -.LF
)


findmatch=: {{
user_words =: 'riG rePEAT copies put mo rest types fup. 6 poweRin'
lens=. >{.ctable
for_ndx. I.y-:"1&tolower (#y){.&> {:ctable do.
if. (#y) >: ndx{lens do. toupper;(<1,ndx){ctable return. end.
end.
'*error*'
}}</lang>


Task:
command_table expand user_words
<lang J> ;:inv findmatch each ;: ' riG rePEAT copies put mo rest types fup. 6 poweRin'
RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT
RIGHT REPEAT *error* *error* MOVE RESTORE *error* *error* *error* POWERINPUT</lang>
</pre>


=={{header|Java}}==
=={{header|Java}}==