| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 903 人关注过本帖
标题:求助 遍历一段文本找单词并删掉出现频率最少的单词
只看楼主 加入收藏
wuyu123321
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-9-18
收藏
 问题点数:0 回复次数:3 
求助 遍历一段文本找单词并删掉出现频率最少的单词
遍历一段文本找单词并删掉出现频率最少的单词
搜索更多相关主题的帖子: 遍历 单词 频率 文本 删掉 
2007-10-08 17:51
user886633
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-6-16
收藏
得分:0 
你想用什么编程呢
2007-10-08 19:56
moximon
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-10-8
收藏
得分:0 
好像挺麻烦...因为是单词,要比较两空隔之间字符>_____<,组合起来有很多种,怎样能够存储和比较.......>_______<
2007-10-08 20:30
gamma
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-10-7
收藏
得分:0 

用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-08 23:29
快速回复:求助 遍历一段文本找单词并删掉出现频率最少的单词
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011614 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved