2012-01-03 14 views
6

में उच्चतम शब्दों को ढूंढें मैं स्ट्रिंग में शब्दों की शीर्ष घटनाओं को खोजने का प्रयास कर रहा हूं।एक स्ट्रिंग सी #

उदा।

Hello World This is a great world, This World is simply great 
ऊपर स्ट्रिंग से

मैं इस प्रकार की तरह परिणाम कुछ गणना करने के लिए कोशिश कर रहा हूँ:,

  • दुनिया 3
  • महान, 2
  • हैलो, 1
  • इस 2

लेकिन किसी भी शब्द को कम से कम 3 वर्णों को अनदेखा करते हुए एस उदा। is जो दो बार हुआ।

मैंने Dictionary<key, value> जोड़े में देखने की कोशिश की, मैंने linq के GroupBy एक्सटेंशन को देखने का प्रयास किया। मुझे पता है कि समाधान कहीं के बीच में स्थित है लेकिन मैं सिर्फ एल्गोरिदम के चारों ओर अपना सिर नहीं प्राप्त कर सकता हूं और यह कैसे किया जाए।

+4

यह एक होमवर्क है? – dasblinkenlight

+0

यह समान है: http://stackoverflow.com/questions/8630235/finding-the-number-of-occurences-strings-in-a- विशिष्ट-format-occur-in-a-given-t/8630247#8630247 – Matthias

+0

@dasblinkenlight - नहीं, यह होमवर्क नहीं है, मैं मेटा कीवर्ड निकालने और प्रत्येक रिकॉर्ड के लिए डेटाबेस में सहेजने की कोशिश कर रहा हूं। – Thr3e

उत्तर

14

LINQ और Regex

Regex.Split("Hello World This is a great world, This World is simply great".ToLower(), @"\W+") 
    .Where(s => s.Length > 3) 
    .GroupBy(s => s) 
    .OrderByDescending(g => g.Count()) 
+3

महान उत्तर, लेकिन मैं इस समाधान की अनुशंसा नहीं करता, उसके लिए रेगेक्स या लिनक्स की बजाय शब्दकोश के उपयोग को समझना बेहतर होगा। बस केह रहा हू। – DarthVader

+0

+1 सर्वश्रेष्ठ उत्तर खाता विराम चिह्न में लेता है ... – xandercoded

+0

@DarthVader मैं गणितीय स्तर पर सहमत हूं। प्रोग्रामिंग परिप्रेक्ष्य से LINQ जानना उतना ही मूल्यवान है। –

0
string words = "Hello World This is a great world, This World is simply great".ToLower(); 

var results = words.Split(' ').Where(x => x.Length > 3) 
           .GroupBy(x => x) 
           .Select(x => new { Count = x.Count(), Word = x.Key }) 
           .OrderByDescending(x => x.Count); 

foreach (var item in results) 
    Console.WriteLine(String.Format("{0} occured {1} times", item.Word, item.Count)); 

Console.ReadLine(); 

का उपयोग करना सबसे घटनाओं के साथ शब्द पाने के लिए:,

results.First().Word;

+0

को 'Word = x.First()' के बजाय, आप 'Word = x.Key' के माध्यम से समूह कुंजी तक पहुंच सकते हैं। –

+0

धन्यवाद @TathamOddie को यह नहीं पता था - * बीटीडब्ल्यू *, प्रपत्र प्रमाणीकरण एक्सटेंशन के लिए धन्यवाद;) – xandercoded

3
const string input = "Hello World This is a great world, This World is simply great"; 
var words = input 
    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) 
    .Where(w => w.Length >= 3) 
    .GroupBy(w => w) 
    .OrderByDescending(g => g.Count()); 

foreach (var word in words) 
    Console.WriteLine("{0}x {1}", g.Count(), word.Key); 

// 2x World 
// 2x This 
// 2x great 
// 1x Hello 
// 1x world, 
// 1x simply 

सही नहीं है क्योंकि यह अल्पविराम ट्रिम नहीं है , लेकिन यह आपको दिखाता है कि कम से कम समूहकरण और फ़िल्टरिंग कैसे करें।

3

तो मैं LINQ और Regex से बचूंगा और जैसा कि ऐसा लगता है जैसे आप एक एल्गोरिदम खोजने की कोशिश कर रहे हैं और समझते हैं कि यह आपके लिए कुछ फ़ंक्शन का उपयोग नहीं करता है।

नहीं कि वे वैध समाधान नहीं हैं। वो हैं। निश्चित रूप से। इस

Dictionary<string, int> dictionary = new Dictionary<string, int>(); 

string sInput = "Hello World, This is a great World. I love this great World"; 
sInput = sInput.Replace(",", ""); //Just cleaning up a bit 
sInput = sInput.Replace(".", ""); //Just cleaning up a bit 
string[] arr = sInput.Split(' '); //Create an array of words 

foreach (string word in arr) //let's loop over the words 
{ 
    if (word.Length >= 3) //if it meets our criteria of at least 3 letters 
    { 
     if (dictionary.ContainsKey(word)) //if it's in the dictionary 
      dictionary[word] = dictionary[word] + 1; //Increment the count 
     else 
      dictionary[word] = 1; //put it in the dictionary with a count 1 
    } 
} 

foreach (KeyValuePair<string, int> pair in dictionary) //loop through the dictionary 
    Response.Write(string.Format("Key: {0}, Pair: {1}<br />",pair.Key,pair.Value)); 
+0

आप स्ट्रिंग sInput = "हैलो वर्ल्ड, यह एक महान दुनिया है। मुझे इस महान दुनिया से प्यार है"; स्ट्रिंग sInput = "हैलो वर्ल्ड, यह एक महान दुनिया है। मुझे इस महान दुनिया से प्यार है" .लोअर(); इसे केस असंवेदनशील बनाने के लिए। व्यक्तिगत रूप से मुझे केस संवेदनशीलता पसंद है क्योंकि यह आपको उचित संज्ञाएं और बिना किसी विराम चिह्न के वाक्य की शुरुआत बता सकता है, इसलिए कुछ भी नहीं खो गया है। दुनिया और दुनिया के नीचे की तरफ बराबर नहीं हैं। – Jordan

+0

+1 अच्छा शिक्षण पोस्ट। यदि मैं कर सकता तो टिप्पणियों के लिए एक और +1 दूंगा। –

+1

आवरण पर आपकी टिप्पणी के बारे में, एक बहुत ही आसान समाधान है: केस-असंवेदनशील तुलनाकर्ता का उपयोग करके शब्दकोश बनाएं: 'var dictionary = new Dictionary (स्ट्रिंगकंपियरर.इविरिएंट कल्चर इग्नोरकेस); –

2

तरह

कोशिश कुछ मैं एक स्ट्रिंग प्रोसेसर class.You उपयोग कर सकते हैं लिखें।

उदाहरण:

metaKeywords = bodyText.Process(blackListWords: prepositions).OrderByDescending().TakeTop().GetWords().AsString(); 

कक्षा:

public static class StringProcessor 
{ 
    private static List<String> PrepositionList; 

    public static string ToNormalString(this string strText) 
    { 
     if (String.IsNullOrEmpty(strText)) return String.Empty; 
     char chNormalKaf = (char)1603; 
     char chNormalYah = (char)1610; 
     char chNonNormalKaf = (char)1705; 
     char chNonNormalYah = (char)1740; 
     string result = strText.Replace(chNonNormalKaf, chNormalKaf); 
     result = result.Replace(chNonNormalYah, chNormalYah); 
     return result; 
    } 

    public static List<KeyValuePair<String, Int32>> Process(this String bodyText, 
     List<String> blackListWords = null, 
     int minimumWordLength = 3, 
     char splitor = ' ', 
     bool perWordIsLowerCase = true) 
    { 
     string[] btArray = bodyText.ToNormalString().Split(splitor); 
     long numberOfWords = btArray.LongLength; 
     Dictionary<String, Int32> wordsDic = new Dictionary<String, Int32>(1); 
     foreach (string word in btArray) 
     { 
      if (word != null) 
      { 
       string lowerWord = word; 
       if (perWordIsLowerCase) 
        lowerWord = word.ToLower(); 
       var normalWord = lowerWord.Replace(".", "").Replace("(", "").Replace(")", "") 
        .Replace("?", "").Replace("!", "").Replace(",", "") 
        .Replace("<br>", "").Replace(":", "").Replace(";", "") 
        .Replace("،", "").Replace("-", "").Replace("\n", "").Trim(); 
       if ((normalWord.Length > minimumWordLength && !normalWord.IsMemberOfBlackListWords(blackListWords))) 
       { 
        if (wordsDic.ContainsKey(normalWord)) 
        { 
         var cnt = wordsDic[normalWord]; 
         wordsDic[normalWord] = ++cnt; 
        } 
        else 
        { 
         wordsDic.Add(normalWord, 1); 
        } 
       } 
      } 
     } 
     List<KeyValuePair<String, Int32>> keywords = wordsDic.ToList(); 
     return keywords; 
    } 

    public static List<KeyValuePair<String, Int32>> OrderByDescending(this List<KeyValuePair<String, Int32>> list, bool isBasedOnFrequency = true) 
    { 
     List<KeyValuePair<String, Int32>> result = null; 
     if (isBasedOnFrequency) 
      result = list.OrderByDescending(q => q.Value).ToList(); 
     else 
      result = list.OrderByDescending(q => q.Key).ToList(); 
     return result; 
    } 

    public static List<KeyValuePair<String, Int32>> TakeTop(this List<KeyValuePair<String, Int32>> list, Int32 n = 10) 
    { 
     List<KeyValuePair<String, Int32>> result = list.Take(n).ToList(); 
     return result; 
    } 

    public static List<String> GetWords(this List<KeyValuePair<String, Int32>> list) 
    { 
     List<String> result = new List<String>(); 
     foreach (var item in list) 
     { 
      result.Add(item.Key); 
     } 
     return result; 
    } 

    public static List<Int32> GetFrequency(this List<KeyValuePair<String, Int32>> list) 
    { 
     List<Int32> result = new List<Int32>(); 
     foreach (var item in list) 
     { 
      result.Add(item.Value); 
     } 
     return result; 
    } 

    public static String AsString<T>(this List<T> list, string seprator = ", ") 
    { 
     String result = string.Empty; 
     foreach (var item in list) 
     { 
      result += string.Format("{0}{1}", item, seprator); 
     } 
     return result; 
    } 

    private static bool IsMemberOfBlackListWords(this String word, List<String> blackListWords) 
    { 
     bool result = false; 
     if (blackListWords == null) return false; 
     foreach (var w in blackListWords) 
     { 
      if (w.ToNormalString().Equals(word)) 
      { 
       result = true; 
       break; 
      } 
     } 
     return result; 
    } 
} 
संबंधित मुद्दे