Indexing words in a text file

Hey,

I want to list (=index) all words which are present in a text file, and count how many times each word is repeated.
Any suggestion on how to do that?
Thanks a lot,

L.

Hi.

Something like this?

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

on wordCountIndex(someText)
	set theWords to current application's class "NSArray"'s arrayWithArray:(words of someText) -- These will be AppleScript's idea of "words".
	set theWords to theWords's valueForKey:("capitalizedString")
	
	set wordCounter to current application's class "NSCountedSet"'s alloc()'s initWithArray:(theWords)
	set sortedWords to wordCounter's sortedArrayUsingDescriptors:({current application's class "NSSortDescriptor"'s sortDescriptorWithKey:("self") ascending:(true)})
	
	set wordCount to current application's class "NSMutableArray"'s new()
	repeat with thisWord in sortedWords
		tell wordCount to addObject:((thisWord as text) & ": " & (wordCounter's countForObject:(thisWord)))
	end repeat
	
	return (wordCount's componentsJoinedByString:(linefeed)) as text
end wordCountIndex


set someText to "Jim, whereas John had had \"had\", had had \"had had\". \"Had had\" had had the teacher's approval."
return wordCountIndex(someText)
(* -->
"Approval: 1
Had: 11
Jim: 1
John: 1
Teacher's: 1
The: 1
Whereas: 1"
*)

Wow !!!
It works! But it is not too clear to me. I will study it.
Thanks a lot !

I just wanted to add that this script is amazing!
It even sort the words alphabetically ! And it is so fast …
Thanks again !