怎样用delphi实现读取一英文的txt文件,并统计出各个单词出现的频率,请高手们简单给写一点源码!

解决方案 »

  1.   

    这是读取txt文件的代码procedure TForm1.Button1Click(Sender: TObject);
    var
      InputFile:textfile;
      s:   string;
    begin
      AssignFile(InputFile,'d:\tt.txt');
      reset(InputFile);
      while   not   eof(InputFile)   do
      begin
        readln(InputFile,s);
        memo1.Lines.Add(s);
      end;
      closeFile(inputFile);
    end;
      

  2.   

    用stringList读文本
    分词都比较方便
      

  3.   

    用HashStringList
     其中每项 : 个数 - 》A
                个数 - 》B
        .....................
               个数-》z只要分析“个数”,就知道各个字符出现的频率了
      

  4.   

    高手们,是单词不是字母。是统计单词的出现频率!!!例如:boy 在文章中的出现频率。
      

  5.   

    写了个例子, 楼主根据自己情况再改吧:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      AWord  : TStringList;
      ANum   : TStringList;
      AFile  : TStringList;
      i, j, k: Integer;
      s, t   : String;
    begin
      AWord := TStringList.Create;
      ANum := TStringList.Create;
      try
        AFile := TStringList.Create;
        try
          AFile.LoadFromFile('c:\1.txt');
          for i := 0 to AFile.Count - 1 do
          begin
            s := LowerCase(AFile.Strings[i]) + #13;
            t := '';
            for j := 1 to Length(s) do
            begin
              if (s[j] >= 'a') and (s[j] <= 'z') then
                t := t + s[j]
              else begin
                if t <> '' then
                begin
                  k := AWord.IndexOf(t);
                  if k >= 0 then
                    ANum.Strings[k] := IntToStr(StrToIntDef(ANum.Strings[k], 0) + 1)
                  else begin
                    AWord.Add(t);
                    ANum.Add('1');
                  end;
                end;
                t := '';
              end;
            end;
          end;
        finally
          AFile.Free;
        end;
        for i := 0 to AWord.Count - 1 do
          Memo1.Lines.Add('单词 ' + AWord.Strings[i] + ' 出现 ' + ANum.Strings[i] + ' 次');
      finally
        AWord.Free;
        ANum.Free;
      end;
    end;
      

  6.   

    用正则或许可以吧.先第一遍用正则可以根据execnext来取出所有的单词,存到TStringList里,然后再循环用每个单词
    去找出现的次数.