Macro for counting frequency of Words used in a document

Get help using and writing Nisus Writer Pro macros.
Post Reply
davidrcalvert
Posts: 28
Joined: 2014-07-18 18:32:13

Macro for counting frequency of Words used in a document

Post by davidrcalvert »

Hi,
I know there is a word count Macro bout I would like to be able to arrange in a list the words used in a document in descending order by the frequency of usage. Is there one available ? Or can you get me started in creating one.

Thanks,
David
capvideo
Posts: 21
Joined: 2008-03-16 16:41:16
Contact:

Re: Macro for counting frequency of Words used in a docum

Post by capvideo »

This is the kind of thing Perl is pretty good at.

Code: Select all

# Get the current document
$document = Document.active

# Get all the text (but not headers or tables)
$text = $document.text

# Count them
$words = ''
Begin Perl
	#sort subroutine to sort by frequency in descending order
	sub byFrequency {
		$words{$b} <=> $words{$a};
	}

	#break text into individual words
	@words = $text =~ /(\w+(?:'\w+)*)/g;

	#count each occurence in hash
	foreach $word (@words) {
		#normalize text
		$word = lc($word);
		#increment frequency count for this word
		$words{$word}++;
	}

	#sort the words
	@words = keys %words;
	@sortedWords = sort byFrequency @words;

	#construct the frequency table
	foreach $word (@sortedWords) {
		$frequency = $words{$word};
		$words .= "$word:\t$frequency\n";
	}
End

#create new document with frequency table
$frequency = Document.newWithText $words

#format table
Edit:Select:Select All
Table:Convert To Table
Table:Fit To Contents

$emptyEdges = LineAttributes.newInvisible
Table:Select:Table
$table = $frequency.tableSelection
$table.applyLineAttributesToEdges $emptyEdges, "All"

#deselect table
Select Start
davidrcalvert
Posts: 28
Joined: 2014-07-18 18:32:13

Re: Macro for counting frequency of Words used in a docum

Post by davidrcalvert »

Thank You. Can you refer me to any references for a beginner to learn Perl. And Do you know how apple script can work with Nisus?

Can you run an applescript in Nisus Macros?
capvideo
Posts: 21
Joined: 2008-03-16 16:41:16
Contact:

Re: Macro for counting frequency of Words used in a docum

Post by capvideo »

(b) A quick search on AppleScript in the Nisus Macro Reference (available from the Help menu in Nisus Pro) shows the “Run AppleScript” command. I don’t recall ever using it, but it appears to be possible.

(a) The Learning Perl book from O’Reilly was quite useful when I used it; I haven’t looked at the latest version but the O’Reilly books are generally high quality. You may also find my own http://www.hoboes.com/NetLife/Swine/ useful, although it’s really meant to be run as a short class.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro for counting frequency of Words used in a docum

Post by phspaelti »

Hi David,
I don't want to discourage anyone from trying to learn Perl, but it might not be the easiest way to get to your goal.

First about Applescript. It is possible to add bits of Applescript to a Nisus macro, but getting the variables back and forth is a bit of trouble. Here is a nice example that shows how this is done:
http://www.nisus.com/forum/viewtopic.ph ... ipt#p20180

As to making word lists. Nisus Macro language can do this without needing to go to Perl (although the macro provided by Nisus does use Perl). It mostly depends on how fancy a macro you want. Here's a 'barebones' example:
Attachments
Make word list.nwm
(5.61 KiB) Downloaded 827 times
philip
Post Reply