"A" and "an" articles in front of words

Hello guys, I have two objects that consist of arrays and I am suppose
to generate “an” and “a” articles for the words in the arrays by using a
new object.

Let’s be more specific.
I have an object object1 = [‘beatiful girl’, ‘ugly cat’, ‘small book’]
and another object called object2 = [‘ball’, ‘elephant’, ‘flower’]. New
object called “identifier” should generate the correct article (either
“a” or “an”)in front of all these words from these 2 objects so it
should do this: a beautiful girl, an ugly cat, a small book, a ball, an
elephant, a flower.

Any suggestions?

Thank you

2011/10/29 Faith T. [email protected]:

elephant, a flower.

Any suggestions?

IMHO this is not a Ruby question, but a question about how to
implement a logic in Ruby. Probably there is no Ruby library in the
world that examines a word and returns a proper “a” or “an”, so yuo
need to create it.

On Sat, Oct 29, 2011 at 10:49, Faith T. [email protected] wrote:

I am suppose
to generate “an” and “a” articles for the words in the arrays by using a
new object.

Ask yourself how you do it as a human. You look at the first letter
to see if it’s a vowel, right? (Omitting for now some special cases
like h and y under certain circumstances.) So how do you get the
first letter of a string?

The big gotcha you may face is that in Ruby 1.8 (and before), a single
subscripted element of a String got you a number, which is the ASCII
code of the character there. In 1.9, it does what most people would
expect, and gives you the single character as a string. It would be
best to make your code portable between the two, such as extracting a
one-character substring. (No I’m not going to tell you how to do
that, there are plenty of Ruby docs on the web.)

-Dave

On 10/29/2011 10:49 AM, Faith T. wrote:

Any suggestions?

Sure, develop an algorithm, that selects the correct article for the
given word. The grammar rules are pretty simple, so the code should be
pretty straight forward.

2011/10/29 Dave A. [email protected]:

Ask yourself how you do it as a human. You look at the first letter
to see if it’s a vowel, right? (Omitting for now some special cases
like h and y under certain circumstances.) So how do you get the
first letter of a string?

Not exactly: “a user” :slight_smile:

It’s about the sound, not about the letter.

On Sat, Oct 29, 2011 at 18:47, Iñaki Baz C. [email protected] wrote:

2011/10/29 Dave A. [email protected]:

Ask yourself how you do it as a human. You look at the first letter
to see if it’s a vowel, right? (Omitting for now some special cases
like h and y under certain circumstances.)

Not exactly: “a user” :slight_smile:

Good point, but I was trying to Keep It Simple. If the OP seemed more
well-versed in Ruby, or at least in programming, I’d suggest an
approach like ActiveSupport::Inflector, whereby it starts with some
simple rules like that, but also has a built-in knowledgebase of
exceptions, and lets you extend that KB with more exceptions.

So I guess the question for Faith is, how accurate does it need to be?
I get the impression this is an early homework assignment, for a
beginner’s class in programming, using Ruby. (As opposed to a class
for already seasoned programmers, to learn Ruby.) In that case, I
still think my original suggestion is rather appropriate. If the
class is more advanced, or this is to be real-world production code,
that’s different…

-Dave

Yes, I know… write an algorithm… that’s very easy for a computer
programmer but for me it’s the end of the world :). I am a web designer,
not a programmer so I guess I am hopeless already. There are two objects
with two arrays that need to comply with the algorithm, not just one,
this is the biggest problem for me… I haven’t worked with two arrays
yet. Can you suggest any coding? Something that will help me to start
and understand the whole thing behind it. Thank you… will apreciate it
a lot.

Also look at Linguistics.

http://deveiate.org/code/linguistics-1.0.8.tar.bz2
dev(E)iate

On Sun, Oct 30, 2011 at 12:18, Faith T. [email protected] wrote:

There are two objects
with two arrays that need to comply with the algorithm, not just one,
this is the biggest problem for me… I haven’t worked with two arrays
yet. Can you suggest any coding?

Mainly: don’t tie your code to either of the objects or their classes.
Instead, write a function that can accept a string from any source,
so that either object can use it, and so can anything else that might
want to. That’s what we call “loose coupling”, and it’s a Good Thing.
Then you won’t care that the string you’re trying to find the right
article for, is the result of pulling things out of two arrays. (I’m
assuming this is from the “array of adjectives and array of nouns”
title-making exercise you posted about earlier.)

As for specific code samples, no, that’s exactly what I’m trying to
avoid. It might be easier in the short term, for you, and in fact for
the rest of us, if we just gave you some code. But I want you to
understand the concept behind the code, and then come up with the
translation of concept into code, yourself.

But I suppose I could give you a skeleton to illustrate a few things.
In Ruby, code doesn’t have to be inside a class. You can just define
a function like so:

def article str
# insert logic here
end

completely outside of any class. By contrast, in some other
languages, such as Java, all code has to be in a class, such as:

class ArticleMaker

# possibly put an initializer method here

# maybe some more methods

def makeArticle
  # insert logic here
end

# maybe yet more methods

end

See how much more useless overhead there is in the second example? We
love that Ruby doesn’t make us do that. :slight_smile:

Anyway, now you just have to figure out first what logic should be in
that first example, and second how to express it in Ruby.

-Dave

hi Faith,

one way to approach this could be…

check out the #each method that works with Arrays -

this will cycle through each entry in an array, and in your case give
you back a string…

ruby Strings have an easy way of finding the first letter, the []
method… you can do something like this:

string = “hi there”
puts string[0]

=> h

Class: String (Ruby 1.9.2)

you could use a regular expression
(Class: Regexp (Ruby 1.9.2)) to determine whether to
put an “a” or an “an” before each word based on its first letter…

maybe you could write a method called indefinite_article(string)
which would take a string as an argument, check its first letter, and
then decide what article to use. you could then call this method from
within the Array#each block…

if you get a bit of code together, post it here and folks will be
happy to help out more…

  • j

On Sun, Oct 30, 2011 at 22:23, jake kaiden [email protected] wrote:

ruby Strings have an easy way of finding the first letter, the []
method… you can do something like this:

string = “hi there”
puts string[0]

=> h

In 1.9 yes… but many people are still on 1.8, which will give a
number instead. Gotta use string[0:0] instead. :frowning:

you could use a regular expression
(Class: Regexp (Ruby 1.9.2)) to determine whether to
put an “a” or an “an” before each word based on its first letter…

If she’s having a hard time extracting a character from a string, I
don’t think it’s sporting to hit her with regexps quite yet. :wink:

-Dave

On Mon, Oct 31, 2011 at 15:42, Siddharth Venkatesan
[email protected] wrote:

I like the way you approach the problem and try to teach.
People like you make the entry into ruby land pleasurable. thanks :slight_smile:

Thank you. Now if someone would make my entry into Ruby employment
pleasurable… :wink:

Thanks,
Dave

Hi Dave,

I like the way you approach the problem and try to teach.
People like you make the entry into ruby land pleasurable. thanks :slight_smile:

Sid.

Sorry to be late to the party on this one, but a regex seems a bit of a
big hammer. How about:

def article_for(noun)
article = %w(a e i o u).include?(noun[0…0]) ? ‘an’ : ‘a’
“#{article} #{noun}”
end

irb(main):022:0> article_for ‘dog’
=> “a dog”
irb(main):023:0> article_for ‘animal’
=> “an animal”

On Nov 6, 2011, at 12:59 , steve ross wrote:

=> “an animal”
A regex is not that big of a hammer, and doing this is one method
dispatch over two has direct performance benefits (if that matters):

require ‘benchmark’

# of iterations = 1000000

user system total real

null_time 0.120000 0.000000 0.120000 ( 0.118742)

article_for 8.440000 0.000000 8.440000 ( 8.444280)

articlize 6.100000 0.010000 6.110000 ( 6.107140)

articlize2 5.940000 0.000000 5.940000 ( 5.942385)

def article_for(noun)
article = %w(a e i o u).include?(noun[0…0]) ? ‘an’ : ‘a’
“#{article} #{noun}”
end

def articlize noun
article = /^[aeiou]/ =~ noun ? ‘an’ : ‘a’
“#{article} #{noun}”
end

just to see if this makes much of a difference

def articlize2 noun
“#{/^[aeiou]/ =~ noun ? ‘an’ : ‘a’} #{noun}”
end

max = (ARGV.shift || 1_000_000).to_i

puts “# of iterations = #{max}”
Benchmark::bm(20) do |x|
x.report(“null_time”) do
for i in 0…max do
# do nothing
end
end

x.report(“article_for”) do
for i in 0…max do
article_for ‘dog’
article_for ‘animal’
end
end

x.report(“articlize”) do
for i in 0…max do
articlize ‘dog’
articlize ‘animal’
end
end

x.report(“articlize2”) do
for i in 0…max do
articlize2 ‘dog’
articlize2 ‘animal’
end
end
end

On Sun, Nov 6, 2011 at 12:59 PM, steve ross [email protected] wrote:

def article_for(noun)
article = %w(a e i o u).include?(noun[0…0]) ? ‘an’ : ‘a’
“#{article} #{noun}”
end

irb(main):022:0> article_for ‘dog’
=> “a dog”
irb(main):023:0> article_for ‘animal’
=> “an animal”

article_for ‘unicorn’
=> “an unicorn” # => oops :slight_smile:

On Sun, Nov 6, 2011 at 15:59, steve ross [email protected] wrote:

Sorry to be late to the party on this one, but a regex seems a bit of a big
hammer.

My thoughts exactly.

How about:

def article_for(noun)
article = %w(a e i o u).include?(noun[0…0]) ? ‘an’ : ‘a’
“#{article} #{noun}”
end

You and I might certainly do it that way, but I was trying to avoid
confusing her with %w and the ternary operator. More newbie-friendly
would be:

def article_for noun
if [‘a’, ‘e’, ‘i’, ‘o’, ‘u’].include? noun[0…0]
‘an’
else
‘a’
end
end

-Dave

Indeed. My understanding is that the usage of a/an depends on the
pronunciation of the next word. In the case of unicorn, it sounds like
it begins with a “you”, hence “a unicorn”

on the flip side, consider the phrase “I’ll be there in an hour”,
silent ‘h’ so we use “an hour”

So this very general regex will be enough for most cases, but wont
solve the problem at hand entirely.

On Mon, Nov 7, 2011 at 9:27 AM, Hassan S.

Indeed. My understanding is that the usage of a/an depends on the
pronunciation of the next word. In the case of unicorn, it sounds like
it begins with a “you”, hence “a unicorn”

on the flip side, consider the phrase “I’ll be there in an hour”,
silent ‘h’ so we use “an hour”

true. vowels following a mute h should be included should be included
together with the (single) vowels.

btw, out of curiosity, how can one distinguish in code a short vowel
(like
the u in umbrella) from a long you-nicorn-like vowel ?

does different pronunciation comes from the subsequent letters ? i’m
thinking uMBrella, uNCle, uRGengt, uNDer, uGLy, uPPer, uRGe but uNIcorn,
eULogy (or is this “an eulogy”? now i’m confused)… i’m wondering if
two
consonants make it “an” and at least one vowel make in “a”. Maybe I’m
just
ramblingm, this sounds so un-rubyesque :S

cheers,
g.

On 11/08/2011 03:23 AM, Gonçalo C. Justino wrote:

btw, out of curiosity, how can one distinguish in code a short vowel (like
the u in umbrella) from a long you-nicorn-like vowel ?

Likely you’ll have to use a dictionary of words that fit one or the
other requirments. Then you would just check whether that dictionary
contains the word being evaluated and then respond accordingly.