Reply to topic  [ 5 posts ] 
Macro for counting frequency of Words used in a document 
Author Message

Joined: 2014-07-18 18:32:13
Posts: 28
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


2014-09-02 17:12:30
Profile

Joined: 2008-03-16 16:41:16
Posts: 20
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

_________________
Mimsy Were the Borogoves


2014-09-02 20:24:02
Profile WWW

Joined: 2014-07-18 18:32:13
Posts: 28
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?


2014-09-03 09:33:46
Profile

Joined: 2008-03-16 16:41:16
Posts: 20
(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.

_________________
Mimsy Were the Borogoves


2014-09-03 12:47:21
Profile WWW
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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

_________________
philip
2014-09-03 20:14:36
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: Bing [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software