MyChat Scripts: функция TStringList.LoadFromFile, загрузить текст в список из файла

MyChat Scripts: функция TStringList.LoadFromFile, загрузить текст в список из файла

"Для разработчиков", "Серверные скрипты", "Описание функций", "Строки", "TStringList", "Методы", "LoadFromFile".

 

Загрузить в список текстовый файл с диска.

 

Синтаксис

procedure TStringList.LoadFromFile(sFileName: string);

 

Параметры и возвращаемые значения

Параметр

Тип

Значение

sFileName

string

название текстового файла с полным путём.

 

Если загрузка прошла успешно (текстовый файл на месте, он не занят), то предыдущее содержимое списка будет очищено и загружено содержимое указанного текстового файла построчно.
 

Пример

Подсчёт количества уникальных слов в песне.

const
  LYRICS = 'C:\Text\Wicked Game.txt';
  REMOVE_SYMBOLS = '.,()';
  
function GetClearLine(sInput: string): string;
var
  s: string;
  j: integer;
begin
  s := LowerCase(Trim(sInput));

    if length(s) > 0 then
      for j := 1 to length(REMOVE_SYMBOLS) do
        s := ReplaceString(s, REMOVE_SYMBOLS[j], '', true, false);
    
  result := s;
end;

var
  SLData, SLWords: TStringList;
  sLine, sItem, sOut: string;
  i: integer;
begin
  SLData := TStringList.Create;
  SLData.LoadFromFile(LYRICS);
  
  SLWords := TStringList.Create;
  SLWords.Sorted := true;
  SLWords.Duplicates := dupIgnore;
  
    for i := 0 to SLData.Count - 1 do begin
      sLine := GetClearLine(SLData[i]);
      
        while length(sLine) > 0 do begin
          sItem := Fetch(sLine, ' ');
          SLWords.Append(sItem);
        end;
    end;
  
  mLogScript(CRLF + SLData.Text, 'Original text');
  mLogScript('Unique words count is: ' + IntToStr(SLWords.Count), '');
  
  sOut := '';
  
    if SLWords.Count > 0 then 
      for i := 0 to SLWords.Count - 1 do begin
        sOut := sOut + SLWords[i] + ' ';
      
          if (i + 1) mod 10 = 0 then begin
            mLogScript(sOut, '');
            sOut := '';
          end;  
      end;
  
    if length(sOut) > 0 then mLogScript(sOut, '');
    
  SLData.Free;
  SLWords.Free;
end.

Результат работы скрипта

[15:54:46] (Log "LoadFromFileMethod"): [Original text]

The world was on fire and no one could save me but you

It's strange what desire will make foolish people do

And I'd never dreamed that I'd knew somebody like you

And I'd never dreamed that I'd need somebody like you

 

No I don't wanna fall in love (This world is only gonna break your heart)

No I don't wanna fall in love (This world is only gonna break your heart)

With you

With you (This girl is only gonna break your heart)

 

What a wicked game you played to make me feel this way

What a wicked thing to do to let me dream of you

What a wicked thing to say you never felt this way

What a wicked thing to do to make me dream of you

 

And I don't wanna fall in love (This world is only gonna break your heart)

No I don't wanna fall in love (This world is only gonna break your heart)

With you

 

The world was on fire and no one could save me but you

It's strange what desire will make foolish people do

I'd never dreamed that I'd love somebody like you

I'd never dreamed that I'd lose somebody like you

 

No I don't wanna fall in love (This world is only gonna break your heart)

No I don't wanna fall in love (This world is only gonna break your heart)

With you (This girl is only gonna break your heart)

With you (This girl is only gonna break your heart)

 

No I... (This girl is only gonna break your heart)

(This girl is only gonna break your heart)

 

Nobody loves no one

[15:54:46] (Log "LoadFromFileMethod"): Unique words count is: 61

[15:54:46] (Log "LoadFromFileMethod"): a and break but could desire do don't dream dreamed

[15:54:46] (Log "LoadFromFileMethod"): fall feel felt fire foolish game girl gonna heart i

[15:54:46] (Log "LoadFromFileMethod"): i'd in is it's knew let like lose love loves

[15:54:46] (Log "LoadFromFileMethod"): make me need never no nobody of on one only

[15:54:46] (Log "LoadFromFileMethod"): people played save say somebody strange that the thing this

[15:54:46] (Log "LoadFromFileMethod"): to wanna was way what wicked will with world you

[15:54:46] (Log "LoadFromFileMethod"): your

[15:54:46] (Run "LoadFromFileMethod"): Время выполнения скрипта: 11 мс

[15:54:46] (Run "LoadFromFileMethod"): Скрипт выполнен успешно.
 

Ещё посмотреть

Fetch

IntToStr

Length

LowerCase

mLogScript

ReplaceString

Trim

TStringList.Append

TStringList.Create

TStringList.Count

TStringList.Duplicates

TStringList.Free

TStringList.Sorted