You can use String#split method. You have to define very well what is
a word for you. For example, consider things like "one-way street" or
"it's raining", and also be careful with punctuation. A simplistic
approach could be just to use the default split behaviour, which
splits by the spaces:
s = "this has words. how many? let's see"
[5] pry(main)> s.split
=> ["this", "has", "words.", "how", "many?", "let's", "see"]
[6] pry(main)> s.split.size
=> 7
You can pass a regular expression to the split method to tune how you split.
Jesus.
···
On Fri, Jul 6, 2012 at 5:52 AM, Joao Silva <lists@ruby-forum.com> wrote:
Hi All.
I'm trying to make a program in which you must enter a string and
calculate the number of words entered.
The problem is that you deal with whole words in a string, only handle
characters or letters.
For large text you may use String#scan, which has the advantage of not
collecting all words in an array like String#split does:
input_text = 'This is a sentence.'
word_count = input_text.strip.scan(/\s+/).size + 1
But like Jesus already said, this simple approach will not always work.
If the "words" in your text may contain whitespace, then looking for
whitespace will obviously fail. You'll have to use a dictionary in this
case. This would also cover errors (missing or superfluous whitespace).
Whereas positive matching sequences of word characters is much closer
to the reality:
irb(main):004:0> input_text.scan(/\w+/).size
=> 0
But like Jesus already said, this simple approach will not always work.
If the "words" in your text may contain whitespace, then looking for
whitespace will obviously fail. You'll have to use a dictionary in this
case. This would also cover errors (missing or superfluous whitespace).
It's crucial to clarify the definition of "word", I agree.
Kind regards
robert
···
On Fri, Jul 6, 2012 at 10:06 AM, Jan E. <lists@ruby-forum.com> wrote:
"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post #1067632:
You can use String#split method. You have to define very well what is
a word for you. For example, consider things like "one-way street" or
"it's raining", and also be careful with punctuation. A simplistic
approach could be just to use the default split behaviour, which
splits by the spaces:
s = "this has words. how many? let's see"
[5] pry(main)> s.split
=> ["this", "has", "words.", "how", "many?", "let's", "see"]
[6] pry(main)> s.split.size
=> 7
You can pass a regular expression to the split method to tune how you
split.
Jesus.
and in case you want to count the words that begin with a particular
letter (for example "a").