How to use this Hyphenation library in delphi? - delphi

This is a hyphenation lib by Synopse delphi open source.
The demo is a console application. I do not know how to use it in GUI application.
Below is my test, but not work. It does not display word with hyphen (or separaror). The lib can be downloaded here:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, hyphen, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure testhyphenator;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.testhyphenator;
var
h: THyphen;
s: string;
F, L: Integer;
begin
s := 'hyph_en_US.txt'; //this is from the folder, is that correct to call?
if FileExists(s) then
begin
F := FileOpen(s, fmOpenRead);
L := FileSeek(F, 0, soFromEnd);
if L > 0 then
begin
SetLength(s, L);
FileSeek(F, 0, soFromBeginning);
FileRead(F, s[1], L);
end;
FileClose(F);
end;
h := THyphen.Create(s);
h.Execute('pronunciation'); //is this correct?
ShowMessage(h.filllist); //not display hyphenated word
end;
It does not display hyphenated word. In the demo, I am also confused about the constructor:
H := THyphen.create('ISO8859-1'#10'f1f'#10'if3fa/ff=f,2,2'#10'tenerif5fa');
writeln('"',H.Execute('SchiffahrT'),'"'); writeln(H.FillList);
...
The author has also enclosed the obj file. If I want to compile it into a single exe, how to do it?
Can you please help me understand how to use it correctly?
Thanks a lot.

Disclaimer: I have harnessed a not so recent distribution of Hyphen, it may not be in sync with the latest version.
Here are my points:
Compilation of the distribution
I have compiled it under Delphi 7 and it's OK.
hyphen.rc File
There is no hyph_en_EN.dic file in the distribution. If you are going to rebuild hyphen.res, you may need to fix hyphen.rc using the following:
hyphen Text HYPH_EN_US.dic
I have not checked the hyphen.res file in the distribution wether it contains hyph_en_EN.dic and/or hyph_en_US.dic.
*.dic Files available in my distribution
hyph_it_IT.dic
hyph_es_ES.dic
hyph_fr_FR.dic
hyp_en_US.dic
hyp_de_DE.dic
Answers to the comments in your snippet
s := 'hyph_en_US.txt'; //this is from the folder, is that correct to call?
No! The correct file extension is .dic. You should write instead:
s := 'hyph_en_US.dic;
The following is Ok (you can refer to the definition of THyphen class):
Execute('pronunciation'); // is this correct?
The following is Ok (but it doesn't work because h as a THyphen instance was not properly initialized):
ShowMessage(h.filllist); //not display hyphenated word
Your concern about the constructor
H := THyphen.create('ISO8859-1'#10'f1f'#10'if3fa/ff=f,2,2'#10'tenerif5fa');
It's just one of the proper way to set up THyphen (refer again to the definition of THyphen class among others).
E.g.:
H := THyphen.create('EN');
Harnessing hyphen in a GUI App using Delphi 2007
I can tell that it's OK so long as THyphen instance is properly constructed (Dont forget to include the hyphen.res resource file with {$R hyphen.res}, the hyphen.obj file is already linked in the hyphen.pas unit).
Last but not the least
Feel free to get in touch with Arnaud Bouchez the great man behind Synopse. He is a Stackoverflow member and always ready to help for sure, a top delphi user moreover.

I don't have my Delphi install handy, so understand you may need to tweak this a bit.
After looking at the hyphen code, I believe you are using it incorrectly. The parameter on the constructor is the language or character set.
h := THyphen.Create('UTF-8');
or (based on your file name, I think you need this next one)
h := THyphen.Create('EN');
Then "Execute" is used to generate a hyphenated version of the string passed in. "Execute" is a function that returns a new string. You are calling it, but not doing anything with the result.
NewStr := h.Execute('correct');
"NewStr" should now equal "cor-rect".
If I read the code correctly, the "FillList" function and procedure return a list of all of the possible hyphenation possibilities for the last word that was Execute'd.

Related

Programmatically get all units used in a dpr in Delphi

I am new in Delphi and I am trying to make an application in which I will give as an input a .dpr file and the application will create a list with all the .pas files used by this .dpr... I still cannot find a function in Delphi or a way to read the uses of the .dpr in order to navigate through the file system to these pas files and read their uses, and so on... Does anyone has any idea on how to achieve this?
It's not exactly straightforward: You don't just need to read the .dpr file, but you also need to parse the .dproj and registry to get Search Paths. If you're trying to do this right, you also have to parse the .dpr and .pas files as code files so you can find the uses statements, handle {$I '...'} includes, {$IFDEF} blocks, interface vs implementation sections, and so on.
All that said, you might want to look to the open source CnPack and GExperts projects for inspiration. Both of them have solved this problem, and you may be able to leverage their work towards whatever problem you're trying to solve.
If you let Delphi create a .map file (linker option), it will contain a list of all source and dcu files used in that project. GExperts does that, using a simple parser for a map file which is taken from my dzlib https://sourceforge.net/p/dzlib/code/HEAD/tree/dzlib/trunk/src/u_dzMapFileReader.pas
I would like to update this question for possible answer for future reference.
Create a separate unit file (PhonyObject.pas)
unit PhonyObject;
interface
uses
System.Classes, FMX.Forms, FMX.Dialogs;
type
TPhonyObject = class(TObject)
end;
TPhonyClass = class of TPhonyObject;
procedure FindUnitName(anObject: TObject);
var
PhonyName: string;
PhonyClass: TPhonyClass;
PhonyInstance: TObject;
PhonyClassName: procedure(anObject: TObject) = FindUnitName; //function: String = FindUnitName;
implementation
uses System.TypInfo;
procedure FindUnitName(anObject: TObject);
begin
if anObject <> nil then PhonyName := anObject.UnitName
else if not (TObject.UnitName <> 'System') then
begin
if TypInfo.GetTypeData(anObject.ClassInfo) <> nil then PhonyName := String(GetTypeData(anObject.ClassInfo)^.UnitName);
end else PhonyName := TObject.UnitName;
//FreeAndNilProperties
end;
initialization
PhonyClass := TPhonyObject;
PhonyInstance := PhonyClass.Create;
ShowMessage('Unit Name =' + PhonyInstance.UnitName);
PhonyInstance.Free;
finalization
PhonyClass := nil; //PhonyClass.Free;
end.
And in order to use this inside another (multiple) units, this is the code I have used so far, but I hope to update it later on. I have this showing up inside a hand made "console" with black background and white text in a TMemo. If anyone wants the code for the TMemo (its not commonly known), or how to show all these inside basically a debug window, all you just have to do let me know. This is the best I have gotten it so far, but I need a better understanding of the child/parent object/classes
unit AnotherUnit;
interface
uses
System.Classes, PhonyObject;
type
TPhonyObj = class(TPhonyObject)
end;
//var
implementation
{$R *.fmx}
uses ...;
initialization
PhonyClass := TPhonyObj;
PhonyInstance := PhonyClass.Create;
ShowMessage('UnitName= ' + PhonyInstance.UnitName + ' (AnotherUnit)'); // PhonyClass.UnitName // PhonyClassName(PhonyInstance);
PhonyInstance.Free;
finalization
PhonyClass := nil;
end;
I used as unique of Unit Names and class names, as I could and I realize I don't actually use any objects till the end, none the less it should work with out any problems. Please comment if there are some better ideas, but I think this is a powerful feature for Delphi programming when you can predict when certain unit names are going to suddenly show up. And how to predict for them too.

Querying Active Directory (AD) without linking in "ActiveDs_TLB.pas"

I want to query Active Directory in an app developed with Delphi (7 and up), but do not want to include "ActiveDs_TLB" in the "uses" clause to keep the EXE size down. When querying WMI it is possible to use the IBindCtx and IMoniker interfaces to avoid linking in the type library (see How do I use WMI with Delphi without drastically increasing the application's file size? for a solution).
Is it possible to do the same when performing AD queries? I my case I want to retrieve "IADsUser" and "IADsComputer". I am aware that I can decrease the EXE size by manually copying only the required definitions from "ActiveDs_TLB" into my program or to use an LDAP query, but I would prefer a solution similar to the one described for WMI.
I'm no Active Directory expert, but I just created two D7 console applications, one accessing the WnNTSystemInfo object using the ActiveDS_TLB.Pas type library and the other using late binding do do the same thing, namely get the ComputerName from AD.
First, the late binding one:
program ActiveDSLBConsole;
{$APPTYPE CONSOLE}
uses
SysUtils, ActiveX, ComObj;
var
SI : OleVariant;
S : String;
begin
CoInitialize(Nil);
SI := CreateOleObject('WinNTSystemInfo');
S := SI.ComputerName;
writeln(S);
readln;
end.
(what took me longest writing the above was checking the registry for the name of
the object to create)
Anyway, I hope that shows that, yes, you can query AD via late binding and that this minimal example will get you started querying AD that way.
The equivalent AD console application using ActiveDS_Tlb is
program ActiveDSConsole;
{$APPTYPE CONSOLE}
uses
SysUtils, ActiveX, ActiveDS_Tlb;
var
SI : IADsWinNTSystemInfo;
S : String;
begin
CoInitialize(Nil);
SI := CoWinNTSystemInfo.Create;
S := SI.ComputerName;
writeln(S);
readln;
end.
These have .Exe sizes of
ActiveDSConsole : 390144 bytes
ActiveDSLBConsole : 87552 bytes (late bound)
So there's evidently quite a bit of code pulled in to support the use
of the tlb objects, but neither is huge.
FWIW, the above re-written as Button1Click handlers of a minimalist VCL app gives Exe sizes
of
using ActiveDS_TLB : 396288 bytes
late bound : 392704 bytes
the difference between these two seems fairly marginal to me, but there's a clear
size advantage to late binding in a minimal D7 console application. Your mileage may vary,
so probably best to "suck it and see", if you'll pardon the mixed metaphors.
Btw, late binding has the advantage that you don't always have to supply arguments for each of the parameters in an interface method. And you can call a method with this special syntax that the compiler was enhanced to allow (when automation support was added, in D2) for variants that it knows contain late-bound automation objects:
(from an MS Word late binding example)
Table := MSWord.ActiveDocument.Tables.Add(Range:= MSWord.Selection.Range, NumRows:= Rows, NumColumns:= Columns, DefaultTableBehavior:= wdWord9TableBehavior, AutoFitBehavior:= wdAutoFitFixed);
Martyn's answer filled in the missing pieces. Here's an example on how to query IADsUser using late binding:
program GetUserObjectPath;
{$APPTYPE CONSOLE}
uses SysUtils, ActiveX, ComObj;
function GetObject (const Name: WideString) : IDispatch;
var
Moniker : IMoniker;
Eaten : Integer;
BindContext : IBindCtx;
begin
OleCheck (CreateBindCtx (0, BindContext));
OleCheck (MkParseDisplayName (BindContext, PWideChar (Name), Eaten,
Moniker));
OleCheck (Moniker.BindToObject (BindContext, NIL, IDispatch, Result));
end; { GetObject }
procedure Query_AD (const sQuery: String);
var
vUser : OleVariant;
begin
vUser := GetObject (sQuery); // = IADsUser
WriteLn ('Name = ' + vUser.FullName);
end; { Query_AD }
var
sQuery, sDomain, sUserName : String;
begin
sDomain := GetEnvironmentVariable ('USERDNSDOMAIN');
sUserName := GetEnvironmentVariable ('USERNAME');
sQuery := Format ('WinNT://%s/%s,user', [sDomain, sUserName]);
CoInitialize (NIL);
try
Query_AD (sQuery);
finally
// Causes Access Violation if AD query does not happen in subroutine
CoUninitialize;
end; { try / finally }
WriteLn;
Write ('Press [Enter] to continue ...');
ReadLn;
end.
The actual AD query should happen in a subroutine (here "Query_AD"), otherwise calling "CoUninitialize" is going to lead to an access violation (see Why does CoUninitialize cause an error on exit? for an explanation).

Directory path manipulation in Delphi?

I have the full path name of a given folder for e.g.
c:\foo\bar
Now I would like to reference a file inside c:\foo named baz.txt,
c:\foo\bar\..\baz.txt
I am currently using the .. path operator to go down one level and get the file that I need.
Is there a function that can do path manipulations, for e.g. UpOneLevel(str) -> str ? I know I can write one by splitting the string and removing the last token, but I would rather it be a built-in / library function so I don't get into trouble later if there are for e.g. escaped backslashes.
Use the ExpandFileName function:
var
S: string;
begin
S := 'c:\foo\bar\..';
S := ExpandFileName(S);
ShowMessage(S);
end;
The message from the above example will show the c:\foo path.
Look at ExtractFilePath() and ExtractFileDir(). These are available in just about all Delphi versions, particularly those that do not have TDirectory, IOUtils, etc.
And before anyone says it, these work just fine whether the path ends with a filename or not. ForceDirectories() uses them internally to walk backwards through a hierarchy of parent folders, for example.
This answer is valid for Delphi XE +
Use the TDirectory class of the IOutils unit, which have the method GetParent, like this::
uses IOUtils;
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
s := 'c:\foo\bar';
ShowMessage(TDirectory.GetParent(s));
end;
In older versions
Look at the other answers.
You can take a look at TPathBuilder record in SvClasses unit from delphi-oop library. This unit does not support Delphi 2007 but TPathBuilder implementation is compatible with this Delphi version. Example usage:
var
LFullPath: string;
begin
LFullPath := TPathBuilder.InitCustomPath('c:\foo\bar').GoUpFolder.AddFile('baz.txt').ToString;
//LFullPath = c:\foo\baz.txt

Ini Files - Read Multi Lines?

I am aware that Ini Files are meant for single lines of information, needless to say I am trying to read/write multi lines to and from the Ini - without much success (I always seem to do things the hard way!)
Lets say my Ini File when saved, looks like this:
[richardmarx]
Filenames=hazard
children of the night
right here waiting
Suppose the Ini File is built dynamically (ie, the richardmarx and the Filenames are not know, but unique - they could literally be anything).
How would I be able to read the Ini File?
In this example then, how could I put richardmarx into a TEdit, and the Filenames associated with richardmarx section into a memo?
Many thanks in advance.
Do not store multi-line strings into an INI file to begin with. Escape the line breaks, like #RobertFrank suggested. I would not use an asterik for that, though, as that is a valid text character. I would use something like this instead:
[richardmarx]
Filenames=hazard%nchildren of the night%nright here waiting
You can then read the string and replace the %n sequences with the value of the sLineBreak global variable. If you needed to store an actual % character, escape it as %%, eg:
[sales]
value=Sale! 50%% off%nat Macy's
You're not using a valid .ini format, so it's not going to be easy. It's much easier if you use a properly formed .ini file.
A valid ini file is of the format
[section]
akey=value
bkey=value
ckey=value
Here's a sample of reading multiple lines from an ini file. While it uses a TListBox instead of a TEdit, it should be enough to get you started.
The code below will work with an improperly formatted file as well, but you'll probably have to change the code in the ListBox1Click event to use ReadSectionValues instead and do some manual parsing for each item before displaying them; in that case, create another TStringList in the event handler and pass it instead of Memo1.Lines.
With a properly formatted ini file, you can use TIniFile.ReadSection or TMemIniFile.ReadSections to load all of the sections into a TStrings descendant, and then use ReadSection(SectionName) to get each section's values.
Here's an example - save this ini file somewhere (I've used d:\temp\sample.ini:
[A Section]
Item1=Item A1
Item2=Item A2
Item3=Item A3
Item4=Item A4
[B Section]
Item1=Item B1
Item2=Item B2
Item3=Item B3
Item4=Item B4
[C Section]
Item1=Item C1
Item2=Item C2
Item3=Item C3
Item4=Item C4
Here's a sample of the form's code:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IniFiles;
type
TForm2 = class(TForm)
ListBox1: TListBox;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
FIni: TMemIniFile;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
const
IniName = 'd:\Temp\Sample.ini';
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FIni.Free;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
FIni := TMemIniFile.Create(IniName);
Memo1.Lines.Clear;
FIni.ReadSections(ListBox1.Items);
end;
procedure TForm2.ListBox1Click(Sender: TObject);
var
Section: string;
begin
if ListBox1.ItemIndex > -1 then
begin
Section := ListBox1.Items[ListBox1.ItemIndex];
FIni.ReadSection(Section, Memo1.Lines);
end;
end;
end.
Clicking on each section name in the ListBox displays the items that are in that section, as seen below:
EDIT: OK. I got curious to see how it would work with the ini file content you posted in your question.
So I made the following change:
Copied and pasted your sample ini content verbatim as a new section to the end of the Sample.ini I created above.
Ran the code, and clicked the new richardmarx item. Here's what I got:
Obviously, that wouldn't work. So I made the following additional changes:
Changed the ListBox1Click event to use FIni.ReadSectionValues instead of ReadSection.
Ran the modified application, and clicked on the C Section item to see how it displayed, and then the new richardmarx item to see how it displayed. The results are as follows:
Pre-process the .ini file! Change all line-breaks between ] and [ to some character that will never appear in a filename (like asterisk). Then use TInifile to access the file you just preprocessed, changing the asterisks back to line breaks after you retrieve the strings. (Use StringReplace)
It's a little more complicated than that if you have more than one identifier in a section. In that case, you could use the equals sign as a flag that the preceding line break should not be removed. Maybe you read the file from the end towards the beginning.
You could even create a descendant of TIniFile that did the astrerisk-to-linebreak change for you.
No, this is certainly not an elegant solution. But, sometimes brute force like this works if you're stuck! The other solutions here are probably better, but thought I'd share this anyway in case it gives you a direction to think about heading...
Using any TStrings descendent object you can just use the CommaText property to read and write all the lines as a single string.
MyStrings.CommaText := IniFile.ReadString('Section', 'Ident');
and
IniFile.WriteString('Section', 'Ident', MyStrings.CommaText);
CommaText is smart enough to handle lines containing commas by automatically embracing them in quotes.
this code show you how to write and read multi lines with INI file
procedure TForm1.SaveButtonClick(Sender: TObject);
begin
IniFile.WriteString('Name', 'FirtName', FirstName.Text);
IniFile.WriteString('Name', 'LastName', LastName.Text);
IniFile.WriteInteger('Alpha Blend', 'Alpha Blend Value', Form1.AlphaBlendValue);
//Here start save Memo Lines
LinesCount := AboutMemo.Lines.Count;
IniFile.WriteInteger('About', 'Lines Count', LinesCount);
for I := 0 to LinesCount-1 do
IniFile.WriteString('About', 'About'+IntToStr(I), AboutMemo.Lines[i]);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin;
GetCurrentDir;
IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
FirstName.Text := IniFile.ReadString('Name', 'FirstName', '');
LastName.Text := IniFile.ReadString('Name', 'LastName', '');
Form1.AlphaBlendValue := IniFile.ReadInteger('Alpha Blend', 'Alpha Blend Value', 255);
//Here Start Read Memo Lins From INI File
LinesCount := IniFile.ReadInteger('About', 'Lines Count', 0);
for I := 0 to LinesCount-1 do
AboutMemo.Lines.Insert(I, IniFile.ReadString('About', 'About'+IntToStr(I), ''));
end;
end.
I wanted something similar, to include the body of an automated email in the ini file.
But I also wanted blank lines in the body. Here is part of the ini file
[EmailBody]
This is the weekly Survey Status notification email.
.
It lists Survey Reports that may need some attention.
.
The purpose of this information is to improve conduct-of-operations in our tracking,
recordkeeping, and maintenance of Survey Reports. This report will also enable
us to identify any changes made to electronic Surveys following the normal QA review.
.
Please review your information weekly and determine if you need to take corrective action.
Here is what I ended up doing
var
Config: TMemIniFile;
sl: TStringList;
...
sl := TStringList.Create;
Config.ReadSectionValues('EmailBody', sl);
for i := 0 to sl.Count - 1 do
if sl[i] = '.' then
sl[i] := '';
#Ken White
FIni.ReadSectionValues get full string after same key, like "key=value".
In .ini like
[B Section]
Item1=Item B1
Item B2
Item B3
Item4=Item B4
and after
FIni.ReadSectionValues('B Section',Memo1.Lines);
Memo1.Lines will be {'Item1=Item B1', 'Item4=Item B4'}
Unfortunately, we can't read values directly, without any key.
But in fact, it's not too difficult:
procedure TForm2.FormCreate(Sender: TObject);
var i:longint;
strtemp: TStringList;
begin
FIni := TMemIniFile.Create(IniName);
Memo1.Lines.Clear;
Fini.ReadSection('B Section',strtemp);
for i:=0 to strtemp.Count-1 do
Memo1.Items.Add(Fini.ReadString('B Section',strtemp[i],''));
end;
Memo1.Lines would be {'Item B1', 'Item B4'}
Upper method will read all values in section, without keys.
How to read not-keyed values - I don't know (if at all it possible in .ini).
As a solution for huge non-format .ini, just numerate all strings - read file like text file, find needed section and add same count index like a key, schematically(!):
FileString[i]:=inttostr(i)+'='+FileString[i];
If need mixed format, will require more complicated parser.
P.S. Sorry for my english, I'm not strong in it.
You can use ReadSections method to get all the section names in ini file.
And perhaps you can use TMemIniFile's ReadSectionValues method to read "multiline values" (ie to read whole section into stringlist). If that doesn't work then perhaps you could use GetStrings to get the content of the ini file and parse it "semy manually" - IIRC it returns you a stringlist with section names where each item's object holds another stringlist with section data.

Delphi can't get text from TEdit

I've encountered a problem while writing code in Delphi.
Namely I can't get acces to Components, even though they're declared and I used them in code above ( previously in procedures, now I am trying to use them in functions - maybe this is the reason, I don't know, I am not good at Delphi ).
I made a few screens to make it look clearer.
Take a look.
http://imageshack.us/photo/my-images/135/weirddelphi3.png/">
As you can see on the first screen I'm getting compiler error. It says that the component doesn't exist, but on the third screen you can see that this component exists. On the second screen I can even use this component ( Code Completion can be invoked successfully, but if I try to invoke it in secondFunction's scope I get error like this :
"Unable to invoke Code Completion due to errors in source code " - but what the hell is the error?! ). If I comment these two lines, which refer to Edit7 and Edit8, I can run the program without problems. I really can't figure out what is wrong, if any of you could give me some advice, it would be greatly appreciated. I didn't wanted to post whole code here, because it would take about 300 lines, however if u need to know something else to sort this out then ask I will tell you..
I don't have enough reputation points to post more than 2 hyperlinks so you have to do "copy & paste " with the last one :D
The problem is that Edit7 is a part of the TForm1 class. Edit7 is not accessible by name outside of TForm1. So either you can use the global Form1 variable, and do
function secondFunction(x: extended): extended;
var
paramA, paramB: extended;
begin
paramA := StrToFloat(Form1.Edit7.Text);
paramB := StrToFloat(Form1.Edit8.Text);
Result := paramA + paramB * sin(x);
end;
or you can make the secondFunction part of the TForm1 class:
function TForm1.secondFunction(x: extended): extended;
var
paramA, paramB: extended;
begin
paramA := StrToFloat(Edit7.Text);
paramB := StrToFloat(Edit8.Text);
Result := paramA + paramB * sin(x);
end;
But then you need to declare secondFunction in the declaration of the TForm1 class, like
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
function secondFunction(x: extended): extended;
end;
in the beginning of the unit.

Resources