【发布时间】:2012-01-27 01:27:02
【问题描述】:
我需要一种快速的方法来处理大文本文件
我有 2 个文件, 一个大文本文件(~20Gb) 以及另一个包含约 1200 万个 Combo 单词列表的文本文件
我想在第一个文本文件中找到所有组合词并将其替换为另一个组合词(带下划线的组合词)
示例“计算机信息”>替换为>“计算机信息”
我使用此代码,但性能很差(我在具有 16Gb 内存和 16 核的 Hp G7 服务器中进行测试)
public partial class Form1 : Form
{
HashSet<string> wordlist = new HashSet<string>();
private void loadComboWords()
{
using (StreamReader ff = new StreamReader(txtComboWords.Text))
{
string line;
while ((line = ff.ReadLine()) != null)
{
wordlist.Add(line);
}
}
}
private void replacewords(ref string str)
{
foreach (string wd in wordlist)
{
// ReplaceEx(ref str,wd,wd.Replace(" ","_"));
if (str.IndexOf(wd) > -1)
str.Replace(wd, wd.Replace(" ", "_"));
}
}
private void button3_Click(object sender, EventArgs e)
{
string line;
using (StreamReader fread = new StreamReader(txtFirstFile.Text))
{
string writefile = Path.GetFullPath(txtFirstFile.Text) + Path.GetFileNameWithoutExtension(txtFirstFile.Text) + "_ReplaceComboWords.txt";
StreamWriter sw = new StreamWriter(writefile);
long intPercent;
label3.Text = "initialing";
loadComboWords();
while ((line = fread.ReadLine()) != null)
{
replacewords(ref line);
sw.WriteLine(line);
intPercent = (fread.BaseStream.Position * 100) / fread.BaseStream.Length;
Application.DoEvents();
label3.Text = intPercent.ToString();
}
sw.Close();
fread.Close();
label3.Text = "Finished";
}
}
}
在合理的时间内完成这项工作的任何想法
谢谢
【问题讨论】:
标签: c# string list replace text-processing