`
seara
  • 浏览: 655152 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

《程序员》第9期智慧擂台题目——高频词汇提取

阅读更多
本文为原创,如需转载,请注明作者和出处,谢谢!

面对浩瀚的信息海洋,找到想要的资源有时真的是不容易。在大量文字中搜索高频词汇是信息搜索和数据压缩的共通课题。
这次智慧擂台请大家在一个比较庞大的英文文本中找出M个数量最多的短语(由N个单词组成)。统一处理相同的文本文件,该文本只包含英文单词、空格和回行符,比较谁的程序效率最高。

  程序输入:M,N,文本文件路径(M不超过20,N不超过8)
程序输出:高频短语及其数量清单

擂台规则:提交符合以上要求的可执行程序,语言招式不限,点到为止;
我们将在统一的环境下对每位选手的作品进行公平的测试,
比较出综合用时最少的程序。

源程序


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->importjava.io.*;
importjava.util.*;

classtt
{
publicStringphrase;
publicintcount;
}

publicclasssearchphrase
{

privatestaticLinkedHashMapphrase=newLinkedHashMap();
statictt[]max_phrase;

privatestaticVectorSeparateString(Strings)
{
Vectorv
=newVector();
Stringtemp
="";
for(inti=0;i<s.length();i++)
{
if(s.charAt(i)!='')
{
temp
+=s.charAt(i);
}
else
{
if(temp!="")
v.add(temp);
temp
="";
}
}
if(temp!="")
v.add(temp);
returnv;
}

privatestaticvoidswap(intpos,intcount,Stringphrase)
{
inti;
if(max_phrase[pos-1].count<count)
{
for(i=pos-1;i>0;i--)
{
if(max_phrase[i-1].count>max_phrase[i].count)
break;
}
max_phrase[pos].count
=max_phrase[i].count;
max_phrase[pos].phrase
=max_phrase[i].phrase;
max_phrase[i].count
=count;
max_phrase[i].phrase
=phrase;
}

}

privatestaticvoidadjust_max(intcount,Stringphrase)
{
inti,j;
if(count<=max_phrase[max_phrase.length-1].count)
return;
for(i=max_phrase.length-1;i>=0;i--)
{
if(max_phrase[i].phrase.equals(phrase))
{
max_phrase[i].count
=count;
if(i>0)
{
swap(i,count,phrase);
}
return;
}
}
max_phrase[max_phrase.length
-1].count=count;
max_phrase[max_phrase.length
-1].phrase=phrase;
if(i>0)
{
swap(max_phrase.length
-1,count,phrase);
}
}

privatestaticvoidjs(Vectorv,intn)
{
Strings;
for(inti=0;i<v.size()-n+1;i++)
{
s
="";
for(intj=i;j<i+n;j++)
{
s
+=v.get(j)+"";
}
intcount=1;
if(phrase.containsKey(s.hashCode()))
{
count
=Integer.parseInt(phrase.get(s.hashCode()).toString());
count
++;
}
phrase.put(s.hashCode(),count);
adjust_max(count,s);
}
}

publicstaticvoidmain(String[]args)
{
try
{
longt;
intm,n;
Stringpath;
m
=Integer.parseInt(args[0]);
n
=Integer.parseInt(args[1]);
path
=args[2];
max_phrase
=newtt[m];
for(inti=0;i<m;i++)
{
max_phrase[i]
=newtt();
max_phrase[i].count
=0;
max_phrase[i].phrase
="";
}
t
=(newjava.util.Date()).getTime();
java.io.FileReaderfr
=newjava.io.FileReader(path);
java.io.BufferedReaderbr
=newBufferedReader(fr);
Strings;

Vectorv
=null;

while((s=br.readLine())!=null)
{
v
=SeparateString(s);
js(v,n);
}
for(inti=0;i<m;i++)
{
System.out.println(max_phrase[i].phrase);
System.out.println(max_phrase[i].count);
System.out.println();
}
t
=(newjava.util.Date()).getTime()-t;
System.out.print(t);
System.out.println(
"ms");
}
catch(Exceptione)
{
System.out.println(e.getMessage());
}

}

}

测试结果1:m = 20 n = 8

under games played won drawn lost goals for
71

tabulated under games played won drawn lost goals
70

games played won drawn lost goals for against
70

May Xinhua Following are the results from the
69

played won drawn lost goals for against and
59

won drawn lost goals for against and points
59

Jan Xinhua Following are the results from the
48

Chinas economic efficiency indicators of the sector of
39

The industrial statistics include all stateowned enterprises and
39

industrial statistics include all stateowned enterprises and the
39

statistics include all stateowned enterprises and the nonstateowned
39

include all stateowned enterprises and the nonstateowned ones
39

all stateowned enterprises and the nonstateowned ones with
39

stateowned enterprises and the nonstateowned ones with annual
39

enterprises and the nonstateowned ones with annual sales
39

and the nonstateowned ones with annual sales income
39

Xinhua Chinas economic efficiency indicators of the sector
39

the nonstateowned ones with annual sales income over
39

nonstateowned ones with annual sales income over million
39

up percent over the same period last year
35

13594 ms


测试结果2 m = 10 n = 5

Xinhua Following are the results
295

May Xinhua Following are the
209

Following are the results from
183

are the results from the
176

April Xinhua Following are the
141

Jan Xinhua Following are the
122

billion yuan billion US dollars
120

won drawn lost goals for
88

played won drawn lost goals
88

Dec Xinhua Following are the
87

12437 ms


以上源程序是采用的是最简单的方法,谁有更好,效率更高的方法,请跟贴!!



国内最棒的Google Android技术社区(eoeandroid),欢迎访问!

《银河系列原创教程》发布

《Java Web开发速学宝典》出版,欢迎定购

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics