cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
Rongyu_Kuang
Level I

HOW to count the word as there is a column about sentence

hi, 

 

I have a question about how to count the word in a column about sentences. 

 

for example, if a participant writes "I like chocolate bars" , it would be counted as 4 words. if the sample size is 100, how can I count words in this column by each participant? 

 

best wishes, 

Rongyu

6 REPLIES 6
txnelson
Super User

Re: HOW to count the word as there is a column about sentence

It is a simple task.  Just count the number of elements that are created using the Words() function.

Names Default To Here( 1 );
string = "I like chocolate bars";
Show( Length( Words( string ) ) );
Jim
StarfruitBob
Level VI

Re: HOW to count the word as there is a column about sentence

Building on what txnelson mentioned, if you have a column with strings, column: Strings, then consider something like this

  1. Create new column: Num of Words
  2. Set formula to 
    Length( Words( :Strings ) )

This will fill in the Num of Words column with the number of words found in the Strings column. The data type will initially be character, but you can modify the data type and modeling to your needs.

Learning every day!
Rongyu_Kuang
Level I

Re: HOW to count the word as there is a column about sentence

many thanks indeed!! 

 

may I ask another question, if I need to count the number of sentences in a string, what should I do? 

 

thanks again. 

txnelson
Super User

Re: HOW to count the word as there is a column about sentence

All of the JMP functions are defined in the Scripting Index.  The description of the Words() function shows that it allows for a second element which defines what delimiter(s) are to be used in the specification of what is a "Word".  

txnelson_0-1679490248009.png

The default delimiter is a blank.  If you change the delimiter element to specify both a period and a question mark, I believe what you will get is a separate entry in the output list, for each sentence.

 

Jim
pmroz
Super User

Re: HOW to count the word as there is a column about sentence

To parse out sentences you could use punctuation marks as delimiters to the words function.

sentences = "The quick brown fox jumped over the lazy dog.  Hello World!  Where's Waldo?";

slist = words(sentences, ".!?");
print(slist);

nsentences = nitems(slist);
print(nsentences);

Output from the log window:

{"The quick brown fox jumped over the lazy dog", "  Hello World", "  Where's Waldo"}
3
jthi
Super User

Re: HOW to count the word as there is a column about sentence

Other option could be to replace punctuation marks with "" and then check for string lengths

Names Default To Here(1);

sentences = "The quick brown fox jumped over the lazy dog.  Hello World!  Where's Waldo?";
sentences_new = Substitute(sentences, {".", "!", "?"}, ""); // or regex with GLOBALREPLACE
show(sentences_new);
nsentences = Length(sentences) - Length(sentences_new);
show(nsentences);
-Jarmo