String, Get Number of Words

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.

2020-03-24 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/string-get-number-of-words/

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
// Nashorn Script (engine type: 1)
// 
// 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

Capture

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.

Notes

  1. Consecutive blanks (including tabs etc) are recognized as one blank in advance.
  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

See also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: