Keywords count as in Project Targets

Dear All,

I have the requirement to compile plain text documents with Project Targets based on (1) given keywords, (2) its amount of occurrence within the document, and (3) length computed by words (word count).

I would like to know if there is a way to achieve this with Scrivener either native with its already existing capabilities or if this can be implemented by creating plug-ins or apple scripts.

There is ‘Project Targets’, which already excels on word counts. I would like to see this for keywords also

The keywords, the amount of keywords, the required word counts of the document may vary from document to document. It is therefore paramount to easily enter the required parameters.

Here is an example.

length: 500 words
keywords: Scrivener 4 - 6
Project Targets 2 - 3
Draft 2 - 3
Session 2 - 3

The results should be displayed during editing similar to ‘Project Targets’ and should update during runtime, i. e. while editing, cut, copy, paste. Can you assist?

As far as compiling goes, yes, I can think of ways of analysing text files for word counts, and for counting occurrences of specific words, using the command-line. Of significance to Scrivener, that means you can script the output using the Processing compile format pane, available to the plain-text output.

For example, here is a sequence of commands that will provide some basic stats about the file (lines, words, characters) with wc, and then produce a chart of frequently used words along with the frequency:

wc filename.txt cat filename.txt | tr -d '[:punct:]' | tr '[:blank:]' '\n' | sort | uniq -c | sort -r | head

The longer sequence of commands executes the following actions on the output:

  1. Loads the contents of the file into memory.
  2. Strips out all punctuation marks.
  3. Converts all whitespace to newlines (result: one word per line).
  4. Sorts the word list alphabetically.
  5. Strips out all non-unique lines, counting them up as it does so.
  6. Sorts again by reverse numerical (since the counted instances will be added to the beginning of the line).
  7. Snips the file to the first ten lines (top ten).

Example Result (using the content of your post here):

[code] 18 168 981 filename.txt

7 to
6 the
5 keywords
4 is
4 document
4 Targets
4 Project
4 3
4 2[/code]

This could be modified to print only from a list select words rather than all words:

cat filename.txt | tr -d '[:punct:]' | tr '[:blank:]' '\n' | sort | uniq -c | sort -r | grep -Ei 'keywords|target|project'

Now, instead of just snipping the top ten matches from all of the words in the file, we get a concise list of only those words we are interested in looking up:

5 keywords 4 Targets 4 Project

I’ve attached a project with a simple demonstration of the above, adapted to how Scrivener works with embedded shell scripts—and a few lines to pretty up the output a bit. Instructions are included in the binder.

P.S. if you’re more comfortable with another scripting language, you can put the path to it in Scrivener’s settings (for example, “/usr/bin/ruby”). Or if you prefer AppleScript, you can execute scripts from the command-line with the osascript command:

osascript -e 'say "That works!"'

Many thanks. To work with grep and wc is a reat idea. Tools already available on Mac utilising for this purpose is an excellent approach. May thanks for that. This isn’t real time, but it will do for the moment.