Sorting text -- frequency count addition

Marielle Lange mlange at widged.com
Wed Mar 7 07:03:08 EST 2007


> Hi there,
> As an addition to this does the sorting machinery allow frequency  
> counts for words in a text field?

<code>
on mouseup
   put the text of field 1 into tText
   put replacetext(tText, "[^\w\s]", "") into tText
   put replacetext(tText, "\b\d+\b", "") into tText
   put replacetext(tText, "\s+", " ") into tText
   set the itemdel to " "
   sort items of tText by word 1 of each
   put tText
   put frequencyCount(tText, true) into message
end mouseup

function frequencyCount pText, pCaseSensitive
   if pText is empty then return empty
   --------
   if pCaseSensitive is true then
     put tolower(pText) into pText
    end if
    --------
   set the itemdel to " "
   repeat with x = 1 to the number of items in pText
     put item x of pText into tWord
     if tWord is empty then next repeat
     if aWordFreq[tWord] is empty then
       put 1 into aWordFreq[tWord]
     else
       put aWordFreq[tWord] + 1 into aWordFreq[tWord]
        end if
     end repeat
     put the keys of aWordFreq into tKeys
     put empty into tList
     repeat for each line tKey in tKeys
       put tKey && "(" & aWordFreq[tKey] & ")" & cr after tList
     end repeat
     sort lines of tList by item 1 of each
    -- replace cr with "  " in tList
     return tList
   end frequencyCount
</code>

Both the sorting and the frequency count functions were typed in 2  
minutes, you may have to tweak them a little to ensure a correct  
behavior.

Best,
Marielle

------------------------------------------------
Marielle Lange (PhD),  http://widged.com
Bite-size Applications for Education








More information about the use-livecode mailing list