| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 227 人关注过本帖
标题:求助 遍历一段文本找单词并删掉出现频率最少的单词
收藏  订阅  推荐  打印 
wuyu123321
Rank: 1
等级:新手上路
帖子:8
积分:180
注册:2007-9-18
求助 遍历一段文本找单词并删掉出现频率最少的单词

遍历一段文本找单词并删掉出现频率最少的单词
搜索更多相关主题的帖子: 遍历  单词  频率  文本  删掉  
2007-10-8 17:51
user886633
Rank: 1
等级:新手上路
帖子:13
积分:230
注册:2007-6-16

你想用什么编程呢
2007-10-8 19:56
moximon
Rank: 1
等级:新手上路
帖子:13
积分:230
注册:2007-10-8

好像挺麻烦...因为是单词,要比较两空隔之间字符>_____<,组合起来有很多种,怎样能够存储和比较.......>_______<
2007-10-8 20:30
gamma
Rank: 1
等级:新手上路
帖子:14
积分:240
注册:2007-10-7

用delphi给你写了一段,你要用别的语言,自己改吧
第一次把自己写的代码拿出来献丑了,请各位多指教

function thisfun(s: string):string;
var
i: integer; // 用作遍历的计数器
tempword: string; // 存储临时单词
words: array of string; // 存储所有单词
counts: array of integer; // 存储所有单词频率
t: integer; // 用于上面两个数组的下标计数
al: integer; // 上面两个数组的长度
minindex: integer; // 频率最少的单词序号

// 这个函数用来在数组中查找
function arrayindex(str: string; arr: array of string):integer;
var
i: integer;
begin
result:= -1;
for i:= 0 to al-1 do
begin
if (arr[i] = str) then
begin
result:= i;
exit;
end;
end;
end;

begin

al:= 0;
tempword:= '';
for i:= 1 to length(s)+1 do
begin
if ((i = length(s)+1) or IsDelimiter(' ,.', s, i)) then
// 判断 s[i]是不是一个分隔符
// 或者已经到达句子结束
// IsDelimiter 这个函数很简单,delphi有标准的,也可以自己实现


// 如果单词结束,则把单词加入words, 或者把单词计数加一,否则把当前字母累加到临时单词,继续遍历
begin
if (tempword <> '') then
begin
t:= arrayindex(tempword, words);
if (t>=0) then
begin
inc(counts[t]);
end else
begin
setlength(words,al+1);
setlength(counts, al+1);
words[al]:= tempword;
counts[al]:= 1;
inc(al);
end;
tempword:= '';
end;
end else
begin
tempword:= tempword+s[i];
end;
end;
// 以上一次遍历结束,统计出了所有的单词和出现的频率

minindex:= 0;
for i:=1 to al-1 do
begin
if (counts[i]<counts[minindex]) then
begin
minindex:= i;
end;
end;
// 以上一次循环,找出哪个单词频率最低 (words[minindex])

//然后删除该单词,可以自己写,我偷懒了,用delphi的replace了
result:= stringreplace(s, words[minindex], '', [rfReplaceAll]);

end;

2007-10-8 23:29
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.055774 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved