Page 1 of 1

Create Word list

Posted: 2014-10-15 05:44:33
by Matze
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

Re: Create german Word list

Posted: 2014-10-15 06:35:12
by Matze
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