c# - Write to file cut off -
my goal take file of sentences, apply basic filtering, , output remaining sentences file , terminal. i'm using hunspell library.
here's how sentences file:
public static string[] sentencesfromfile_old(string path) { string s = ""; using (streamreader rdr = file.opentext(path)) { s = rdr.readtoend(); } s = s.replace(environment.newline, " "); s = regex.replace(s, @"\s+", " "); s = regex.replace(s, @"\s*?(?:\(.*?\)|\[.*?\]|\{.*?\})", string.empty); string[] sentences = regex.split(s, @"(?<=\. |[!?]+ )"); return sentences; } here's code writes file:
list<string> sentences = new list<string>(checker.sentencesfromfile_old(path)); streamwriter w = new streamwriter(outfile); foreach(string x in xs) if(checker.check(x, speller)) { w.writeline("[{0}]", x); console.writeline("[{0}]", x); } here's checker:
public static bool check(string s, nhunspell.hunspell speller) { char[] punctuation = {',', ':', ';', ' ', '.'}; bool upper = false; // check string length. if(s.length <= 50 || s.length > 250) return false; // check if string contains allowed punctuation , letters. // disallow words multiple consecutive caps. for(int = 0; < s.length; ++i) { if(punctuation.contains(s[i])) continue; if(char.isupper(s[i])) { if(upper) return false; upper = true; } else if(char.islower(s[i])) { upper = false; } else return false; } // spellcheck each word. string[] words = s.split(' '); foreach(string word in words) if(!speller.spell(word)) return false; return true; } the sentences printed on terminal fine, text file cuts off mid-sentence @ 2015 characters. what's that?
edit: when remove parts of check method, file cut off @ various lengths somewhere around either 2000 or 4000. removing spellcheck eliminates cutoff entirely.
you need flush stream before closing it.
w.flush(); w.close(); the using statement (which should use) close stream automatically, not flush it.
using( var w = new streamwriter(...) ) { // stuff w.flush(); }
Comments
Post a Comment