Skip to main content
Participant
January 14, 2021
Answered

List of words used in document

  • January 14, 2021
  • 4 replies
  • 877 views

Hi there, 

 

I am wanting to generate a list of words, punctuation, and frequency. Is there a way to do this through InDesign? or if I would need a script. 

 

Many thanks!

This topic has been closed for replies.
Correct answer rob day

Not sure how well this will work on a long document, the list could be written to a text file rather than an alert dialog, but you could try this script:

 

 

alert(wordFrequency());
  
/**
* Displays a list of document words with their usage count
* @Return a string list of word frequency
* 
*/
function wordFrequency(){
    var wList = "Document Word Usage: \n";
    var aw = getAllWords()
    var cArray = []

    for (var i = 0; i < aw.length; i++){
        var cnt = 0;
        var cword = aw[i];
        if (!checkItem(cArray, cword)) {
            cArray.push(cword)
            //gets the word count
            for (var j = i; j < aw.length; j++){
                if (aw[j] == cword) {
                    cnt++
                } 
            }
            wList = wList + cword + ": " + cnt.toString() + "\n"
        } 
    } 
    return wList
}


/**
* Get every word in active document
* @Return a string list of words 
* 
*/
function getAllWords(){
    var s = app.activeDocument.stories
    var allWords = []

    for(var i=0; i < s.length; i++){  
        var w = s[i].words; 
        for(var j=0; j < w.length; j++){  
            allWords.push(w[j].contents)
        }  
    } 
    return allWords
}



/**
* Check if an item is in an array
* @9397041 the array to check 
* @9397041 the item to look for 
* @Return true if the item is in the array 
* 
*/
function checkItem(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

 

 

 

 

4 replies

Derek Cross
Community Expert
Community Expert
January 15, 2021

Have a look at TextExporter 4 https://www.rorohiko.com/wordpress/indesign-downloads/textexporter-4/ though it may be overkill for your purposes.

TextExporter 4 can export all the stories of an InDesign document into a single file.

Supported output file formats: Rich Text Format (RTF), InDesign Tagged Text, Text

 

You could use Find/ Change to count the number of a particular punctuation marks.

rob day
Community Expert
Community Expert
January 15, 2021

This version writes a tab delimited text file to the desktop:

 

var path = Folder.desktop + "/wordusage.txt";
writeText(path, wordFrequency())

/**
* Displays a list of document words with their usage count
* @Return a string list of word frequency
* 
*/
function wordFrequency(){
    var wList = "Document Word Usage: \n";
    var aw = getAllWords()
    var cArray = []

    for (var i = 0; i < aw.length; i++){
        var cnt = 0;
        var cword = aw[i];
        if (!checkItem(cArray, cword)) {
            cArray.push(cword)
            //gets the word count
            for (var j = i; j < aw.length; j++){
                if (aw[j] == cword) {
                    cnt++
                } 
            }
            wList = wList + cword + "\t" + cnt.toString() + "\n"
        } 
    } 
    return wList
}


/**
* Get every word in active document
* @Return a string list of words 
* 
*/
function getAllWords(){
    var s = app.activeDocument.stories
    var allWords = []

    for(var i=0; i < s.length; i++){  
        var w = s[i].words; 
        for(var j=0; j < w.length; j++){  
            allWords.push(w[j].contents)
        }  
    } 
    return allWords
}



/**
* Check if an item is in an array
* @9397041 the array to check 
* @9397041 the item to look for 
* @Return true if the item is in the array 
* 
*/
function checkItem(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}


/**
* Write a text file 
* @9397041 the file path 
* @9397041 the text 
* 
*/
function writeText(p,s){
    var file = new File(p);
    file.encoding = 'UTF-8';
    file.open('w');
    file.write(s);
    file.close();
}
Participating Frequently
March 17, 2024

you are the best, share knowledge in the biggest and best. Thanks. Sometimes they seem like simple things, but when people come along wanting to sell something similar, we know it's not simple. And we also know that it is not simple, because at some point, it required your time and knowledge. Thank you for your generosity in sharing knowledge.

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
January 15, 2021

Not sure how well this will work on a long document, the list could be written to a text file rather than an alert dialog, but you could try this script:

 

 

alert(wordFrequency());
  
/**
* Displays a list of document words with their usage count
* @Return a string list of word frequency
* 
*/
function wordFrequency(){
    var wList = "Document Word Usage: \n";
    var aw = getAllWords()
    var cArray = []

    for (var i = 0; i < aw.length; i++){
        var cnt = 0;
        var cword = aw[i];
        if (!checkItem(cArray, cword)) {
            cArray.push(cword)
            //gets the word count
            for (var j = i; j < aw.length; j++){
                if (aw[j] == cword) {
                    cnt++
                } 
            }
            wList = wList + cword + ": " + cnt.toString() + "\n"
        } 
    } 
    return wList
}


/**
* Get every word in active document
* @Return a string list of words 
* 
*/
function getAllWords(){
    var s = app.activeDocument.stories
    var allWords = []

    for(var i=0; i < s.length; i++){  
        var w = s[i].words; 
        for(var j=0; j < w.length; j++){  
            allWords.push(w[j].contents)
        }  
    } 
    return allWords
}



/**
* Check if an item is in an array
* @9397041 the array to check 
* @9397041 the item to look for 
* @Return true if the item is in the array 
* 
*/
function checkItem(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

 

 

 

 

Participant
January 20, 2021

Fantastic, that works! 

 

Many thanks!

Barb Binder
Community Expert
Community Expert
January 14, 2021

This isn't a feature of InDesign. Maybe one of the scripters who volunteer here can weigh in.

 

You could also export the text from InDesign and create a word cloud fairly easily. See https://universeofmemory.com/how-to-create-your-own-frequency-list/.

 

I don't see that punctuation is picked up, though. 

 

~Barb 

~Barb at Rocky Mountain Training