-
Notifications
You must be signed in to change notification settings - Fork 6
Text Analysis
schlompf edited this page Oct 6, 2012
·
2 revisions
How can we detect the content of strings and words. This example usess just javascript build in functions to get a count for every word. You also should have a look into RegExp.
/*
This is text analysis the javascript way
This script splits a string by its words and checks for doubles
*/
main();
function main(){
// create doc, get page, add textframe
var doc = app.documents.add();
var page = doc.pages[0];
var docpref = doc.documentPreferences; // these are the prefs
// some values
var pw = 200;
var ph = 200;
// set the doc size
docpref.pageWidth = pw;
docpref.pageHeight = ph;
// make a textframe
var tf = page.textFrames.add({geometricBounds:[12.7,12.7,ph-12.7,pw-12.7]});
// add some placeholder text
tf.contents = TextFrameContents.PLACEHOLDER_TEXT;
// et the content of the textFrame
var content_string = tf.contents;
// now we split that string into an array
var splitted_string_arr = content_string.split(" ");
// the function compress returns an array of object like this
// [({value:"Miu", count:1}), ({value:"sidet;", count:1}), ({value:"num", count:6})]
var result = compressArray (splitted_string_arr);
alert("\n"+ result.toSource());
};
/*
Array compress function found here
http://ryanbosinger.com/blog/2011/javascript-count-duplicates-in-an-array/
by ryanbosinger
*/
function compressArray(original) {
var compressed = [];
// make a copy of the input array
var copy = original.slice(0);
// first loop goes over every element
for (var i = 0; i < original.length; i++) {
var myCount = 0;
// loop over every element in the copy and see if it's the same
for (var w = 0; w < copy.length; w++) {
if (original[i] == copy[w]) {
// increase amount of times duplicate is found
myCount++;
// sets item to undefined
delete copy[w];
}
}
// if the counter is hier than 0 do something
if (myCount > 0) {
var a = new Object(); // greate an object called a
a.value = original[i]; // add the text of the word
a.count = myCount; // add the count
compressed.push(a); // push into the return array
}
}
return compressed;
};
This wiki is maintained by:
fabiantheblind
Collaborators:
- How To Install And Run Scripts
- References And Links
- Tools
- First Session
- Comments
- Extended JavaScript Guide
- Variables And Operations
- Conditionals
- Arrays
- Output And Interaction
- Objects
- Classes
- Loops
- Functions
- Recursive Functions
- The ID Scripting DOM
- ExtendScript
- Inspect Properties
- app
- Documents
- Pages And Margins
- Masterspreads
- Layers
- GeometricBounds and Coordinates
- Text
- Line Feeds And Carrige Returns
- Simple Find And Change Grep
- Simple Find And Change Text
- Text Analysis
- Text Analysis ID FC
- Text Find Locations
- InsertionPoints
- Styles
- Objectstyles
- Rectangles Ovals Polygons
- Matrix
- Outlines Groups Alignment
- Graphic Lines
- Spiro
- Colors And Swatches
- HSL Color Wheel
- Pathfinder
- Fonts
- Transformation Matricies
- Duplicate And Transform
- Includes
- Storing Data In A Target Engine
- Create And Read Files
- Read In JSON From File And Eval
- Delay And View
- ScriptUI Resource Strings