1

作業中の文字列リストが 2 つあります。キーワードのリストを持つものと、除外キーワードのリストを持つものです。リストを検索して、除外キーワードを含まないリスト アイテムを選択し、3 番目のキーワード リストに出力できるようにしたいと考えています。私は AnsiPos 関数を使用していましたが、完全な単語ではなく、単語の一部である場合は除外キーワードが見つかりました。

これを行うための比較的簡単な方法に関する提案はありますか? 速度はそれほど重要ではありませんが、いいでしょう。

私がやろうとしていることの例:

キーワード リスト:

ネコ
ナマズ
フィッシュスティック
ドッグフード

除外キーワード リスト:

返される値:

ネコ
ナマズ
ドッグフード

これは私がこれまでに得たものです..これは機能しません。私は以下からの情報を使用しました: Is There An Efficient Whole Word Search Function in Delphi?

function ExistWordInString(aString: PAnsichar; aSearchString: string;
  aSearchOptions: TStringSearchOptions): Boolean;
var
  Size : Integer;
begin
  Size := StrLen(aString);
  result := SearchBuf(aString, Size, 0, 0, aSearchString, aSearchOptions) <> nil;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  i, j, index: integer;
  s: string;
  stl: tstringlist;
begin
  stl := TStringList.Create;
  stl.Text := listbox1.Items.Text;
  for I := 0 to stl.Count - 1 do
  begin
    for j := 0 to listbox2.Count - 1 do
    begin
      if not ExistWordInString(PAnsiChar(listbox2.Items.Strings[j]),
        listbox1.Items.Strings[i], [soWholeWord, soDown])
      then
        listbox3.Items.Append(stl.Strings[i]);
    end;
  end;
end;
4

5 に答える 5

2

心配する必要がある単語区切り文字がスペースだけの場合は、AnsiPos を使用して、キーワードと除外キーワードの両方の前後にスペースを追加することにより、単語全体の一致を行うことができます。つまり、

AnsiPos(' '+SubStr+' ', ' '+Str+' ')

除外キーワード リストのすべてのエントリをチェックするには、ループが必要です。

于 2010-01-11T02:41:30.450 に答える
1

A..Z / Unicode以外の文字に興味がない場合は、SearchBuf関数を使用できます(pastacoolの回答を参照) 。

Unicode Delphi (D2009 または D2010) を使用している場合は、TCharacter.IsLetterOrDigit(aString: string; aIndex: integer): boolean;を使用する必要があります。キャラクターユニットから。アイデアを得るための簡単な例:

procedure TForm7.btn1Click(Sender: TObject);
var
  bMatches: boolean;

begin
  with rgx1 do //custom component - disregard it
  begin
    RegEx:=edtTextToFind.Text; //text to find
    Subject:=mmoResult.Text; //text in which to search
    if Match then //aha! found it!
    begin
      bMatches:=True;
      if chkWholeWord.Checked then //be attentive from here!! - I think that's self explaining...
      begin
        if MatchedExpressionOffset>1 then
          bMatches:=not TCharacter.IsLetterOrDigit(Subject, MatchedExpressionOffset-1);
        if bMatches and (MatchedExpressionOffset+MatchedExpressionLength<=Length(Subject)) then
          bMatches:=not TCharacter.IsLetterOrDigit(Subject, MatchedExpressionOffset+MatchedExpressionLength);
      end;
      if bMatches then //select it in the memo
      begin
        mmoResult.SelStart:=MatchedExpressionOffset-1;
        mmoResult.SelLength:=MatchedExpressionLength;
        mmoResult.SetFocus;
      end
      else
        ShowMessage('Text not found!');
    end
    else
      ShowMessage('Text not found!');
  end;
end;
于 2010-01-11T17:27:04.337 に答える
1

関数を次のように変更します。

function ExistWordInString(aString:PAnsichar;
   aSearchString:string;
   aSearchOptions: TStringSearchOptions): Boolean;
var 
  b : boolean;
begin
  if soWholeWord in aSearchOptions then
    b := Pos(' '+Uppercase(aSearchString)+' ',' '+UpperCase(aString)+' ') > 0;
  else
    b := Pos(UpperCase(aSearchString),UpperCase(aString)) > 0;
  Result := b;
end;

Delphi 2009/2010 を使用している場合は、 からPosに変更しAnsiPosます。ここでの私の仮定は、soWholeWord は、"Fish" という一致は "Fish Sticks" には一致するが、"catfish" には一致しないことを意味するということです。

于 2010-01-11T17:27:15.147 に答える
1

私はそれを理解したと思います。stringlist.find('fish',index); を使用します。

私はそれを理解していませんでした。.find が機能しませんでした。

-ブラッド

于 2010-01-11T01:17:18.947 に答える
1

このサンプル コードは魅力的に機能します (Delphi 7 を使用):

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, StrUtils;

type
  TForm1 = class(TForm)
  Button1: TButton;
  ListBox1: TListBox;
  ListBox2: TListBox;
  ListBox3: TListBox;
  procedure Button1Click(Sender: TObject);

  private
     function ExistWordInString(aString, aSearchString:string;aSearchOptions: TStringSearchOptions): Boolean;

  public
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
    i,k: integer;

begin

    for k:= 0 to ListBox2.Count -1 do
        for i:= 0 to ListBox1.Count - 1 do
        begin
            if not ExistWordInString(ListBox1.Items[i], ListBox2.Items[k],[soWholeWord,soDown]) then
                ListBox3.Items.Append(ListBox1.Items[i]);
        end;

end;

function TForm1.ExistWordInString(aString, aSearchString: string; aSearchOptions: TStringSearchOptions): Boolean;
var
  Size : Integer;

begin
        Size:=Length(aString);
        Result := SearchBuf(PChar(aString), Size, 0, 0, aSearchString, aSearchOptions)<>nil;

end;
end.    

フォームは次のとおりです。

object Form1: TForm1
  Left = 1008
  Top = 398
  Width = 411
  Height = 294
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 320
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object ListBox1: TListBox
    Left = 8
    Top = 8
    Width = 177
    Height = 97
    ItemHeight = 13
    Items.Strings = (
      'Cat '
      'Catfish'
      'Fish Sticks'
      'Dog Food')
    TabOrder = 1
  end
  object ListBox2: TListBox
    Left = 192
    Top = 8
    Width = 121
    Height = 97
    ItemHeight = 13
    Items.Strings = (
      'Fish')
    TabOrder = 2
  end
  object ListBox3: TListBox
    Left = 8
    Top = 112
    Width = 305
    Height = 137
    ItemHeight = 13
    TabOrder = 3
  end
end

お役に立てれば。

ラインハルト:-)

于 2010-01-11T11:09:13.283 に答える