views:

158

answers:

2

When you search in Google "100F to C" how does it know to convert from Fahrenheit to Celsius? Similarly, conversion from different currencies and simple calculation.

What is the data structure used, or is it simple pattern matching the strings?

A: 

it's simple pattern matching

try
100 kmh in mph = no calculation
100 kph in mph = 62.1371192 mph

Martin Beckett
"100 km/h in mph" works though: km/h is the correct abbreviation
gbn
I'm almost 100% sure that for mathematical expressions (e.g. 100 * (6 +3)) they use a parser as they are non-regular.
DrJokepu
@DrJokepu: agreed, but there is the harder problem of recognizing it is a suitable expression for sending to the calculator prior to treating it as a search string. I'm guessing there's a pile of regexps and heuristics for that determination.
msw
@gbn thats my point it just looks for a bunch of strings. Of course if km/h is correct then it should also require mi/h. You can't have too many alternatives in your lookup or you will try and eval everything as a calculation.
Martin Beckett
+1  A: 

It's not exactly simple pattern matching. Evaluating the mathematical expressions you can enter is not trivial. For example, here's an algorithm that evaluates a math expression. That's just the evaluation, there's probably a lot of code to detect if it's even valid.

For the currencies conversion and other units, that's simple pattern matching.

IVlad