如何将一篇英文文章打散成单词集合

MikeCheers 2010-06-17 03:59:38
例文:
This section is written by M. Jordan.

希望得到如下集合:
Dictionary<int, string> words = new Dictionary<int, string>();
words[0] = "This";
words[1] = "section";
words[2] = "is";
words[3] = "written";
words[4] = "by";
words[5] = "M."; // 如果只有一个字母后面跟"." 则保留"."
words[6] = "Jordan"; // 如果多个字母后面跟"." 则忽略"."

我目前有可以实现这个功能的函数,但是效率不高,希望高手能够指点高效的方法。
public static Dictionary<int, string> BreakToWords3(string sentence)
{
Dictionary<int, string> dicWords = new Dictionary<int, string>();
if (sentence == null || sentence.Length == 0) return dicWords;
sentence = sentence.ToLower();

bool start = false;
const int MAXWORDLEN = 30;
int index = 0, cindex = 0;
char[] cs = null;

foreach (char c in sentence)
{
if (char.IsLetter(c))
{
if (!start)
{
cs = new char[MAXWORDLEN];
//tmpStr = string.Empty;
start = true;
}
if (cindex < MAXWORDLEN)
{
cs.SetValue(c, cindex);
cindex++;
}
else
{
cs = null;
cindex = 0;
start = false;
}
//tmpStr = string.Concat(tmpStr, c);
continue;
}
if (start && cindex == 1 && c == '.')
{
cs.SetValue(c, cindex);
cindex++;
//tmpStr = string.Concat(tmpStr, c);
continue;
}
if (cindex > 0)
{
Array.Resize<char>(ref cs, cindex);
dicWords.Add(index, new string(cs));
cindex = 0;
index++;
}
start = false;
}
if (start)
{
Array.Resize<char>(ref cs, cindex);
dicWords.Add(index, new string(cs));
start = false;
cs = null;
}

return dicWords;
}
...全文
497 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
MikeCheers 2010-06-20
  • 打赏
  • 举报
回复
还是我自己的效率高一些 :(
Alden 2010-06-18
  • 打赏
  • 举报
回复
最简单的使用空格来拆分,复杂点的自己写程序扫描,这要看你的需求而定.
dylike 2010-06-18
  • 打赏
  • 举报
回复
英文分词比较容易,基本上只要以标点分割即可(单引号'除外)
MikeCheers 2010-06-18
  • 打赏
  • 举报
回复
To:hzzasdf
非常感谢,我研究研究它 :)
MikeCheers 2010-06-18
  • 打赏
  • 举报
回复
好像没有朋友考虑我的这条需求
words[5] = "M."; // 如果只有一个字母后面跟"." 则保留"."
words[6] = "Jordan"; // 如果多个字母后面跟"." 则忽略"."
而且正则的效率不高, 不知道还有没有其他思路.
因为要处理海量文本, 高效率是其中一个重要需求.
  • 打赏
  • 举报
回复
楼上的需求用lucene.net应该能实现,这象是个全文搜索的问题,lucene.net正是做这个的。
正在看lucene.net,理解还非常粗浅。说是它先对文本建索引,扫描文本其实还是词法分析,建索引大概有它比较高效的算法,然后搜索时就利用建好的索引来进行,据说非常高效。
MikeCheers 2010-06-18
  • 打赏
  • 举报
回复
就是因为我现在的程序效率还是没有达到预期,所以恳求一个高效的解决方案,我也实验过很多方法,效果都不理想;
朋友们提到的Lucene.net我没有接触过,是否可以简单介绍一下它的处理思路?

或者我可以把需求做一个扩展,希望能得到更多的idea,我现在有一个词语列表,希望能高效的找出一篇文章中,有哪些词语出现过。
  • 打赏
  • 举报
回复
如楼上说的,要求效率高,可以用Lucene.net,自己写程序,类似楼主那样的代码我写过,处理含有12000个单词也就1,2秒钟,我觉得已经可以满意了,如果处理几百万个单词,那肯定不行;改用编译器所用的词法分析程序来实现,可能效率会高些,但没测试过性能。
词法分析,无非是用正则表达式定义一些规则,然后用某些工具如antlr, javaCC(.net下也有类似工具,但参考资料比java少得多)生成词法分析器。
jianuMan 2010-06-18
  • 打赏
  • 举报
回复
用空格键分割字符串
listenyang 2010-06-18
  • 打赏
  • 举报
回复
我觉得可以用分割字符串方法Split(),然后逐行操作。
dylike 2010-06-18
  • 打赏
  • 举报
回复
public void GetWords(string S)
{
string[] SP = S.Split(" ");
for (int i = 0; i <= SP.Length - 1; i++) {
if (SP[i].EndsWith(".") == true && SP[i].IndexOf(".") == 1) {
Console.WriteLine(SP[i]);
} else if (SP[i].EndsWith(".") == true && SP[i].IndexOf(".") != 1) {
Console.WriteLine(SP[i].Substring(0, SP[i].Length - 1));
} else {
Console.WriteLine(SP[i]);
}
}
}

private void Button1_Click(System.Object sender, System.EventArgs e)
{
GetWords("This section is written by M. Jordan.");
}
dylike 2010-06-18
  • 打赏
  • 举报
回复
public void GetWords(string S)  

{

string[] SP = S.Split(" ");

for (int i = 0; i <= SP.Length - 1; i++) {

if (SP[i].EndsWith(".") == true && SP[i].IndexOf(".") == 1) {

Console.WriteLine(SP[i]);

} else if (SP[i].EndsWith(".") == true && SP[i].IndexOf(".") != 1) {

Console.WriteLine(SP[i].Substring(0, SP[i].Length - 1));

} else {

Console.WriteLine(SP[i]);

}

}

}



private void Button1_Click(System.Object sender, System.EventArgs e)

{

GetWords("This section is written by M. Jordan.");

}
daichenghua 2010-06-17
  • 打赏
  • 举报
回复
路过 学习
兔子-顾问 2010-06-17
  • 打赏
  • 举报
回复
要考虑单词写不完一行的连接符。
以下代码未验证
string line = "This section is written by M. Jordan.";
string[] words = (from s in Regex.Matches(line,@"[\w.]+(-\r\n\w+)?").Cast<Match>().ToList() select s.Value).ToArray();
whb147 2010-06-17
  • 打赏
  • 举报
回复
英文直接用空格分隔就可以了
wuyq11 2010-06-17
  • 打赏
  • 举报
回复
string[] arr=s.Split(' ');
分词lucene.net,还有ICTCLAS 中文分词
haa17 2010-06-17
  • 打赏
  • 举报
回复
匹配空字符

[Quote=引用 3 楼 healer_kx 的回复:]

string[] ss = Regex.Split(sentence, @"\s+");
[/Quote]
MikeCheers 2010-06-17
  • 打赏
  • 举报
回复
To:wangkun9999
什么分词组件,能详细说说吗?

To:sprc_lcl
string[] ss = Regex.Split(sentence,@"\s+");
for(int i=0;i<ss.Length;i++)
{
dicWords.Add(i,ss[i]);
}
这个效率我先测试下

To:healer_kx
谢谢关注,方法与二楼一样。
healer_kx 2010-06-17
  • 打赏
  • 举报
回复
string[] ss = Regex.Split(sentence, @"\s+");
sprc_lcl 2010-06-17
  • 打赏
  • 举报
回复
public static Dictionary<int, string> BreakToWords3(string sentence)
{
Dictionary<int, string> dicWords = new Dictionary<int, string>();
if (sentence == null || sentence.Length == 0) return dicWords;

string[] ss = Regex.Split(sentence,@"\s+");
for(int i=0;i<ss.Length;i++)
{
dicWords.Add(i,ss[i]);
}
加载更多回复(1)

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧