tags:

views:

401

answers:

5

Hi guys, sorry about the title :)

Here is my basic problem, I trying to implement an SEO type query for a location.

Here are my examples

  • /Leeds
  • /Leeds_England
  • /Hampshire_England
  • /England_Leeds
  • /Europe_England

I am trying to get the location, now I am splitting on the '_', then doing a LINQ lookup through my List's for each part. Location has

City Province Region Country Continent

If I find one with a greater count, I set a variable as "cityFound" and append the results to a range of locations.

I then check again using LINQ on these results on the split to see what I've got, to try and work out if each split part is in the same location.

Now I feel I am doing it wrong, but I can't figure out the way to do it "nicely". I think I could create a recursive method, or I'm looking at it totally wrong.

How would you guys tackle this problem? Pseudo code is fine, it's the logic I'm getting stumped on!

Cheers, Sarkie.

A: 

Logic aside, I do have one recommendation. When building location based URLs, I have always found it easier to work with the largest geographical location and work to the smallest. It is a logical assumption that the each additional "refinement" is more targeted than the parent. Define a strict pattern and stick to it, because it will be easier to maintain. I notice that your examples don't always stick to your pattern, either, specifically, the last one having the continent before the city.

joseph.ferris
A: 

Yeah that is the problem, I know what I want from them. But according to our "SEO" its better to allow multiple types. (Don't get me started).

You'd say it's better to do biggest first... and if I find a result, plug that back into the method. Might be worth a shot.

I'm just want to make sure I've tried every param split, since I don't know the order.

Sarkie
+1  A: 

The concern that I have with unordered splitting is the possibility of running into duplicates. Please forgive my Yankee ignorance regarding European geography, and give it to you in the US geography that I am more familiar with.

Las Vegas is a good place to start. People think of Las Vegas, that tropical desert oasis looking to suck money out of you. Of course, people don't think of Las Vegas, New Mexico - just Las Vegas, Nevada. Same thing with Phoenix, Arizona and Phoenix, New York. You get to a point where technical feasibility needs to be a part of the equation. Trust me, I have had these same arguments with our search engine person, too. I feel your pain.

joseph.ferris
A: 

Here's how I would do it. It still costs linear time in the worst case but will smooth out to constant time for the regular cases.

If you don't reuse the object, then this implementation still gives you the ability to set overrides to handle Las Vegas and Phoenix.

It will allow you to query in any order of Location properties. (Pardon me for any syntax mistakes - I haven't programmed C# in awhile.)

class LocationResolver {
    private IEnumerable<Location> locations;
    private Map<String, Location> memory = new Map<String, Location>();

    public LocationResolver(IEnumerable<Location> locs) {
        locations = locs;
        //
        // Add any locations that need disambiguation
        //
        memory["/Las Vegas"] = FromUrl("/Las Vegas_Nevada");
        memory["/Phoenix"] = FromUrl("/Phoenix_Arizona");
    }

    public Location Resolve(string url) {
        Location result;
        if (memory.TryGet(url, out result)) {
            result = FindWithParts(url.Substring(1).Split('_'));
            memory[url] = result;
        }
        return result;
    }

    private Location FindWithParts(string[] parts) {
        string[] locParts = new string[5];
        for (Location l in locations) {
            locParts[0] = l.City;
            locParts[1] = l.Province;
            locParts[2] = l.Region;
            locParts[3] = l.Country;
            locParts[4] = l.Continent;
            bool found = true;
            for (int i = 0; i < parts.Length && found; i++)
                found = Array.IndexOf(locParts, parts[i]) >= 0;
            }
            if (found) {
                return l;
            }
        }
        return null;
    }
}
Frank Krueger
A: 

Sorry forgot to close this, what I did was have a few variables.

CityText ProvinceText etc

For each split of the _, I looked up if I could find the location, if I could I set that value.

CityText = Leeds ProvinceText = West Yorkshire

I then went through the results and look up results where.

Where all Locations == Leeds

and all Locations == West Yorkshire

and x and x

Then I have the locations at the bottom satisfying all the parts of the location.

Cheers for the code though!

Sarkie.

Sarkie