最近在背单词,为了能提高效率,找到一个比较有名的《美国当代英语语料库COCA词频20000 》
来源应该是:http://www.wordfrequency.info/,当然是收费的。
免费的可以在这里下载pdf文件:http://vdisk.weibo.com/s/ctvvyfhPYLfj
由于是pdf文件,且包含大量其他不太有用的信息,就想到提纯。
花了半小时写了个小程序,对pdf文件进行提炼。
这个下载https://sourceforge.net/projects/pdfbox/?source=typ_redirect的相关pdf读取的dll。
引用上面4个dll,代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using org.pdfbox.pdmodel;
using org.pdfbox.util;
using System.IO;
namespace ConsoleApplication6
{
class Program
{
public static void pdf2txt(FileInfo pdffile, FileInfo txtfile)
{
PDDocument doc = PDDocument.load(pdffile.FullName);
PDFTextStripper pdfStripper = new PDFTextStripper();
string text = pdfStripper.getText(doc);
StreamWriter swPdfChange = new StreamWriter(txtfile.FullName, false, Encoding.GetEncoding("gb2312"));
swPdfChange.Write(text);
swPdfChange.Close();
}
static void Main(string[] args)
{
pdf2txt(new FileInfo(@"C:\Users\pchome\Desktop\美国当代英语语料库COCA词频20000.pdf"), new FileInfo(@"C:\Users\pchome\Desktop\output.txt"));
}
}
}
生成output.txt文件,里面包含了pdf文件里的所有内容,不过是排除了格式的。内容大致如下:
然后按照文本文件的排版规律,写了个提纯程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using org.pdfbox.pdmodel;
using org.pdfbox.util;
using System.IO;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream(@"C:\Users\pchome\Desktop\ok.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
StreamReader sr = File.OpenText(@"C:\Users\pchome\Desktop\output.txt");
string str = "";
int counter = 0;
string word;
string chix;
while ((str = sr.ReadLine()) != null)
{
var lst = str.Split(' ');
try
{
int seq = Convert.ToInt32(lst[0]);
if (counter + 1 == seq)
{
counter += 1;//词频
word = Convert.ToString(lst[1]);//单词
chix = Convert.ToString(lst[2]);//词性
string content = counter.ToString() + " " + word + " " + chix;
sw.WriteLine(content);
Console.WriteLine(content);
}
}
catch
{
}
}
sr.Close();
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
}
}
最终得到了一个包含 词频 + 单词 + 词性 的文本文件。
可以各位同学导入到其他背单词软件里,进行学习了