Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2002.10.31;
Скачать: CL | DM;

Вниз

Вы не подскажете алгоритм поиска слова в тексте...   Найти похожие ветки 

 
Slavok ©   (2002-10-18 18:04) [0]

Дело в том, что мне надо найти все файлы содержащие данное слово. Через какой алгоритм осуществлять поиск, мне не приходит в голову, кроме как:
Я тут в поиске нашёл, как в RichEdit поиск осуществлять:

procedure TForm1.Button1Click(Sender: TObject);
begin
FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top);
FindDialog1.Execute;end;
procedure TForm1.FindDialog1Find(Sender: TObject);
var FoundAt: LongInt;
StartPos, ToEnd: Integer;
begin with RichEdit1 do
begin { begin the search after the current selection if there is one } { otherwise, begin at the start of the text }
if SelLength <> 0 then
StartPos := SelStart + SelLength;
else
StartPos := 0; { ToEnd is the length from StartPos to the end of the text in the rich edit control }
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
if FoundAt <> -1 then
begin
SetFocus;
SelStart := FoundAt;
SelLength := Length(FindDialog1.FindText);
end;
end;
end;

Так что можно загнать текст в RichEdit, а потом там осуществить поиск, но ведь размеры файла могут помешать...


 
Kibitzer ©   (2002-10-18 19:01) [1]

Тебе нужно найти на диске (или в папке) файл содержащий определённое слово, и для этого ты хочешь загружать каждый файл в RichEdit и его средствами искать? Мда, не лучший алгоритм...


 
VictorT ©   (2002-10-18 20:14) [2]

Просто читаешь посимвольно файл и сверяешь с искомым словом. Вот и весь алгоритм.


 
Dm9 ©   (2002-10-18 20:45) [3]

Из 2-х зол лучше уж загонять текст в memo и использовать его св-во text
Edit1.Text := IntToStr (Pos (YourStr, Memo1.Text));


 
алгоритм Бойера - Мура,   (2002-10-18 20:48) [4]

например.


 
Kotka ©   (2002-10-18 22:52) [5]

Вот пример моей программки:

unit NewFinder1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Masks, ShellApi, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
List1: TListBox;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;

procedure SearchInDir(Mask, Dir: string; Subdir: Boolean; var List: tStringList);
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
words:array of string;
exist:array of boolean;
flag:boolean;
fl:textfile; kol:integer;
tmpdir:string;
implementation

{$R *.dfm}
procedure tform1.SearchInDir(Mask, Dir: string; Subdir: Boolean; var List: tStringList);
var
r,i: integer;
f: TSearchRec;s:string;
begin
if Dir = "" then Exit;
if Dir[Length(Dir)] <> "\" then Dir := Dir + "\";
{$I-}
ChDir(Dir);
{$I+}
i:= IOResult;
if i <> 0 then Exit;
r := FindFirst("*.*", faAnyFile, f);
while r = 0 do
begin

if MatchesMask(f.Name, Mask) then
if (f.Name <> ".") and (f.Name <> "..") then
begin
for i:=0 to kol do
exist[i]:=false;
if fileexists(ExpandFileName(f.Name)) then
begin
assignfile(fl,ExpandFileName(f.Name));
reset(fl);
while not eof(fl) do
begin
label3.Caption :=ExpandFileName(f.Name);
application.ProcessMessages;
readln(fl,s);
s:=ansilowercase(s);
for i:=0 to kol do
if pos(words[i],s)<>0 then
exist[i]:=true;
end;
flag:=true;
for i:=0 to kol do
flag:=(flag)and(exist[i]);
if flag then
List.Add(ExpandFileName(f.Name));
closefile(fl);
end;
end;
if (f.Attr and faDirectory) = faDirectory then
if SubDir = True then
begin
if (f.Name <> ".") and (f.Name <> "..") then
begin
SearchInDir(Mask, ExpandFileName(f.Name), SubDir, List);
ChDir(Dir);
end;
end;
r := FindNext(f);
end;
FindClose(f);
end;

procedure TForm1.Button1Click(Sender: TObject);
var list:tstringlist;
i,x,tmp,l:integer;
s,s1,s2:string;
path:array[0..254]of char;
begin
s:=edit2.Text ;x:=0;
list:=tstringlist.Create;
for i:=1 to length(s) do
if s[i]=" " then
x:=x+1;
x:=x+1;
setlength(words,x);
setlength(exist,x);kol:=x-1;x:=0;tmp:=1;
s:=s+" ";
for i:=1 to length(s) do
if (s[i]=" ") then
begin
words[x]:=ansilowercase(copy(s,tmp,i-tmp));
if words[x]<>"" then
x:=x+1;
tmp:=i+1;
end;
s:=edit1.Text ;
for i:=1 to length(s) do
if s[i]="\" then
x:=i;
s1:=copy(s,x+1,length(s)-x);
s2:=copy(s,1,x);
searchindir(s1,s2,true,list);
list1.items :=list;
label3.Caption :="Готово";
end;

end.

В Edit2 содержутся слова, а в Edit1 - путь вида C:\Windows\*.txt



Страницы: 1 вся ветка

Текущий архив: 2002.10.31;
Скачать: CL | DM;

Наверх




Память: 0.49 MB
Время: 0.012 c
7-101553
Andrew_R
2002-08-19 22:05
2002.10.31
Com port и таймер


4-101599
boa
2002-09-18 09:07
2002.10.31
Сообщение WM_GETFONT


1-101273
HostGuy
2002-10-22 14:43
2002.10.31
Помогите новичку найти литературу по Дельфину 6 в Internet-е


7-101548
vvant
2002-08-28 16:02
2002.10.31
Как определить какое приложение в данный момент активно


8-101394
plimut
2002-07-05 14:49
2002.10.31
Delphi виснет при запуске приложения (RUN)