Create Word list

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
Matze
Posts: 156
Joined: 2004-12-14 07:54:49
Location: Düsseldorf Germany
Contact:

Create Word list

Post by Matze » 2014-10-15 05:44:33

Hi there nise people,

is there a word list macro, that works with german text in a german version of Nisus Writer?
I need a macro, that counts each word of a document and displays the number of occurences.

Best regards, Matze

User avatar
Matze
Posts: 156
Joined: 2004-12-14 07:54:49
Location: Düsseldorf Germany
Contact:

Re: Create german Word list

Post by Matze » 2014-10-15 06:35:12

Stupid me. I should have read the other Subjects first ...
capvideo has created a Perl-Script, which I just had to translate here and there.

For any german Nisus User, who needs this script as well:

# 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
Bearbeiten:Auswählen:Alles Auswählen
Tabelle:In Tabelle konvertieren
Tabelle:An Inhalte anpassen

$emptyEdges = LineAttributes.newInvisible
Tabelle:Auswählen:Tabelle
$table = $frequency.tableSelection
$table.applyLineAttributesToEdges $emptyEdges, "All"

#deselect table
Select Start

A big THANK YOU capvideo!!

Matze

Post Reply