Custom Chunk

Something you want to see in a LiveCode product? Want a new forum set up for a specific topic? Talk about it here.

Moderator: Klaus

Post Reply
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Custom Chunk

Post by dunbarx » Fri Apr 08, 2016 2:12 pm

This came up in another thread.

It is simple and fast to count all the occurrences of a word in a body of text by, say,

Code: Select all

repeat for each word tWord in tText
  add 1 to myArray[tWord]
end repeat
But what if one wanted to count pairs of adjacent words, such as "has fleas"?

The real issue here is that to use an array to advantage, there must be a "chunk" of two words. Otherwise LC has to assemble a construct on its own, which, though readily doable, takes more effort and much more runtime.

In other words, might there be a use for a custom chunk:
set the customChunk to "has fleas"
With this, you could:

Code: Select all

repeat for each customChunk in tText
  add 1 to myArray["my dog"]

or

add 1 to myArray[the customChunk]
end repeat
Craig Newman

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 194
Joined: Thu Apr 18, 2013 2:48 pm

Re: Custom Chunk

Post by livecodeali » Fri Apr 08, 2016 4:50 pm

You could *kind of* currently do this with the itemDelimiter:

Code: Select all


function countOccurrences pText, pWords
set the itemDelimiter to pWords
# Ensure item is not treated as a trailing delimiter
return the number of items in (pText & " ") - 1 
end countOccurrences
although it wouldn't work if pWords was "has fleas" and the text contained "has fleassssssss"

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Custom Chunk

Post by dunbarx » Fri Apr 08, 2016 5:10 pm

Ali.

Thanks for the reply.

The counting of instances of a string was actually not the original issue, I only wrote the request that way for clarity, and maybe fogged up the whole point instead. The OP wanted to find the word offsets of certain word pairs. The thread can be seen in the Beginners section under "Count # of Unique Two-Word Phrases (Strings/Items)"

The "Custom Chunk" would have that flexibility. It might be defined in any way, and would essentially become a new keyword, albeit a user-defined one.

Craig

EDIT.

To your point, though, and I still am in 6.7, so do not take advantage of the new expanded itemDel property (it is not yet in my radar), using the itemOffset would do the trick. And simple counting can be done directly into an array counter.

Post Reply