?
Solved

I need a function to split sentence

Posted on 2007-10-10
7
Medium Priority
?
1,381 Views
Last Modified: 2008-01-09
Hello guys

I have a function that split my sentence in words

procedure SplitString (const Delimiter: Char; Input: string; const Strings: TStrings) ;
begin
 Assert(Assigned(Strings));
 Strings.Clear;
 Strings.Delimiter := Delimiter;
 Strings.DelimitedText := Input;
end;

But I need something more clever, imagine a sentence like this:

Phr := "I am here working with my sister and she is working right now"

The procedure should work this way
procedure SplitSentence(Sentence: String, SplitNumber: Integer, const Strings: TStrings)

SplitNumber is the number words together, example:

SplitSentence(Phr, 2, List);

the sentence will be split in group of two words, something like this:

I am
here working
with my
sister and
she is
working right
now

As you can see, the last word is odd, then as it is the last, it can be without any group.

I hope you understand what I need.
I thank
Alexandre


0
Comment
Question by:hidrau
  • 3
  • 2
  • 2
7 Comments
 
LVL 18

Assisted Solution

by:Johnjces
Johnjces earned 400 total points
ID: 20052549
Try this. Quick and dirty!

procedure SplitStr(Text : String; NumWords : Integer; List: TStrings);
var
i, z : Integer;
s, last: String;
begin
 z := 0;
 if Text[length(text)]<>' ' then
 Text := Text+' ';
 for i := 1 to length(text) do
     if text[i] <> ' ' then
        s := s+text[i]
   else
    begin
     Last := '';
     last := s;
     s := s + ' ';
     z := z + 1;
     if z = NumWords then
     begin
      List.Add(s);
      s := '';
      z := 0;
     end;
    end;
 List.Add(Last);
end;

Test it...

procedure TForm1.Button1Click(Sender: TObject);
begin
 SplitStr('This is a test of this stuff', 3, ListBox1.Items);
end;

John
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20052560
Of course you can change the '3' to '2' or whatever'.

It has some issues in the last string depending upon the number of splits, but hopefully you get the idea.

John
0
 
LVL 7

Accepted Solution

by:
Cynna earned 1600 total points
ID: 20052799
And my 2c...

procedure SplitSentence(Sentence: String; SplitNumber: Integer; const Words: TStrings);
var i, j: Integer;
    Delimit: Char;
    w: String;
begin
 Assert(Assigned(Words)); Delimit := ' ';
 with TStringList.Create do try
      Clear; Delimiter := Delimit;
      DelimitedText := Sentence;
      Words.Clear;
      if Count<=SplitNumber then begin
         Words.Add(Sentence); Exit;
      end;
      i:=-1;
      repeat
        Inc(i); w:=Strings[i];
        if i<(Count-1) then begin
           for j:=2 to SplitNumber do begin
               Inc(i); w:=w+Delimit+Strings[i];
               if i>=(Count-1) then Break;
           end;
        end;
        Words.Add(w);
      until i>=(Count-1);
 finally Free; end;
end;

// --------------- DEMO ------------------

procedure TForm1.Button1Click(Sender: TObject);
begin
  SplitSentence('I am here working with my sister and she is working right now', 2, Memo1.Lines);
end;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:hidrau
ID: 20052806
If I put only one, the last word repeats, why?
0
 
LVL 1

Author Comment

by:hidrau
ID: 20052854
Cynna, it is perfect :))
very good

Johnjces, thanks for your support too , only the last word is being repeated. Take a look at it
0
 
LVL 1

Author Comment

by:hidrau
ID: 20052961
hello cynna

I remember you. You have made this function to me
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22859109.html

I am having problem on it, when I try to find two our three words
can you see it for me? if you want I can open another thread.
thanks
0
 
LVL 7

Expert Comment

by:Cynna
ID: 20053271
Hi hidrau from Brasil :-)

For the sake of completness, please go to original question (http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22859109.html)

And we'll continue there, OK?
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question