0

I've got problem. I have to equal value from XML with string, which is typed in textBox. What I have to do is make program more "inteligent" which means, if I type "kraków" instead of "Kraków", program should find the location anyway.

Sample of code:

public static IEnumerable<XElement> GetRowsWithColumn(IEnumerable<XElement> rows, String name, String value)
{
    return rows
        .Where(row => row.Elements("col")
            .Any(col =>
                col.Attributes("name").Any(attr => attr.Value.Equals(name))
                && col.Value.Equals(value)));
}

If I type "Kraków" then I get good response from XML, but when I type "kraków" there's no match. What should I do?

And if I can ask one more question, how can I prompts such as google have? If you type "progr" google shows you "programming" for example.

3 Answers3

0

just make a function which compares the strings. you can use any criteria you want

    ...
    col.Attributes("name").Any(attr => AreEquivelant(attr.Value, name)) 
    ...

private static bool AreEquivelant(string s1, string s2)
{
    //compare the strings however you want
}
  • Unfortunately it's not solution, coz there are cities like "Bielsko-Biała" "Nowa Wieś" and when you type without second upper, and "-" there's no match. – Piotr Bagier Mar 31 '15 at 20:59
0

You could compare your values while you use

.ToUpper()

for your strings.

To get these prompts as google have, you could need regular expression. For more see here:

Learning Regular Expressions

Community
  • 1
  • 1
anon
  • 192
  • 6
-1

You are going to find a distance. A distance is the difference between two words. You can use Levenshtein for this one.

From Wikipedia :

In information theory and computer science, the Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one word into the other.

A basic usecase :

static void Main(string[] args)
{
    Console.WriteLine(Levenshtein.FindDistance("Alois", "aloisdg"));
    Console.WriteLine(Levenshtein.FindDistance("Alois", "aloisdg", true));
    Console.ReadLine();
}

Output

3
2

Lower the value, better is the match. For your example, you could use it and if the match is lower than something (like 2) you got a valid match.

I made one here :

Code :

public static int FindDistance(string s1, string s2, bool forceLowerCase = false)
{
    if (String.IsNullOrEmpty(s1) || s1.Length == 0)
        return String.IsNullOrEmpty(s2) ? s2.Length : 0;
    if (String.IsNullOrEmpty(s2) || s2.Length == 0)
        return String.IsNullOrEmpty(s1) ? s1.Length : 0;
    // not in Levenshtein but I need it.
    if (forceLowerCase)
    {
        s1 = s1.ToLowerInvariant();
        s2 = s2.ToLowerInvariant();
    }
    int s1Len = s1.Length;
    int s2Len = s2.Length;
    int[,] d = new int[s1Len + 1, s2Len + 1];
    for (int i = 0; i <= s1Len; i++)
        d[i, 0] = i;
    for (int j = 0; j <= s2Len; j++)
        d[0, j] = j;
    for (int i = 1; i <= s1Len; i++)
    {
        for (int j = 1; j <= s2Len; j++)
        {
            int cost = Convert.ToInt32(s1[i - 1] != s2[j - 1]);
            int min = Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1);
            d[i, j] = Math.Min(min, d[i - 1, j - 1] + cost);
        }
    }
    return d[s1Len, s2Len];
}
aloisdg
  • 22,270
  • 6
  • 85
  • 105