Categorizing Words and Category Values

前端 未结 21 1652
温柔的废话
温柔的废话 2021-01-31 05:49

We were set an algorithm problem in class today, as a \"if you figure out a solution you don\'t have to do this subject\". SO of course, we all thought we will give it a go.

21条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 06:39

    Really poor answer (demonstrates no "understanding") - but as a crazy stab you could hit google (through code) for (for example) "+Fishing +Sport", "+Fishing +Cooking" etc (i.e. cross join each word and category) - and let the google fight win! i.e. the combination with the most "hits" gets chosen...

    For example (results first):

    weather: fish
    sport: ball
    weather: hat
    fashion: trousers
    weather: snowball
    weather: tornado
    

    With code (TODO: add threading ;-p):

    static void Main() {
        string[] words = { "fish", "ball", "hat", "trousers", "snowball","tornado" };
        string[] categories = { "sport", "fashion", "weather" };
    
        using(WebClient client = new WebClient()){
            foreach(string word in words) {
                var bestCategory = categories.OrderByDescending(
                    cat => Rank(client, word, cat)).First();
                Console.WriteLine("{0}: {1}", bestCategory, word);
            }
        }
    }
    
    static int Rank(WebClient client, string word, string category) {
        string s = client.DownloadString("http://www.google.com/search?q=%2B" +
            Uri.EscapeDataString(word) + "+%2B" +
            Uri.EscapeDataString(category));
        var match = Regex.Match(s, @"of about \([0-9,]+)\");
        int rank = match.Success ? int.Parse(match.Groups[1].Value, NumberStyles.Any) : 0;
        Debug.WriteLine(string.Format("\t{0} / {1} : {2}", word, category, rank));
        return rank;
    }
    

提交回复
热议问题