2017-04-14 11 views
2

이것은 Stop 단어를 제거하기위한 C# 프로그램이며 _delimiters에 오류가 있습니다. 하나만 도와주세요! 내가이 오류 와 아웃 풋은C# stop Word Dictionaryfom String

고양이 말을보고해야 복구 할 수있는 방법을

는 구글은 귀하의 함수가 걸리는 모든

static class StopwordTool 
    { 
     static Dictionary<string, bool> _stops = new Dictionary<string,bool> 
    { 
     { "a", true }, 
     { "about", true }, 
     { "above", true }, 
     { "across", true }, 
     { "after", true }, 
     { "afterwards", true }, 
     { "again", true }, 
     { "against", true }, 
     { "all", true }, 
     { "almost", true }, 
     { "alone", true }, 
     { "along", true }, 
     { "already", true }, 
     { "also", true }, 
     { "although", true }, 
     { "always", true }, 
     { "am", true }, 
     { "among", true }, 
     { "amongst", true }, 
     { "amount", true }, 
     { "an", true }, 
     { "and", true }, 
     { "another", true }, 
     { "any", true }, 
     { "anyhow", true }, 
     { "anyone", true }, 
     { "anything", true }, 
     { "anyway", true }, 
     { "anywhere", true }, 
     { "are", true }, 
     { "around", true }, 
     { "as", true }, 
     { "at", true }, 
     { "back", true }, 
     { "be", true }, 
     { "became", true }, 
     { "because", true }, 
     { "become", true }, 
     { "becomes", true }, 
     { "becoming", true }, 
     { "been", true }, 
     { "before", true }, 
     { "beforehand", true }, 
     { "behind", true }, 
     { "being", true }, 
     { "below", true }, 
     { "beside", true }, 
     { "besides", true }, 
     { "between", true }, 
     { "beyond", true }, 
     { "bill", true }, 
     { "both", true }, 
     { "bottom", true }, 
     { "but", true }, 
     { "by", true }, 
     { "call", true }, 
     { "can", true }, 
     { "cannot", true }, 
     { "cant", true }, 
     { "co", true }, 
     { "computer", true }, 
     { "con", true }, 
     { "could", true }, 
     { "couldnt", true }, 
     { "cry", true }, 
     { "de", true }, 
     { "describe", true }, 
     { "detail", true }, 
     { "do", true }, 
     { "done", true }, 
     { "down", true }, 
     { "due", true }, 
     { "during", true }, 
     { "each", true }, 
     { "eg", true }, 
     { "eight", true }, 
     { "either", true }, 
     { "eleven", true }, 
     { "else", true }, 
     { "elsewhere", true }, 
     { "empty", true }, 
     { "enough", true }, 
     { "etc", true }, 
     { "even", true }, 
     { "ever", true }, 
     { "every", true }, 
     { "everyone", true }, 
     { "everything", true }, 
     { "everywhere", true }, 
     { "except", true }, 
     { "few", true }, 
     { "fifteen", true }, 
     { "fify", true }, 
     { "fill", true }, 
     { "find", true }, 
     { "fire", true }, 
     { "first", true }, 
     { "five", true }, 
     { "for", true }, 
     { "former", true }, 
     { "formerly", true }, 
     { "forty", true }, 
     { "found", true }, 
     { "four", true }, 
     { "from", true }, 
     { "front", true }, 
     { "full", true }, 
     { "further", true }, 
     { "get", true }, 
     { "give", true }, 
     { "go", true }, 
     { "had", true }, 
     { "has", true }, 
     { "have", true }, 
     { "he", true }, 
     { "hence", true }, 
     { "her", true }, 
     { "here", true }, 
     { "hereafter", true }, 
     { "hereby", true }, 
     { "herein", true }, 
     { "hereupon", true }, 
     { "hers", true }, 
     { "herself", true }, 
     { "him", true }, 
     { "himself", true }, 
     { "his", true }, 
     { "how", true }, 
     { "however", true }, 
     { "hundred", true }, 
     { "i", true }, 
     { "ie", true }, 
     { "if", true }, 
     { "in", true }, 
     { "inc", true }, 
     { "indeed", true }, 
     { "interest", true }, 
     { "into", true }, 
     { "is", true }, 
     { "it", true }, 
     { "its", true }, 
     { "itself", true }, 
     { "keep", true }, 
     { "last", true }, 
     { "latter", true }, 
     { "latterly", true }, 
     { "least", true }, 
     { "less", true }, 
     { "ltd", true }, 
     { "made", true }, 
     { "many", true }, 
     { "may", true }, 
     { "me", true }, 
     { "meanwhile", true }, 
     { "might", true }, 
     { "mill", true }, 
     { "mine", true }, 
     { "more", true }, 
     { "moreover", true }, 
     { "most", true }, 
     { "mostly", true }, 
     { "move", true }, 
     { "much", true }, 
     { "must", true }, 
     { "my", true }, 
     { "myself", true }, 
     { "name", true }, 
     { "namely", true }, 
     { "neither", true }, 
     { "never", true }, 
     { "nevertheless", true }, 
     { "next", true }, 
     { "nine", true }, 
     { "no", true }, 
     { "nobody", true }, 
     { "none", true }, 
     { "nor", true }, 
     { "not", true }, 
     { "nothing", true }, 
     { "now", true }, 
     { "nowhere", true }, 
     { "of", true }, 
     { "off", true }, 
     { "often", true }, 
     { "on", true }, 
     { "once", true }, 
     { "one", true }, 
     { "only", true }, 
     { "onto", true }, 
     { "or", true }, 
     { "other", true }, 
     { "others", true }, 
     { "otherwise", true }, 
     { "our", true }, 
     { "ours", true }, 
     { "ourselves", true }, 
     { "out", true }, 
     { "over", true }, 
     { "own", true }, 
     { "part", true }, 
     { "per", true }, 
     { "perhaps", true }, 
     { "please", true }, 
     { "put", true }, 
     { "rather", true }, 
     { "re", true }, 
     { "same", true }, 
     { "see", true }, 
     { "seem", true }, 
     { "seemed", true }, 
     { "seeming", true }, 
     { "seems", true }, 
     { "serious", true }, 
     { "several", true }, 
     { "she", true }, 
     { "should", true }, 
     { "show", true }, 
     { "side", true }, 
     { "since", true }, 
     { "sincere", true }, 
     { "six", true }, 
     { "sixty", true }, 
     { "so", true }, 
     { "some", true }, 
     { "somehow", true }, 
     { "someone", true }, 
     { "something", true }, 
     { "sometime", true }, 
     { "sometimes", true }, 
     { "somewhere", true }, 
     { "still", true }, 
     { "such", true }, 
     { "system", true }, 
     { "take", true }, 
     { "ten", true }, 
     { "than", true }, 
     { "that", true }, 
     { "the", true }, 
     { "their", true }, 
     { "them", true }, 
     { "themselves", true }, 
     { "then", true }, 
     { "thence", true }, 
     { "there", true }, 
     { "thereafter", true }, 
     { "thereby", true }, 
     { "therefore", true }, 
     { "therein", true }, 
     { "thereupon", true }, 
     { "these", true }, 
     { "they", true }, 
     { "thick", true }, 
     { "thin", true }, 
     { "third", true }, 
     { "this", true }, 
     { "those", true }, 
     { "though", true }, 
     { "three", true }, 
     { "through", true }, 
     { "throughout", true }, 
     { "thru", true }, 
     { "thus", true }, 
     { "to", true }, 
     { "together", true }, 
     { "too", true }, 
     { "top", true }, 
     { "toward", true }, 
     { "towards", true }, 
     { "twelve", true }, 
     { "twenty", true }, 
     { "two", true }, 
     { "un", true }, 
     { "under", true }, 
     { "until", true }, 
     { "up", true }, 
     { "upon", true }, 
     { "us", true }, 
     { "very", true }, 
     { "via", true }, 
     { "was", true }, 
     { "we", true }, 
     { "well", true }, 
     { "were", true }, 
     { "what", true }, 
     { "whatever", true }, 
     { "when", true }, 
     { "whence", true }, 
     { "whenever", true }, 
     { "where", true }, 
     { "whereafter", true }, 
     { "whereas", true }, 
     { "whereby", true }, 
     { "wherein", true }, 
     { "whereupon", true }, 
     { "wherever", true }, 
     { "whether", true }, 
     { "which", true }, 
     { "while", true }, 
     { "whither", true }, 
     { "who", true }, 
     { "whoever", true }, 
     { "whole", true }, 
     { "whom", true }, 
     { "whose", true }, 
     { "why", true }, 
     { "will", true }, 
     { "with", true }, 
     { "within", true }, 
     { "without", true }, 
     { "would", true }, 
     { "yet", true }, 
     { "you", true }, 
     { "your", true }, 
     { "yours", true }, 
     { "yourself", true }, 
     { "yourselves", true } 
    }; 
     public static string RemoveStopwords(string input) 
     { 
      // 1 
      // Split parameter into words 
      var words = input.Split(_delimiters, 
       StringSplitOptions.RemoveEmptyEntries); 
      // 2 
      // Allocate new dictionary to store found words 
      var found = new Dictionary<string, bool>(); 
      // 3 
      // Store results in this StringBuilder 
      StringBuilder builder = new StringBuilder(); 
      // 4 
      // Loop through all words 
      foreach (string currentWord in words) 
      { 
       // 5 
       // Convert to lowercase 
       string lowerWord = currentWord.ToLower(); 
       // 6 
       // If this is a usable word, add it 
       if (!_stops.ContainsKey(lowerWord) && 
        !found.ContainsKey(lowerWord)) 
       { 
        builder.Append(currentWord).Append(' '); 
        found.Add(lowerWord, true); 
       } 
      } 
      // 7 
      // Return string with words removed 
      return builder.ToString().Trim(); 
     } 
    } 
} 

    class Program 
    { 
     static void Main() 
     { 
      Console.WriteLine(StopwordTool.RemoveStopwords(
       "I saw a cat and a horse")); 
      Console.WriteLine(StopwordTool.RemoveStopwords(
       "Google searches the Internet")); 
      Console.WriteLine(StopwordTool.RemoveStopwords(
       "Using an extra step")); 
     } 
    } 
+0

어디 _delimiters 정의된다? 붙여 넣은 코드에는 없습니다. 이 함수 공공 정적 스트링 RemoveStopwords에 –

+0

(입력 문자열) {// 1 워드로 분할 // 파라미터 VAR 워드 = input.Split (_delimiters, StringSplitOptions.RemoveEmptyEntries); –

+3

그것이 사용 된 곳이지만 어디에도 정의되어 있지 않습니다. –

답변

1

에서 추가 단계를

감사를 사용하여 인터넷 를 검색 첫 번째 단계는 구분 기호로 입력 문자열을 분할하는 것이지만 "_delimiters"변수는 정의되지 않습니다.

분할 기능 (String.Split Method (Char[]))은 유효한 구분 기호에 대한 문자 배열을 취합니다. 링크 된 설명서를 참조하십시오. 샘플 문자열 구분 기호로 공백 만, 가지고

그래서 수업이 같은 추가 충분해야

static Char[] _delimiters = new Char[] {}; 
+0

매우 감사합니다. –

1

단어를 자연 언어 (영어, 러시아어 등)이다 어려운 핀으로 내려. 내가 정규 표현식을 사용하는 것이 좋습니다 첫 번째 대신 string.Split의 시도로 (제거해야 구두점, 새로운 라인을 주목하시기 바랍니다) :

string input = 
    @"“The Iliad” (Gr: “Ilias”) is an epic poem by the ancient Greek poet Homer, 
    which recounts some of the significant events of the final weeks of the 
    Trojan War and the Greek siege of the city of Troy (which was also known 
    as Ilion, Ilios or Ilium in ancient times)"; 

string[] words = Regex 
    .Matches(input, @"\w+", RegexOptions.Multiline) 
    .OfType<Match>() 
    .Select(m => m.Value) 
    .ToArray(); 

또 다른 제안은 중지 단어 대신 Dictionary<string, bool>을위한 컬렉션으로 HashSet<string>을 사용하는 것입니다 :

// We want: 
// 1. Just words without redundant boolean "true" value 
// 2. Case insensive: "The" is a stop word as well as "the" 
static HashSet<string> _stops = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { 
    "a", 
    "about", 
    ... 
    "yourselves", 
}; 

마지막으로, 당신은 중지 단어, 예를 필터링 에게 Linq에을 사용할 수 있습니다

string textWithoutStopWords = string.Join(" ", words 
    .Where(word => !_stops.Contains(word))); 

Console.Write(textWithoutStopWords); 

출력

Iliad Gr Ilias epic poem ancient Greek poet...