nisus.com
https://www.nisus.com/forum/

Macro for counting frequency of Words used in a document
https://www.nisus.com/forum/viewtopic.php?f=17&t=5821
Page 1 of 1

Author:  davidrcalvert [ 2014-09-02 17:12:30 ]
Post subject:  Macro for counting frequency of Words used in a document

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

Author:  capvideo [ 2014-09-02 20:24:02 ]
Post subject:  Re: Macro for counting frequency of Words used in a docum

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

Code:
# 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

Author:  davidrcalvert [ 2014-09-03 09:33:46 ]
Post subject:  Re: Macro for counting frequency of Words used in a docum

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?

Author:  capvideo [ 2014-09-03 12:47:21 ]
Post subject:  Re: Macro for counting frequency of Words used in a docum

(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.

Author:  phspaelti [ 2014-09-03 20:14:36 ]
Post subject:  Re: Macro for counting frequency of Words used in a docum

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:
viewtopic.php?f=17&t=4337&hilit=applescript#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 133 times

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/