Getting Alexa To Pronounce Ordinals

Today, I’m working on a project that requires Alexa to say things like “first,” “second,” or “twenty-first.”  I’ve gone through a few iterations of creating these ordinal strings.

First: Brute Force Attempt

I started the easy way: I created a hard-coded switch statement for the values from 1 – 10, and used a helper function to feed me the appropriate return value as a string..  Not the most elegant, but it got the job done.

Second: Slightly More Elegant and Scaleable

As my application grew, I realized that I would now need the values from 1 – 50 available in my application.  I added to my switch statement…until I got to 15.  At that point, I realized I needed a new solution that could scale to any number I passed in.  So I started writing some logic to append “st” to numbers that ended in 1, “nd” to numbers that ended in 2, “rd” to numbers that ended in 3, and “th” to pretty much everything else.  I had to write some exception cases for 11, 12, and 13.

It was at this point that I made an amazing discovery.

Third: Alexa is already too smart for me.

While playing with my second solution, I used the Voice Simulator that is available when you are building an Alexa skill.  I wanted to see if Alexa would pronounce the words the same if I just appended the suffixes like “th” or “nd” to the actual number value, rather than trying to convert the whole thing to a word.

This is where the discovery was made.

I tried getting her to say “4th,” and she pronounced it as I expected: “fourth.”

On a whim, I added “th” to the number 2, which would normally be incorrect.  She pronounced it “second.”  I had the same experience with “1th,” which she still got correct as “first.”

If you append “th” to the end of any number, Alexa will pronounce the appropriate ordinal.

My mind was slightly blown today.  Thanks, Alexa.

My Frustrations with “Smart Home”

That’s not a fair title.  I LOVE the smart home movement.  I love being able to open/close my garage remotely.  I love having rooms light up as I walk into them.  I love concocting recipes on IFTTT to mash-up my smart devices into even smarter experiences.  I love telling Alexa to control my home with only my voice.

“If it is 10:30pm, and the garage door is open, close it.”

What I don’t love, however, is that much of the experience and joy that is meant to be created by smart devices seems to have been created exclusively for one person that lives by themselves.  Let me give you a few examples:

Smart Bulbs

Smart lightbulbs can be controlled by my phone.  They can change colors, be turned on and off, and even dance to my music.  Amazing, right?  Where this story falls apart quickly, however, is the traditional light switch.

If I turn the bulb off from my phone, the light switch becomes non-functional.

If I turn the bulb off with the switch, I lose all of the “smart” features.

If I am a single person living by myself, I can consciously make a decision to only control the bulbs from my phone, and everything is harmonious.  Once you introduce roommates, like a spouse, or even worse, children, this entire experiment falls apart.  The consistency that you require evaporates instantly.

Smart Plugs

 

 

This is another example of power management that has so much potential.  Plug this into the wall, and now you can control a lamp, a stereo, or really anything else that requires power.  You can even set timers, so it’s an incredible way to manage those random lamps you have around your home!

That is, until someone turns that lamp off in the traditional way.

catlight

Now your smart plug is a $40 brick that can control nothing.  It’s incredibly frustrating, and most of the frustration comes from the fact that our homes are not constructed with the idea of a smart home in mind.

Geofencing

geofencing

Geofencing might be one of the coolest ideas around when it comes to smart home functionality.

“When I pull into my driveway, open the garage, turn on the lights, set the thermostat to 71F, and turn on my favorite music.”

“When I am gone for more than 18 hours, set the entire house to away.  Light bulbs on timers, thermostat as low energy as possible, all doors closed and locked.”

If I lived by myself, This. Would. Be. Awesome.  Instead, it becomes an incredible way to scare my entire family to death as I dramatically announce my home arrival.  There HAS to be a better way.

Summary

In short, I love smart home stuff.  But as a software developer, my brain wants more.  Today, in our homes, we basically get the equivalent of a solitary IF statement.

IF I pull in the driveway, THEN do this stuff.

In order for this smart home stuff to be truly impressive (and accessible) to everyone, we need to be able to add as many conditions as we possibly can.

IF I pull in the driveway AND my family is home, THEN open the garage.
ELSEIF my family isn’t home, THEN open the garage AND turn the house up to eleven.

Smart home is still really in its infancy for consumers.  If we want to make it more accessible, we need to be able to provide this level of customization in an easy to use format.  IFTTT and Stringify have made huge strides here, but we still have a long way to go.

I, for one, look forward to the very near future.  This stuff is amazing, even if it’s also frustrating sometimes.