Copy link to clipboard
Copied
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!
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
...
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
* @Param the array to check
* @Param 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;
}
Copy link to clipboard
Copied
Fantastic, that works!
Many thanks!
Copy link to clipboard
Copied
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
* @Param the array to check
* @Param 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
* @Param the file path
* @Param the text
*
*/
function writeText(p,s){
var file = new File(p);
file.encoding = 'UTF-8';
file.open('w');
file.write(s);
file.close();
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.