Solved

Count number of sentences in a memo

Posted on 2006-11-24
7
728 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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:ciuly
ciuly 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:ciuly
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

19 Experts available now in Live!

Get 1:1 Help Now