c# - why nothing shows up in my output file, it comes out to be blank? -
using system; using system.collections.generic; using system.linq; using system.text; using system.text.regularexpressions; using system.io; namespace wordfreq { class program { static void main(string[] args) { string fullreview = file.readalltext("c:\\reviews.txt").tolower(); string[] stripchars = { ";", ",", ".", "-", "_", "^", "(", ")", "[", "]", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "\n", "\t", "\r","<",">" }; foreach (string character in stripchars) { fullreview = fullreview.replace(character, ""); } // split on spaces list of strings list<string> wordlist = fullreview.split(' ').tolist(); // define , remove stopwords string[] stopwords = new string[] {"a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", "aren't", "as","at", "be", "because", "been", "before", "being", "below", "between", "both", "but", "by", "can't", "cannot", "could", "couldn't", "did", "didn't", "do", "does", "doesn't", "doing", "don't", "down", "during", "each", "few", "for", "from", "further", "had", "hadn't", "has", "hasn't", "have", "haven't", "having", "he", "he'd", "he'll", "he's", "her", "here", "here's", "hers", "herself", "him", "himself", "his", "how", "how's", "i", "i'd", "i'll", "i'm", "i've", "if", "in", "into", "is", "isn't", "it", "it's", "its", "itself", "let's", "me", "more", "most", "mustn't", "my", "myself", "no", "nor", "not", "of", "off", "on", "once", "only", "or", "other", "ought", "our", "ours ", " ourselves", "out", "over", "own", "same", "shan't", "she", "she'd", "she'll", "she's", "should", "shouldn't", "so", "some", "such", "than", "that", "that's", "the", "their", "theirs", "them", "themselves", "then", "there", "there's", "these", "they", "they'd", "they'll", "they're", "they've", "this", "those", "through", "to", "too", "under", "until", "up", "very", "was", "wasn't", "we", "we'd", "we'll", "we're", "we've", "were", "weren't", "what", "what's", "when", "when's", "where", "where's", "which", "while", "who", "who's", "whom", "why", "why's", "with", "won't", "would", "wouldn't", "you", "you'd", "you'll", "you're", "you've", "your", "yours", "yourself", "yourselves" }; foreach (string word in stopwords) { // while there's still instance of stopword in wordlist, remove it. // if don't use while loop on each call remove removes single // instance of stopword our wordlist, , can't call replace on // entire string (as opposed individual words in string) it's // indiscriminate (i.e. removing 'and' turn words 'bandage' 'bdage'!) while (wordlist.contains(word)) { wordlist.remove(word); } } // create new dictionary object dictionary<string, int> dictionary = new dictionary<string, int>(); // loop on over words in our wordlist... foreach (string word in wordlist) { // if length of word @ least 3 letters... if (word.length >= 2) { // ...check if dictionary has word. if (dictionary.containskey(word)) { // if have word in dictionary, increment count of how many times appears dictionary[word]++; } else { // otherwise, if it's new word add dictionary initial count of 1 dictionary[word] = 1; } } // end of word length check } // end of loop on each word in our input // create dictionary sorted value (i.e. how many times word occurs) var sorteddict = (from entry in dictionary orderby entry.value descending select entry).todictionary(keyvaluepair => keyvaluepair.key, keyvaluepair => keyvaluepair.value); // loop through sorted dictionary , output top 10 occurring words int count = 1; console.writeline("---- frequent terms in file: " +fullreview+ " ----"); console.writeline(); foreach (keyvaluepair<string, int> keyvaluepair in sorteddict) { // output occurring words , associated word counts console.writeline(count + "\t" + keyvaluepair.key + "\t" + keyvaluepair.value); count++; // display top 10 words break out of loop! } using (streamwriter streamwrite = new streamwriter("c:\\output.txt")) foreach (keyvaluepair<string, int> keyvaluepair in dictionary) { streamwrite.writeline("{0}:{1}", keyvaluepair.key, keyvaluepair.value); } // wait user press key before exiting } // end of main method } // end of program class } // end of namespace
first please take @ links remus suggested in comments.
secondly, code should work, thing that's wrong here you're trying write directly onto c:\
. try place under directory such c:\temp\
, should go.
take here more info why happens.
Comments
Post a Comment