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
Solved

Count number of sentences in a memo

Posted on 2006-11-24
7
742 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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