private void SplitFileByLength(int length) { //Encoding encoding = null; length = length - 100; //100个字的Buffer //string str = FileOperationHelper.ReadFileContent(txbFilePath.Text, ref encoding, Encoding.UTF8); Encoding encoding = FileOperationHelper.GetEncodingType(txbFilePath.Text); string str = FileOperationHelper.ReadFile(txbFilePath.Text, encoding); int fileIndex = 1; do { if (str.Length == 0) { break; } string content = str.Substring(0, Math.Min(str.Length, length)); str = str.Substring(content.Length); int i = Math.Min(str.IndexOf('\r'), str.IndexOf('\n')); if (i != -1) { content += str.Substring(0, i + 2); str = str.Substring(i + 2); } string newFile = GetWriteFilePath(fileIndex++); FileOperationHelper.WriteFile(newFile, encoding, content); rtbLogs.AppendText(newFile + "\r\n"); } while (true); }
private void btnPreview_Click(object sender, EventArgs e) { var filePaths = txbFilePath.Text.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); if (filePaths.Length == 0) { MessageBox.Show("未选择字幕文件"); return; } SubtitleFiles.Clear(); stOperator = SubtitleHelper.GetOperatorByFileName(filePaths[0]); foreach (var filePath in filePaths) { var content = FileOperationHelper.ReadFile(filePath); var srts = stOperator.Parse(content); SubtitleFiles.Add(filePath, srts); } richTextBox1.Clear(); foreach (var subtitleFile in SubtitleFiles) { richTextBox1.AppendText("----" + subtitleFile.Key + "----\r\n"); richTextBox1.AppendText(stOperator.Subtitle2String(subtitleFile.Value)); } }
public void BatchTest() { var txt = FileOperationHelper.ReadFile("erbi.txt"); foreach (var line in txt.Split('\n')) { var arr = line.Split(' '); var code = arr[0]; for (var i = 1; i < arr.Length; i++) { var word = arr[i]; var codes = generater.GetCodeOfString(word); try { if (!IsContain(codes, code)) { Debug.WriteLine("Not Match:" + word + "\t" + code + " mycode:" + CollectionHelper.ListToString(codes, " ")); } } catch (Exception ex) { Debug.WriteLine("Error:" + word + ";" + ex.Message); } } } }
private void btnPickupNewWords_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() != DialogResult.OK) { return; } SentenceParse sentenceParse = new SentenceParse(); DirectoryInfo directoryInfo = new DirectoryInfo(folderBrowserDialog1.SelectedPath); List <string> sentences = new List <string>(); foreach (FileInfo fileInfo in directoryInfo.GetFiles()) { string filePath = fileInfo.FullName; var txt = FileOperationHelper.ReadFile(filePath); var stOperator = SubtitleHelper.GetOperatorByFileName(filePath); var srts = stOperator.Parse(txt); srts = stOperator.RemoveChinese(srts); sentences.AddRange(srts.Bodies.Values.Select(l => l.EnglishText)); } Splash.Show(); Splash.Status = "解析字幕中..."; IDictionary <string, VPreviewWord> previewWords = new Dictionary <string, VPreviewWord>(); foreach (var sentence in sentences) { var newWords = sentenceParse.Pickup(sentence); foreach (KeyValuePair <string, string> keyValuePair in newWords) { string original = keyValuePair.Key; string word = keyValuePair.Value; if (previewWords.ContainsKey(original)) { previewWords[original].Rank++; } else { var mean = sentenceParse.RemarkWord(sentence, word, original); if (mean != null) { var wd = new VPreviewWord() { Word = mean.Word, Rank = 1, Sentence = sentence, Mean = mean.DefaultMean == null ? mean.Means[0].ToString() : mean.DefaultMean.ToString() }; previewWords.Add(original, wd); } } } } DisplayPreviewWords(previewWords.Values); Splash.Close(); }
public void TestGetFileEncoding(string path, string encoding) { var e = FileOperationHelper.GetEncodingType(path); Assert.AreEqual(e.ToString(), Encoding.GetEncoding(encoding).ToString()); var txt = FileOperationHelper.ReadFile(path); Debug.WriteLine(txt); }
public void TestGetFileEncoding(string path, string encoding) { path = GetFullPath(path); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var e = FileOperationHelper.GetEncodingType(path); Assert.AreEqual(e.EncodingName, Encoding.GetEncoding(encoding).EncodingName); var txt = FileOperationHelper.ReadFile(path); Debug.WriteLine(txt); }
public WordLibraryList Import(string path) { var tempUnzipFolder = FileOperationHelper.GetCurrentFolderPath(); FileOperationHelper.UnZip(path, tempUnzipFolder); var tempFilePath = Path.Combine(FileOperationHelper.GetCurrentFolderPath(), "dictionary.txt"); string str = FileOperationHelper.ReadFile(tempFilePath, new UTF8Encoding(false)); File.Delete(tempFilePath); return(ImportText(str)); }
public EmojiReplacer(string path) { string str = FileOperationHelper.ReadFile(path); foreach (var line in str.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) { var arr = line.Split('\t'); var emoji = arr[0]; var word = arr[1]; mapping[word] = emoji; } }
private void ReadAndShowSubtitle() { var txt = FileOperationHelper.ReadFile(txbSubtitleFilePath.Text); stOperator = SubtitleHelper.GetOperatorByFileName(txbSubtitleFilePath.Text); var srts = stOperator.Parse(txt); srts = stOperator.RemoveChinese(srts); srts = stOperator.RemoveFormat(srts); ShowSubtitleText(srts.Bodies.Values); subtitle = srts; }
private void btnParse_Click(object sender, EventArgs e) { var txt = FileOperationHelper.ReadFile(txbSubtitleFilePath.Text); stOperator = SubtitleHelper.GetOperatorByFileName(txbSubtitleFilePath.Text); var srts = stOperator.Parse(txt); srts = stOperator.RemoveChinese(srts); ShowSubtitleText(srts.Bodies); subtitle = srts; }
private void SplitFileByLine(int maxLine) { Encoding encoding = FileOperationHelper.GetEncodingType(txbFilePath.Text); string str = FileOperationHelper.ReadFile(txbFilePath.Text, encoding); string splitLineChar = "\r\n"; if (str.IndexOf(splitLineChar) < 0) { if (str.IndexOf('\r') > 0) { splitLineChar = "\r"; } else if (str.IndexOf('\n') > 0) { splitLineChar = "\n"; } else { MessageBox.Show("不能找到行分隔符"); return; } } string[] list = str.Split(new[] { splitLineChar }, StringSplitOptions.RemoveEmptyEntries); var fileContent = new StringBuilder(); int fileIndex = 1; for (int i = 0; i < list.Length; i++) { fileContent.Append(list[i]); fileContent.Append(splitLineChar); if ((i + 1) % maxLine == 0 || i == list.Length - 1) { if (i != 0) { string newFile = GetWriteFilePath(fileIndex++); FileOperationHelper.WriteFile(newFile, encoding, fileContent.ToString()); rtbLogs.AppendText(newFile + "\r\n"); fileContent = new StringBuilder(); } } } }
/// <summary> /// 读取外部的字典文件,覆盖系统默认字典 /// </summary> /// <param name="dictionary"></param> protected virtual void OverrideDictionary(IDictionary <char, IList <string> > dictionary) { var fileContent = FileOperationHelper.ReadFile("mb.txt"); if (fileContent != "") { foreach (string line in fileContent.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)) { string[] arr = line.Split('\t'); if (arr[0].Length == 0) { continue; } char word = arr[0][0]; string code = arr[1]; var codes = code.Split(' '); dictionary[word] = new List <string>(codes);//强行覆盖现有字典 } } }
private string GetMutiPinyin() { string path = ConstantString.PinyinLibPath; var sb = new StringBuilder(); if (File.Exists(path)) { string txt = FileOperationHelper.ReadFile(path); var reg = new Regex(@"^('[a-z]+)+\s[\u4E00-\u9FA5]+$"); string[] lines = txt.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < lines.Length; i++) { if (reg.IsMatch(lines[i])) { sb.Append(lines[i] + "\r\n"); } } } sb.Append(Dictionaries.WordPinyin); return(sb.ToString()); }
private void btnMergeWL_Click(object sender, EventArgs e) { string mainWL = FileOperationHelper.ReadFile(txbMainWLFile.Text); Dictionary <string, List <string> > mainDict = ConvertTxt2Dictionary(mainWL); string[] userFiles = txbUserWLFiles.Text.Split('|'); foreach (string userFile in userFiles) { string filePath = userFile.Trim(); string userTxt = FileOperationHelper.ReadFile(filePath); Dictionary <string, List <string> > userDict = ConvertTxt2Dictionary(userTxt); Merge2Dict(mainDict, userDict); } if (cbxSortByCode.Checked) { var keys = new List <string>(mainDict.Keys); keys.Sort(); var sortedDict = new Dictionary <string, List <string> >(); foreach (string key in keys) { sortedDict.Add(key, mainDict[key]); } mainDict = sortedDict; } string result = Dict2String(mainDict); richTextBox1.Text = result; if ( MessageBox.Show("是否将合并的" + mainDict.Count + "条词库保存到本地硬盘上?", "是否保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { FileOperationHelper.WriteFile(saveFileDialog1.FileName, Encoding.Unicode, result); } } }
public WordLibraryList Import(string path) { string str = FileOperationHelper.ReadFile(path, Encoding); return(ImportText(str)); }
/// <summary> /// 通过搜狗细胞词库txt内容构造词库对象 /// </summary> /// <param name="str"></param> /// <returns></returns> public virtual WordLibraryList Import(string path) { string str = FileOperationHelper.ReadFile(path); return(ImportText(str)); }
private void LoadData() { //try //{ string path = GetText(); if (path == "") { MessageBox.Show("请选择一个搜狗词库的文件"); return; } string str = FileOperationHelper.ReadFile(path); string[] lines = str.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); int count = lines.Length; var wls = new Dictionary <string, string>(); for (int i = 0; i < count; i++) { string line = lines[i]; if (line[0] == ';') //说明 { continue; } string[] hzpy = line.Split(' '); string py = hzpy[0]; string hz = hzpy[1]; if (NeedSave(hz, py)) { //多音字做如下处理 if (!wls.ContainsKey(hz)) { wls.Add(hz, py); } } processString = i + "/" + count; } ShowTextMessage("开始载入现有的注音库"); string pylibString = FileOperationHelper.ReadFile(ConstantString.PinyinLibPath); lines = pylibString.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < lines.Length; i++) { string line = lines[i]; string[] hzpy = line.Split(' '); string py = hzpy[0]; string hz = hzpy[1]; if (!wls.ContainsKey(hz)) { wls.Add(hz, py); } processString = i + "/" + count; } ShowTextMessage("载入全部完成,开始去除重复"); Dictionary <string, string> rst = RemoveDuplicateWords(wls); ShowTextMessage("去除重复完成,开始写入文件"); StreamWriter sw = FileOperationHelper.WriteFile(ConstantString.PinyinLibPath, Encoding.Unicode); //清空注音库文件 foreach (string key in rst.Keys) { string line = rst[key] + " " + key; FileOperationHelper.WriteFileLine(sw, line); } sw.Close(); toolStripStatusLabel1.Text = "完成!"; ShowTextMessage("完成!"); timer1.Stop(); MessageBox.Show("完成!"); //} //catch (Exception ex) //{ // ShowTextErrorMessage(ex.Message); //} }