N-grams: Difference between revisions

1,380 bytes added ,  1 year ago
RPL: add section
(Added Perl)
(RPL: add section)
Line 384:
("ET "=>1,"AND"=>1,"LIV"=>2," LI"=>1,"ND "=>1," LE"=>1,"IVE"=>2,"E A"=>1,"VE "=>1,"T L"=>1,"D L"=>1,"LET"=>1," AN"=>1).Bag</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.8}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → text n
≪ { } DUP n text SIZE '''FOR''' j
text j n - 1 + j SUB
'''IF''' DUP2 POS '''THEN'''
LAST 4 ROLL SWAP DUP2 GET 1 + PUT
SWAP DROP SWAP
'''ELSE''' + SWAP 1 + SWAP '''END'''
'''NEXT SHOWG'''
≫ ≫ ‘'''-GRAMS'''’ STO
≪ { } 1 3 PICK SIZE '''FOR''' j
OVER j GET "=" + 4 PICK j GET →STR + + '''NEXT'''
ROT ROT DROP2
≫ ‘'''SHOWG'''’ STO
|
'''-GRAMS''' ''( text n -- { "ngram=count".. } ) ''
Initialize 2 empty lists; for j = n to length(text):
ngram = text[j-n+1..j]
if ngram already in ngram list
increase counter in other list
get rid of ngram
else add to ngram list and set counter at 1 on the other list
Show results
'''SHOWG''' ( { "ngram".. } { counts.. } -- { "ngram=count".. } )
|}
{{in}}
<pre>
"LIVE AND LET LIVE" 2 -GRAMS
"LIVE AND LET LIVE" 3 -GRAMS
"LIVE AND LET LIVE" 4 -GRAMS
</pre>
{{out}}
<pre>
3: { "LI=2" "IV=2" "VE=2" "E =1" " A=1" "AN=1" "ND=1" "D =1" " L=2" "LE=1" "ET=1" "T =1" }
2: { "LIV=2" "IVE=2" "VE =1" "E A=1" " AN=1" "AND=1" "ND =1" "D L=1" " LE=1" "LET=1" "ET =1" "T L=1" " LI=1" }
1: { "LIVE=2" "IVE =1" "VE A=1" "E AN=1" " AND=1" "AND =1" "ND L=1" "D LE=1" " LET=1" "LET =1" "ET L=1" "T LI=1" " LIV=1" }
=={{header|Wren}}==
{{libheader|Wren-str}}
1,151

edits