William James
12/10/2006 4:54:00 PM
Jason Vogel wrote:
> William,
>
> This is exactly what I'm looking for. I don't understand it, but it's
> what I'm looking for.
>
> Would you mind explaining what your code does?
>
> Thanks,
> Jason
>
>
>
> On Dec 8, 8:23 pm, "William James" <w_a_x_...@yahoo.com> wrote:
> > Jason Vogel wrote:
> > > Disclaimer : Ruby Nuby and I don't know RegEx basically at all. I know
> > > RegEx is the answer, just don't know where to start.
> >
> > > Current Source:
> > > str.split(' ').each {|w| w.capitalize!}.join(' ')
> >
> > > Text:
> > > ADDITIONAL SPA (ONLY AVAILABLE W/PURCHASE OF POOL OR SPA)
> > > SELLER HEAT/AC/DUCTWORK
> >
> > > Result:
> > > Additional Spa (only Available W/purchase Of Pool Or Spa)
> > > Seller Heat/ac/ductwork
> >
> > > Desired:
> > > Additional Spa (Only Available w/Purchase of Pool or Spa)
> > > Seller Heat/AC/Ductwork
> >
> > > Isssus:
> > > - Need to capitalize after a "/'
> > > - Need specific word case handling (e.g. "Ac" => "AC","or" => "or",
> > > "w/[a]" => "w/[A]")
> >
> > > Thanks,
> > > Jasonspecials = %w( of or w AC ).
> > inject({}){|h,s| h.update({s.downcase,s}) }
> >
> > puts DATA.read.downcase.split( /([^a-z]+)/ ).map{|s|
> > specials[s] or s.capitalize }.join
> >
> > __END__
> > ADDITIONAL SPA (ONLY AVAILABLE W/PURCHASE OF POOL OR SPA)
> > SELLER HEAT/AC/DUCTWORK
> >
> > --- output -----
> > Additional Spa (Only Available w/Purchase of Pool or Spa)
> > Seller Heat/AC/Ductwork
It helps to inspect the data structures.
Try:
specials = %w( of or w AC ).
inject({}){|h,s| h.update({s.downcase,s}) }
p specials
text = DATA.read.downcase
p text.split( /([^a-z]+)/ )
puts text.split( /([^a-z]+)/ ).map{|s|
specials[s] or s.capitalize }.join
__END__
ADDITIONAL SPA (ONLY AVAILABLE W/PURCHASE OF POOL OR SPA)
SELLER HEAT/AC/DUCTWORK