Solved

Count number of sentences in a memo

Posted on 2006-11-24
7
731 Views
Last Modified: 2008-03-17
Does anyone have some code to count the number sentences in a memo?
0
Comment
Question by:zattz
  • 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 25 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 75 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 25 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now