Regex

2010-12-01 6 views
0

을 사용하여 Linq Object에서 단어 빈도 (개수)를 얻으려고합니다. this post을 취하려고 시도하고 있지만 그 방법을 알 수는 없습니다. 여기 Regex

은 시작 쿼리입니다 :

 string input = sb.ToString(); 
      string[] keywords = new[] { "i","be", "with", "are", "there", "use", "still", "do","out", "so", "will", "but", "if", "can", "your", "what", "just", "from", "all", "get", "about", "this","t", "is","and", "the", "", "a", "to", "http" ,"you","my", "for", "in", "of", "ly" , "com", "it", "on","s", "that", "bit", "at", "have", "m", "rt", "an", "was", "as", "ll", "not", "me" }; 
      Regex regex = new Regex("\\w+"); 
var stuff = regex.Matches(input) 
       .OfType<Match>() 
       .Select(c => c.Value.ToLowerInvariant()) 
       .Where(c => !keywords.Contains(c)) 
       .GroupBy(c => c) 
       .OrderByDescending(c => c.Count()) 
       .ThenBy(c => c.Key); 

하지만 각 키 값의 COUNT (주파수)뿐만 아니라 내 데이터베이스에 저장할 수 있도록 값 자체를 얻을 수 있도록하고 싶습니다 .

foreach (var item in stuff) 
      { 
       string query = String.Format("INSERT INTO sg_top_words (sg_word, sg_count) VALUES ('{0}','{1}')", item.Key, item.COUNT???); 
       cmdIns = new SqlCommand(query, conn); 
       cmdIns.CommandType = CommandType.Text; 
       cmdIns.ExecuteNonQuery(); 
       cmdIns.Dispose(); 
      } 

감사 거의 필요하시면,이 팅겨이 그것을해야하는 쿼리를 가정

답변

3

:

var stuff = regex.Matches(input) 
    .Cast<Match>() // We're confident everything will be a Match! 
    .Select(c => c.Value.ToLowerInvariant()) 
    .Where(c => !keywords.Contains(c)) 
    .GroupBy(c => c) 
    .Select(g => new { Word = g.Key, Count = g.Count() }) 
    .OrderByDescending(g => g.Count) 
    .ThenBy(g => g.Word); 

지금 순서가 Key으로, 익명 형식이 될 것

Count 속성.

데이터베이스에 삽입하는 경우 실제로 결과를 주문해야합니까?

var stuff = regex.Matches(input) 
    .Cast<Match>() // We're confident everything will be a Match! 
    .Select(c => c.Value.ToLowerInvariant()) 
    .Where(c => !keywords.Contains(c)) 
    .GroupBy(c => c) 
    .Select(g => new { Word = g.Key, Count = g.Count() }); 
+0

이제 카운트 값이 저장되었으므로 더 이상 주문할 필요가 없습니다. :) – discorax