Numbering words in a sub-lists

So I’m trying to make a list that contains {“[an arbitrary value]”, “[a word]”} into {“[and arbitrary value]”, “[a word]”, [number of times that word has occurred so far in the sequence]}:

So for example.

Take this list:

{{"269", "It"}, {"439", "was"}, {"509", "the"}, {"829", "best"}, {"1059", "of"}, {"1350", "times"}, {"1449", "it"}, {"2089", "was"}, {"2659", "the"}, {"3250", "worst"}, {"3429", "of"}, {"3529", "times"}, {"4219", "it"}, {"4519", "was"}, {"5210", "the"}, {"5629", "age"}, {"6049", "of"}, {"6279", "wisdom"}, {"6599", "it"}, {"6659", "was"}, {"6839", "the"}, {"7399", "age"}, {"6639", "of"}, {"7639", "foolishness"}, {"8019", "it"}, {"8619", "was"}, {"9369", "the"}, {"9477", "epoch"}, {"9682", "of"}, {"9897", "belief"}}

and make it into this:

{{"269", "It", 1}, {"439", "was", 1}, {"509", "the", 1}, {"829", "best", 1}, {"1059", "of", 1}, {"1350", "times", 1}, {"1449", "it", 2}, {"2089", "was", 2}, {"2659", "the", 2}, {"3250", "worst", 1}, {"3429", "of", 2}, {"3529", "times", 2}, {"4219", "it", 3}, {"4519", "was", 3}, {"5210", "the", 3}, {"5629", "age", 1}, {"6049", "of", 3}, {"6279", "wisdom", 1}, {"6599", "it", 4}, {"6659", "was", 4}, {"6839", "the", 4}, {"7399", "age", 2}, {"of" 4}, {"7639", "foolishness", 1}, {"8019", "it", 4}, {"8619", "was", 5}, {"9369", "the", 5}, {"9477", "epoch", 1}, {"9682", "of", 5}, {"9897", "belief", 1}}

So far all I have is this script that I was able to develop using input from various places on the internet:


set inList to {{"269", "It"}, {"439", "was"}, {"509", "the"}, {"829", "best"}, {"1059", "of"}, {"1350", "times"}, {"1449", "it"}, {"2089", "was"}, {"2659", "the"}, {"3250", "worst"}, {"3429", "of"}, {"3529", "times"}, {"4219", "it"}, {"4519", "was"}, {"5210", "the"}, {"5629", "age"}, {"6049", "of"}, {"6279", "wisdom"}, {"6599", "it"}, {"6659", "was"}, {"6839", "the"}, {"7399", "age"}, {"6639", "of"}, {"7639", "foolishness"}, {"8019", "it"}, {"8619", "was"}, {"9369", "the"}, {"9477", "epoch"}, {"9682", "of"}, {"9897", "belief"}}

set outList to {}
repeat with i from 1 to (length of inList)
	set val_and_wrd to item i of inList
	set the_word to item 2 of val_and_wrd
	set outList to outList & {contents of the_word, 1 + (my countOccurrences(contents of inList, inList))}
end repeat

-- the handler

on countOccurrences(searchWrd, lst)
	local counter
	set counter to 0
	repeat with wordNumPair in lst
		if item 2 of wordNumPair is searchWrd then
			set counter to counter + 1
		end if
	end repeat
	return counter
end countOccurrences

But it returns this

{"It", 1, "was", 1, "the", 1, "best", 1, "of", 1, "times", 1, "it", 1, "was", 1, "the", 1, "worst", 1, "of", 1, "times", 1, "it", 1, "was", 1, "the", 1, "age", 1, "of", 1, "wisdom", 1, "it", 1, "was", 1, "the", 1, "age", 1, "of", 1, "foolishness", 1, "it", 1, "was", 1, "the", 1, "epoch", 1, "of", 1, "belief", 1}

Not what I’m looking for.

I’m very new to applescript so any help would be greatly appreciated.

Hi. Welcome to MacScripter.

Something like this?

set inList to {{"269", "It"}, {"439", "was"}, {"509", "the"}, {"829", "best"}, {"1059", "of"}, {"1350", "times"}, {"1449", "it"}, {"2089", "was"}, {"2659", "the"}, {"3250", "worst"}, {"3429", "of"}, {"3529", "times"}, {"4219", "it"}, {"4519", "was"}, {"5210", "the"}, {"5629", "age"}, {"6049", "of"}, {"6279", "wisdom"}, {"6599", "it"}, {"6659", "was"}, {"6839", "the"}, {"7399", "age"}, {"6639", "of"}, {"7639", "foolishness"}, {"8019", "it"}, {"8619", "was"}, {"9369", "the"}, {"9477", "epoch"}, {"9682", "of"}, {"9897", "belief"}}

copy inList to outList

set len to (count outList)
repeat with i from 1 to len
	set baseGroup to item i of outList -- Get a sublist from the list.
	if ((count baseGroup) is 2) then -- If it only contains two items, its "word" hasn't been done yet.
		set counter to 1
		set end of baseGroup to counter -- Put a 1 at the end of this sublist.
		set the_word to item 2 of baseGroup -- Get the word.
		repeat with j from (i + 1) to len -- Check the remaining sublists, appending a count figure to any containing the current word.
			set thisGroup to item j of outList
			if (item 2 of thisGroup is the_word) then
				set counter to counter + 1
				set end of thisGroup to counter
			end if
		end repeat
	end if
end repeat

return outList

Edit: Comments added.

Hi. I looked at the problem prior to seeing that Nigel had replied, so I’ll leave an alternate; you should be able to determine from reading what was wrong with the original method.

property theList : {{"269", "It"}, {"439", "was"}, {"509", "the"}, {"829", "best"}, {"1059", "of"}, {"1350", "times"}, {"1449", "it"}, {"2089", "was"}, {"2659", "the"}, {"3250", "worst"}, {"3429", "of"}, {"3529", "times"}, {"4219", "it"}, {"4519", "was"}, {"5210", "the"}, {"5629", "age"}, {"6049", "of"}, {"6279", "wisdom"}, {"6599", "it"}, {"6659", "was"}, {"6839", "the"}, {"7399", "age"}, {"6639", "of"}, {"7639", "foolishness"}, {"8019", "it"}, {"8619", "was"}, {"9369", "the"}, {"9477", "epoch"}, {"9682", "of"}, {"9897", "belief"}}

repeat with index from 1 to count theList
	set theList's item index to theList's item index's items & my countOccurrences(theList's item index's item 2, index)
end repeat
theList


to countOccurrences(wordTarget, index)
	set counter to 0
	repeat with numVar from 1 to index
		if theList's item numVar's item 2 = wordTarget then set counter to counter + 1
	end repeat
end countOccurrences

–edited to remove a superfluous return value in the handler