问题
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.
回答1:
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 containshyph_en_EN.dic
and/orhyph_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 thehyphen.res
resource file with{$R hyphen.res}
, thehyphen.obj
file is already linked in thehyphen.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.
回答2:
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.
来源:https://stackoverflow.com/questions/10122297/how-to-use-this-hyphenation-library-in-delphi