编写小程序,测试你的严谨思维能力

有随便的一篇英文文章,把文章里的所有的英文单词都调取出来,比如 I am a chinese.So i love China.......等等。。。 输出的结果就是
I
am
a
chinese
.
so
i
love
China
.
.......

抛土坷垃引玉,我的解法:

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text.RegularExpressions;
 4 
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             string strSource = " I am a chinese.So i love China.";
10 
11             MatchCollection matches = Regex.Matches(strSource, @"(([\w]+)|([\.]{1})(?=[^\.]{1})|([\.]{1})(?=[\.]{6})|([\.]{6,6}))");
12 
13             for (int i=0;i<matches.Count;i++)
14             {
15                 Console.WriteLine(matches[i].Groups[1].Value);
16             }
17 
18             Console.ReadKey();
19         }
20     }

 

用了正则,只能应付例句中的单词和标点,如果有更繁杂的标点如:,! ' &等等之类的,请大家不断给出不同的英文段落和测试程序,看看谁的更完善。

posted @ 2009-06-18 10:58 ztotem 阅读(2260) 评论(22) 编辑 收藏

 回复 引用   
#1楼 2009-06-18 11:29 Sapphire[未注册用户]
单词允许重复出现吗?
 回复 引用 查看   
#2楼[楼主] 2009-06-18 11:32 ztotem      
@Sapphire
sorry,没有描述清楚,单词允许重复出现,并且类似I'm 应该是一项,有-连字符的情况也要考虑。越贴进实际情况越好。

 回复 引用   
#3楼 2009-06-18 11:37 Sapphire[未注册用户]
static void Main(string[] args)
{
string source = @"WASHINGTON – From simple home loans to Wall Street's most exotic schemes, the government would impose and enforce sweeping new rules of the road for the nation's battered financial system under an overhaul proposed Wednesday by President Barack Obam
Aimed at preventing a repeat of the worst economic crisis in seven decades, the changes would begin to reverse a determined campaign pressed in the 1980s by President Ronald Reagan to cut back on federal regulations.
Obama's plan, spelled out in an 88-page white paper, would do little to streamline the alphabet soup of agencies that oversee the financial sector. But it calls for fundamental shifts in authority that would eliminate one regulatory agency, create another and both enhance and undercut the authority of the powerful Federal Reserve.";

string[] words = GetAnyWords(source);

foreach(string s in words)
{
Console.WriteLine(s);
}

Console.ReadKey();
}

private static string[] GetAnyWords(string source)
{
List<string> Words = new List<string>();

StringBuilder sb = new StringBuilder();
foreach (char c in source)
{
if (!IsLetter(c))
{
Words.Add(sb.ToString());
Words.Add(c.ToString());
sb.Remove(0, sb.Length);
}
else
{
sb.Append(c.ToString());
}
}

return Words.ToArray();
}

private static bool IsLetter(char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
return true;
}

return false;
}


暂时不符合【并且类似I'm 应该是一项,有-连字符的情况也要考虑】这样的条件。。。不过,要修改成那样也不麻烦

 回复 引用 查看   
#4楼 2009-06-18 11:41 guojing      
题目有点小无聊,但是还行。。

但是如果用正则做的话我觉得本身没有多大的意义。。

 回复 引用   
#5楼 2009-06-18 11:57 solosky
这个是典型的分词问题吧,英语来说是最简单的,因为每个单词都有空格隔开,逐个扫描空格,分割就行了。至于其他标点符号在分词后做点判断就行了~~

要是中文就相当的难了。。

 回复 引用 查看   
#6楼 2009-06-18 12:04 guojing      
英文本来就比较简单,而且这个既然能够使用类库,就根本就不需要考虑算法了什么了的,不知道这题目考什么。。

namespace Test
{
class Program
{
static void Main(string[] args)
{
string str = "Hello I'm Chinese,so I love China.My Name is Guo-Jing http://www.shangducms.com";
str = Transform(str);
Split(str, ' ');
Console.ReadKey();
}

static void Split(string str,char charLetter)
{
string[] words = str.Split(charLetter);
for (int i = 0; i < words.Length; i++)
{
if(words[i]!="")
Console.WriteLine(words[i]);
}
}

static string Transform(string str)
{
char[] chars = str.ToCharArray();
string retValue = string.Empty;

for(int i=0;i<chars.Length;i++)
{
if (!((chars[i] >= 'a' && chars[i] <= 'z') || (chars[i] >= 'A' && chars[i] <= 'Z')||chars[i]=='\''||chars[i]=='-'))
{
chars[i] = ' ';
}
retValue += chars[i].ToString();
}

return retValue;
}

}
}

输出结果

Hello
I'm
Chinese
so
I
love
China
My
Name
is
Guo-Jing
http
www
shangducms
com

 回复 引用   
#7楼 2009-06-18 12:43 bbbbbutton[未注册用户]
题目出得都不严谨,还考验编程人的严谨思维?
按题目的说法,直接用String.Split(' ')就成,还需要正则?

 回复 引用   
#8楼 2009-06-18 12:58 ddd[未注册用户]
private string Test(string _string)
{
string _return = "";
char[] _speicalChar ={ ',', '.' };//定义不显示的特殊字符
for (int i = 0; i < _speicalChar.Length; i++)
{
_string.Replace(_speicalChar[i], ' ');
}
string[] _wordsList = _string.Split(' ');
for (int i = 0; i < _wordsList.Length; i++)
{
_return += _wordsList[i] + "<br/>";
}
return _return;
}

 回复 引用 查看   
#9楼 2009-06-18 13:05 徐少侠      
--引用--------------------------------------------------
bbbbbutton: 题目出得都不严谨,还考验编程人的严谨思维?
按题目的说法,直接用String.Split(' ')就成,还需要正则?
--------------------------------------------------------
比较赞同

即使有多种符号的情况,那么可以如下操作
枚举所有分隔符,对目标字符串进行replace,都换成空格
这个步骤也可以使用一个正则替换完成

压缩所有一个以上的空格
也可以用一个正则搞定

然后,根据空格分词

正常的三个正则能解决问题了

 回复 引用 查看   
#10楼 2009-06-18 13:06 徐少侠      
--引用--------------------------------------------------
ddd: private string Test(string _string)
{
string _return = &quot;&quot;;
char[] _speicalChar ={ ',', '.' };//定义不显示的特殊字符
for (int i = 0; i &lt; _speicalChar.Length; i++)
{
_string.Replace(_speicalChar[i], ' ');
}
string[] _wordsList = _string.Split(' ');
for (int i = 0; i &lt; _wordsList.Length; i++)
{
_return += _wordsList[i] + &quot;&lt;br/&gt;&quot;;
}
return _return;
}
--------------------------------------------------------
晕,没看见这个老大已经写出来了

呵呵

 回复 引用 查看   
#11楼 2009-06-18 13:18 为爱痴狂      
我想这就是考验正则表达式的能力吧!没什么难的.
你应该用\b.
用这个正则表达式试试:
@"^.+?\b|\b.+?\b|\b.+?$"
如果不想匹配到空格,这将'.'改为'\S'即可:
@"^\S+?\b|\b\S+?\b|\b\S+?$"

 回复 引用 查看   
#12楼 2009-06-18 13:29 James.Ying      
这个。。。。不算分词吧,只是按照一定格式来匹配,比如文章中有ma,难道也算一个单词吗?

 回复 引用 查看   
#13楼[楼主] 2009-06-18 13:38 ztotem      
@bbbbbutton
题目确实出的不够严谨,出这个题的目的也是为了在有闲的时候,自己考虑有哪些可能性,并不局限于我所说的例子。发散性的提问题,分析,并尝试解决。

集思广益的乐趣是无穷的。:)

 回复 引用 查看   
#14楼 2009-06-18 13:39 谢慧琦      
@Sapphire
支持你的方法

 回复 引用 查看   
#15楼 2009-06-18 13:41 谢慧琦      
不建议使用正则表达式
对于大片的文章,能够使用最基本的字符串函数一遍处理是最佳的
使用正则表达式性能不可控

 回复 引用 查看   
#16楼[楼主] 2009-06-18 13:49 ztotem      
--引用--------------------------------------------------
谢慧琦: 不建议使用正则表达式
对于大片的文章,能够使用最基本的字符串函数一遍处理是最佳的
使用正则表达式性能不可控
--------------------------------------------------------
嗯,确实是这样。
表现看起来很简单,如果耐心深入分析,发现里面的水未必是浅的。老外在这方面科学严谨的态度还是值得学习的。

 回复 引用   
#17楼 2009-06-18 15:26 woalang[未注册用户]
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Class2
{
static void Main(string[] args)
{
string str = "Hello I'm Chinese,so I love China.My Name is Guo-Jing http://www.shangducms.com";
Split(str);
Console.ReadKey();
}

static void Split(string str)
{
byte[] bs = Encoding.GetEncoding("GB2312").GetBytes(str);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bs.Length; i++)

if (bs[i] == 32 || bs[i] == 44 || bs[i] == 46 || bs[i] == 58)
{
sb.Append("\n");
}
else if (bs[i] == 47)
{

}
else if (bs[i] < 128)
sb.Append((char)bs[i]);

Console.WriteLine(sb.ToString());
}
}
}

 回复 引用 查看   
#18楼 2009-06-18 16:59 lazylu      
有这么多评论贴代码这篇文章就算成功的了
 回复 引用 查看   
#19楼 2009-06-18 17:19 richie.ju      
不得不顶啊~
 回复 引用 查看   
#20楼 2009-06-18 18:49 冰の酷龙      
@lazylu
顶,我觉得回复的思路比楼主的更值得学习
嘿嘿,别见怪。

 回复 引用 查看   
#21楼[楼主] 2009-06-18 19:38 ztotem      
@冰の酷龙
呵呵,能从不同的角度看问题,是一种优秀品质

 回复 引用   
#22楼 2009-06-23 11:17 YokerWu[未注册用户]
为什么要用上 循环 呐?