?
Solved

Count number of sentences in a memo

Posted on 2006-11-24
7
Medium Priority
?
750 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
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.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 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