[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Count number of sentences in a memo

Posted on 2006-11-24
7
Medium Priority
?
753 Views
Last Modified: 2008-03-17
Does anyone have some code to count the number sentences in a memo?
0
Comment
Question by:zattz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 18009053
you can find that in memo.lines.count
0
 
LVL 9

Assisted Solution

by:alkisg
alkisg earned 100 total points
ID: 18009275
If a sentence is defined as "ending with a dot", then you'll have to count the dots, or do something like
memo1.lines.linebreak := '.';
count := memo1.lines.count;
0
 
LVL 28

Accepted Solution

by:
TName earned 300 total points
ID: 18009416
I think counting the dots, as alkisg suggested, is a good idea ;)
e.g.:

var
  i,j,c:Integer;
begin
 c:=0;
 for i:=0 to Memo1.Lines.Count-1 do
   for j:=1 to Length(Memo1.Lines[i]) do
     if  Memo1.Lines[i][j]='.' then
        c:=c+1;
 ShowMessage(IntToStr(c));
end;

@ alkisg
BTW, I don't seem to find "linebreak" among the properties of my D7 TStringList (or TStrings). Is it a later D version you are referring to, or am I missing something?
Thanks!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 28

Expert Comment

by:TName
ID: 18009427
I mean of course TMemo.Lines property (TStrings anyway).
0
 
LVL 9

Expert Comment

by:alkisg
ID: 18010488
I looked it up on D2006 *help*, it isn't there. I'm sure it exists since D2005, I'm not sure about previous versions. If you declare s: TStrings and s.LineBreak := '.' generates an undeclared undentifier error, then it's not there :-)
0
 
LVL 28

Assisted Solution

by:2266180
2266180 earned 100 total points
ID: 18011004
made a more generic demo below. can be customized according to what a sentence is and what a word is (see the constant set of chars defined)

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Contnrs;

const WhiteSpaces=[' ',#9{tab},#13,#10];

      punctuation=['.',',','!','?'];

      other=['(',')','+','=','<','>','/','\','{','}','[',']'];
      // I put anything that is not whitspace but is can be a word delimiter here.

      AlphaNumeric=['a'..'z','A'..'Z','0'..'9'];

type EEmptySentenceException=class(Exception)
     end;

     TSentence=class
     private
       FWords:TStringList;
       function GetCount: integer;
       function GetWord(index: integer): string;
       procedure SetWord(index: integer; const Value: string);
     public
       constructor Create; overload;
       constructor Create(s:string); overload;
       destructor Destroy; override;

       class function isWordDelimiter(c:char):boolean;

       property Count:integer read GetCount;
       property Words[index:integer]:string read GetWord write SetWord;
     end;

     TSentences=class
     private
       FSentences:TObjectList;
       function GetCount: integer;
       function GetSentence(index: integer): TSentence;
     public
       constructor Create; overload;
       constructor Create(s:string); overload;
       destructor Destroy; override;

       class function isSentenceDelimiter(c:char):boolean;

       property Count:integer read GetCount;
       property Sentences[index:integer]:TSentence read GetSentence;
     end;

{ TSentence }

constructor TSentence.Create(s: string);
var i,j:integer; w:string;
begin
  Create;
  i:=1;
  while i<=length(s) do
  begin
    setlength(w,length(s));// maximum possible
    j:=1;
    while (i<=length(s)) and (not isWordDelimiter(s[i])) do
    begin
      w[j]:=s[i];
      inc(i);
      inc(j);
    end;
    dec(j);
    if j>0 then
    begin
      setlength(w,j);
      FWords.Add(w);
    end;
    while (i<=length(s)) and (isWordDelimiter(s[i])) do
      inc(i);
  end;
  if Count=0 then
    raise EEmptySentenceException.Create('The sentence is empty: contains no words.');
end;

constructor TSentence.Create;
begin
  FWords:=TStringList.Create;
end;

destructor TSentence.Destroy;
begin
  FWords.Free;
  inherited;
end;

function TSentence.GetCount: integer;
begin
  result:=FWords.Count;
end;

function TSentence.GetWord(index: integer): string;
begin
  result:=FWords[index];
end;

class function TSentence.isWordDelimiter(c: char): boolean;
begin
  result:=(c in whitespaces) or (c in punctuation) or (c in other);
  // another definition of a word might be: any set oc alphanumerical characters
  // so it all depends on the definition.
end;

procedure TSentence.SetWord(index: integer; const Value: string);
begin
  FWords[index]:=value;
end;

{ TSentences }

constructor TSentences.Create(s: string);
var i,j:integer; t:string;
begin
  Create;
  i:=1;
  while i<=length(s) do
  begin
    setlength(t,length(s));
    j:=1;
    while (i<=length(s)) and (not isSentenceDelimiter(s[i])) do
    begin
      t[j]:=s[i];
      inc(i);
      inc(j);
    end;
    dec(j);
    if j>0 then
    begin
      setlength(t,j);
      try
        FSentences.Add(TSentence.Create(t))
      except
        on e:EEmptySentenceException do ;// nothing
        on e:Exception do
          raise e;// whatever
      end;
    end;
    while (i<=length(s)) and (isSentenceDelimiter(s[i])) do
      inc(i);
  end;
end;

constructor TSentences.Create;
begin
  FSentences:=TObjectList.Create(true);
end;

destructor TSentences.Destroy;
begin
  FSentences.Free;
  inherited;
end;

function TSentences.GetCount: integer;
begin
  result:=FSentences.Count;
end;

function TSentences.GetSentence(index: integer): TSentence;
begin
  result:=TSentence(FSentences[index]);
end;

class function TSentences.isSentenceDelimiter(c: char): boolean;
begin
  result:=c in punctuation;
end;

procedure TForm1.FormCreate(Sender: TObject);
var s:tsentences;
begin
  memo1.lines.clear;
  memo1.lines.add('word word s. next. making 3 on a line.');
  memo1.lines.add('now splitting');
  memo1.lines.add('one sentence on 2 lines. adding some more.');
  memo1.lines.add('now we have exactly 6 sentences on 4 lines.');
  s:=TSentences.create(memo1.lines.text);

  showmessage(inttostr(s.count));
  s.free;
end;

end.
0
 
LVL 28

Expert Comment

by:2266180
ID: 18011045
I updated a little the above code for another question of the same family. just to keep a nice line of the code, in this Q is the updated one http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_22071897.html :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 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