String, Get Number of Words
String, Get Number of Words
Gets the number of words in the text. Words separated by spaces (including tabs, double-byte spaces, and line breaks) are counted. The number of words counted, including commas and periods as delimiters, can also be obtained.
Configs
  • A: Set String *#{EL}
  • B: Select NUMERIC DATA for Number of Words (update)
  • C: Select NUMERIC for #Words with periods as delimiters (update)
Script (click to open)

// GraalJS Script (engine type: 2)
// 
// Notes:
// Consecutive blanks (including tabs etc) are recognized as one blank in advance.
// Double quotes and single quotes have been removed in advance.
// Whitespaces at both ends have been removed in advance.
// Use C if you want to consider commas as delimiters.
// comma, period, semicolon, colon, question mark, exclamation mark
//
// Notes (ja):
// 連続する空白(改行・全角空白等も含む)は、あらかじめ全体で1つの空白と認識されます。
// ダブルクオートとシングルクオートは、あらかじめ除去されたものとします。
// 先頭と末尾の空白は、あらかじめ除去されたものとします。
// カンマ等も区切り文字と見なしたい場合はCを使用してください。
// カンマ、ピリオド、セミコロン、コロン、クエスチョンマーク、エクスクラメーションマーク


//////// START "main()" ////////////////////////////////////////////////////////////////
main();
function main(){

//// == Config Retrieving / 工程コンフィグの参照 ==
const strA    = configs.get( "conf_StrA" )    + ""; // required
const dataIdB = configs.get( "conf_DataIdB" ) + ""; // not required
const dataIdC = configs.get( "conf_DataIdC" ) + ""; // not required
if( strA === "" ){
  engine.log( " AutomatedTask StringWarning:" +
              " Config {A} is empty" );
}


//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing)


//// == Calculating / 演算 ==
const strB = strA.replace(/\s+/g," ").replace(/("|')/g, "").trim();
const strC = strB.replace(/(\,|\.|:|;|\!|\?|\s)+/g, " ").trim();
// Note) Tab and double byte space are included in "\s" (UTF-8) 
// including space, tab, form feed, line feed, and other Unicode spaces.

const numB = strB.split(" ").length;
const numC = strC.split(" ").length;


//// == ワークフローデータへの代入 / Data Updating ==
if( dataIdB !== "" ){
  engine.setDataByNumber( dataIdB, new java.math.BigDecimal( numB ) );
}
if( dataIdC !== "" ){
  engine.setDataByNumber( dataIdC, new java.math.BigDecimal( numC ) );
}


} //////// END "main()" ////////////////////////////////////////////////////////////////

Download

2021-07-15 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/string-get-number-of-words-2021/
The Add-on import feature is available with Professional edition.

Notes

  1. Consecutive spaces (including line breaks, tabs, etc.) are recognized as one space.
  2. Double quotes and single quotes have been removed in advance.
  3. Whitespaces at both ends have been removed in advance.
  4. Use C if you want to consider commas as delimiters.
    1. comma, period, semicolon, colon, question mark, exclamation mark

Capture

See also

1 thought on “String, Get Number of Words”

  1. Pingback: String, Get Number of Characters – Questetra Support

Comments are closed.

%d bloggers like this: