C # distance (mile / km / etc.) String parsing library

Are there C # libraries that provide the same functionality as Google when entering a request, for example, โ€œ13 miles 743 yards in metersโ€, it will return โ€œ21,600 metersโ€ (for example).

What I want to do is give the string part functions 13 miles 743 yards, and it spits out int / double with the given distance in meters. It should be able to handle all types of input devices (kilometers / meters / ranges / miles / yards / ...), but the output should be only in meters.

It's not so difficult to write your own, but it would be great if you had a ready-made test library.

+5
source share
3 answers

I could not find the answer to this question, so I created my own :) The only real "magic" here is the expression "Regular Expression" to capture groups of values โ€‹โ€‹/ units from the original string. A simple fraction / number is scrolled from there, and then the number of meters of each unit is calculated. I have not tested this at all, so please let me know if you find improvements or bugs (the code below should throw an exception if it cannot handle the situation).

, , "[] []". , . , (, 12/32/43 1.43.3.2.44 ) . , , , 1 kilometer and 10 miles ( and). , , .

,

var a = ExtractDistance("1 1/16 Miles 3/4 yards");
var b = ExtractDistance("02234890234.853 meters");
var c = ExtractDistance("1.8 miles 3.2 furlong");
var d = ExtractDistance("1 kilometer");
var e = ExtractDistance("1/16 Miles");

:

private static Dictionary<string, double> _DistanceLookup = new Dictionary<string, double>()
{
  {"mile", 1609.344},
  {"furlong", 201.168},
  {"yard", 0.9144},
  {"inch", 0.0254},
  {"foot", 0.3048},
  {"feet", 0.3048},
  {"kilometer", 1000},
  {"kilometre", 1000},
  {"metre", 1},
  {"meter", 1},
  {"centimeter", 0.01},
  {"centimetre", 0.01},
  {"millimeter", 0.001},
  {"millimetre", 0.001},
};

private static double ConvertFraction(string fraction)
{
  double value = 0;
  if (fraction.Contains('/'))
  {
    // If the value contains /, we need to work out the fraction
    string[] splitVal = fraction.Split('/');
    if (splitVal.Length != 2)
    {
      ScrewUp(fraction, "splitVal.Length");
    }

    // Turn the fraction into decimal
    value = double.Parse(splitVal[0]) / double.Parse(splitVal[1]);
  }
  else
  {
    // Otherwise it a simple parse
    value = double.Parse(fraction);
  }
  return value;
}

public static double ExtractDistance(string distAsString)
{
  double distanceInMeters = 0;
  /* This will have a match per unit type.
   * e.g., the string "1 1/16 Miles 3/4 Yards" would have 2 matches
   * being "1 1/16 Miles", "3/4 Yards".  Each match will then have 4
   * groups in total, with group 3 being the raw value and 4 being the
   * raw unit
   */
  var matches = Regex.Matches(distAsString, @"(([\d]+[\d\s\.,/]*)\s([A-Za-z]+[^\s\d]))");
  foreach (Match match in matches)
  {
    // If groups != 4 something went wrong, we need to rethink our regex
    if (match.Groups.Count != 4)
    {
      ScrewUp(distAsString, "match.Groups.Count");
    }
    string valueRaw = match.Groups[2].Value;
    string unitRaw = match.Groups[3].Value;

    // Firstly get the value
    double value = 0;
    if (valueRaw.Contains(' '))
    {
      // If the value contains /, we need to work out the fraction
      string[] splitVal = valueRaw.Split(' ');
      if (splitVal.Length != 2)
      {
        ScrewUp(distAsString, "splitVal.Length");
      }

      // Turn the fraction into decimal
      value = ConvertFraction(splitVal[0]) + ConvertFraction(splitVal[1]);
    }
    else
    {
      value = ConvertFraction(valueRaw);
    }

    // Now work out based on the unit type
    // Clean up the raw unit string
    unitRaw = unitRaw.ToLower().Trim().TrimEnd('s');

    if (!_DistanceLookup.ContainsKey(unitRaw))
    {
      ScrewUp(distAsString, "unitRaw");
    }
    distanceInMeters += value * _DistanceLookup[unitRaw];
  }
  return distanceInMeters;
}

private static void ScrewUp(string val, string prop)
{
  throw new ArgumentException("Extract distance screwed up on string [" + val + "] (bad " + prop + ")");
}

! , - . , /.

EDIT: โ€‹โ€‹ , 1,300 meters

+3

- google, html.

: , . , ( ), , / (, ), (13 743), (, ), ( ). , . ( ).

, , , ( Google). . , .

+1

โ€‹โ€‹ . (furlongs!?), , , :

http://www.codeproject.com/KB/library/Measurement_Conversion.aspx

. , . :

  • 13 743
  • 13 743
  • 13 743

, , , , , , ...

If you want to know what people are trying to say, then you really can be better with Google. Otherwise, you can try to specify specific inputs.

0
source

All Articles