1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
|
/** \page HfstWithSFSTOpenFstAndFoma.html Using HFST with SFST, OpenFst and foma
A binary HfstTransducer consists of an HFST header (more on HFST headers on the
<a href="https://github.com/hfst/hfst/wiki/TransducerHeader">
wiki pages</a>) and the transducer of the backend implementation.
If you want to write backend transducers as such, you can specify it with
the <i>hfst_format</i> argument of HfstOutputStream constructor:
\verbatim
HfstOutputStream(ImplementationType type, bool hfst_format=true);
\endverbatim
The following piece of code will write an OpenFst transducer
with tropical weights to standard output:
\verbatim
HfstTransducer ab("a", "b", TROPICAL_OFST_TYPE);
HfstOutputStream out(TROPICAL_OFST_TYPE, false);
out << ab;
\endverbatim
An HfstInputStream can also read backend transducers that do not have an
HFST header. If the standard input contains an SFST transducer, the following
piece of code will read it successfully and convert it into an HFST transducer
of type SFST_TYPE and write it to standard output (with the HFST header
included).
\verbatim
HfstInputStream in();
HfstTransducer tr(in);
HfstOutputStream out(tr.get_type(), true);
out << tr;
\endverbatim
For more information on HFST transducer formats and conversions, see the
<a href="https://github.com/hfst/hfst/wiki/TransducerFormats">
wiki pages</a>.
<H3>An issue with foma</H3>
Foma writes its binary transducers in gzipped format using the gz tools.
However, we experienced problems when trying to write to standard output or
read from standard in with gz tools (foma tools do not write to or read
from standard streams). So we choose to write, and accordingly read,
foma transducers unzipped when writing or reading binary HfstTransducers
of FOMA_TYPE. As a result, when we write an HfstTransducer of FOMA_TYPE
in its plain backend format, the user must zip it themselves before it
can be used by foma tools. Similarily, a foma transducer must be unzipped
before it can be read by HFST tools.
Suppose we have written a FOMA_TYPE HfstTransducer and want to use it with
foma tools. First we write it, in its plain backend format, to file "ab.foma"
with the following piece of code:
\verbatim
HfstTransducer ab("a", "b", FOMA_TYPE);
HfstOutputStream out("ab.foma", FOMA_TYPE, false);
out << ab;
\endverbatim
The command
\verbatim
gzip ab.foma
\endverbatim
will create a file 'ab.foma.gz' that can be used by foma tools.
The same with command line tools:
\verbatim
echo "a:b" | hfst-strings2fst -f foma > ab.hfst
hfst-fst2fst --use-backend-format -f foma > ab.foma
gzip ab.foma
\endverbatim
An example of the opposite case follows. Suppose we have a foma transducer
'transducer.foma' and want to read it inside an HFST program.
The name of the file must be appended a .gz extension so that the program
'gunzip' knows it is a zipped file. The commands
\verbatim
mv transducer.foma transducer.foma.gz
gunzip transducer.foma.gz
\endverbatim
overwrite the original file 'transducer.foma' with an unzipped version of the
same file. Now the file can be used by HFST:
\verbatim
HfstInputStream in("transducer.foma");
HfstTransducer tr(in);
\endverbatim
The same with command line tools:
\verbatim
mv transducer.foma transducer.foma.gz
gunzip transducer.foma.gz
hfst-sometool transducer.foma
\endverbatim
*/
/** \page VersionDifferences.html Differences between versions 2 and 3
If you need documentation on HFST version 2, you find it
<a href="http://hfst.sourceforge.net/hfst2/index.html">here</a>.
The page is no longer updated and it is recommended to update
to version 3.
Some changes from version 2 to 3:
<table>
<tr>
<th>HFST version 2</th>
<th>HFST version 3</th>
</tr>
<tr>
<td>Namespaces HFST and HWFST</td>
<td>Namespace #hfst and an
\link hfst::ImplementationType ImplementationType\endlink argument</td>
</tr>
<tr>
<td>KeyLayer and SymbolLayer</td>
<td>Functions take a \link hfst::String String\endlink argument,
string-to-number mapping
is done internally and automatically</td>
</tr>
<tr>
<td>TransducerLayer, Rule Layer, CoreExtensionLayer</td>
<td>\link hfst::HfstTransducer HfstTransducer\endlink,
namespace \link hfst::rules rules\endlink,
class \link hfst::implementations::HfstTransitionGraph HfstTransitionGraph\endlink</td>
</tr>
<tr>
<td>Functions delete their transducer arguments</td>
<td>Functions modify their calling object and do not change
their arguments (unless otherwise said)</td>
</tr>
<tr>
<td>No unknown or identity symbols</td>
<td>Unknown and identity \link hfst::String symbols\endlink are supported
in all implementation formats</td>
</tr>
</table>
<BR>
*/
/** \page InstallHfst.html Installing the HFST API library
You can download the source code packet hfst-3.x.x.tar.gz
from Sourceforge
<a href="http://sourceforge.net/projects/hfst/files/hfst/">download page</a>.
(NOTE: we have officially moved to <a href="https://hfst.github.io/index.html">Github</a>,
but for the time being the latest release is still available on Sourceforge.)
Extract all files with the command
\verbatim
tar -xzvf hfst-3.x.x.tar.gz
\endverbatim
move to directory hfst-3.x.x and run the commands
\verbatim
autoreconf -i && ./configure && make && make install
\endverbatim
The command 'make' will compile all the API and command line tools and 'install'
will install them to /usr/local. If you would rather install in eg. your home directory,
you can tell ./configure:
\verbatim
./configure --prefix=${HOME}
\endverbatim
<!-- If you want to compile or install just the API library without the
command line tools -->
*/
/** \page AddingYourImplementation.html Adding your own implementation under the HFST API
The HFST API is currently implemented with three finite-state libraries,
SFST, OpenFst and foma. The API is designed so that it is relatively easy to
add a new implementation to it.
There are some places in the code where you must make modifications but they are all
clearly marked in the files inside comments. Most of these modifications
just make HFST aware that there is a new implementation available and are
quite straightforward to carry out. The new backend implementation itself
is written in a separate file that must fulfil an interface common to all
backend implementations. This interface defines functions that create and
operate on transducers as well as datatypes for writing and reading binary transducers.
This interface is used for cooperation between the different finite-state libraries
and the HFST API that is visible to the end-user.
We first tell what functions and datatypes your implementation must
must offer so that it can be connected as a part of the HFST API.
We then go through the modifications that you must do to the code when adding
your implementation to the HFST API. All these modifications are also indicated in the
code inside comments, so we do not handle each change here but advise
you to see the files instead. Finally we tell what changes must be done
in the configuration file.
<BR>
<H2>The interaction between HFST and the new finite-state library</H2>
The directory libhfst/src/implementations contains two files per each
library that is added under HFST. For instance for SFST there are files
SfstTransducer.h and SfstTransducer.cc.
If a library offers more than one transducer format, there are separate files
for each format. For example for OpenFst there are files TropicalWeightTransducer.cc and .h and
LogWeightTransducer.cc and .h. Each pair of files contains
three static classes that act as an interface between HFST and the
finite-state library in question. For instance the files FomaTransducer.h and FomaTransducer.cc
contain classes FomaTransducer, FomaInputStream and FomaOutputStream that
take care of interoperation between the foma library and HFST.
The directory contains skeleton files
MyTransducerLibraryTransducer.h and MyTransducerLibraryTransducer.cc.
These files define a group of static classes
MyTransducerLibraryTransducer, MyTransducerLibraryInputStream
and MyTransducerLibraryOutputStream that
contain functions that operate on transducers and streams.
These classes act as an interface between HFST and your library.
You should write your implementations to these files.
The assumption is that most of the functionalities are found already in the
finite-state library that you are using and you just have to modify them a little
so that thay can be accessed via a standardized interface that works similarily
for all implementations.
All functions in the skeleton files return a FunctionNotImplementedException
as they have no implementation. When starting to write your own implementation,
you can also return the same exception for all functions that you have not
yet implemented.
In the same directory there are files
ConvertTransducerFormat.h ConvertTransducerFormat.cc
that contain functions that convert between HFST's own transducer format,
hfst::implementations::HfstBasicTransducer, and the transducer formats
of different implementations. Add here functions that convert between
HfstBasicTransducer and your transducer class (change MyFst to the name of
your transducer class, my_namespace to the namespace where it is written and
"my_transducer_library" to the name of your transducer library
or some other descriptive name):
\verbatim
#if HAVE_MY_TRANSDUCER_LIBRARY
static HfstBasicTransducer *
my_transducer_library_transducer_to_hfst_basic_transducer
(my_namespace::MyFst * t);
static my_namespace::MyFst *
hfst_basic_transducer_to_my_transducer_library_transducer
(const HfstBasicTransducer * t);
#endif // HAVE_MY_TRANSDUCER_LIBRARY
\endverbatim
Add also the following lines to libhfst/src/implementations/Makefile.am:
\verbatim
if WANT_MY_TRANSDUCER_LIBRARY
MAYBE_MY_TRANSDUCER_LIBRARY=MyTransducerLibraryTransducer.cc
endif
\endverbatim
and the variable <CODE>$(MAYBE_MY_TRANSDUCER_LIBRARY)</CODE> to the list
of source files:
\verbatim
BRIDGE_SRCS=$(MAYBE_SFST) $(MAYBE_OPENFST) $(MAYBE_FOMA) $(MAYBE_HFSTOL) # $(MAYBE_MY_TRANSDUCER_LIBRARY)
\endverbatim
<BR>
<H2>Making HFST aware of your implementation</H2>
When you have written your implementation, you must connect it to
HFST.
In file HfstDataTypes.h there is an enum ImplementationType that
lists all possible HfstTransducer implementation types.
It needs a new enumerator:
\verbatim
MY_TRANSDUCER_LIBRARY_TYPE,
\endverbatim
In file HfstTransducer.h you must include the header file
MyTransducerLibraryTransducer.h.
\verbatim
#if HAVE_MY_TRANSDUCER_LIBRARY
#include "implementations/MyTransducerLibraryTransducer.h"
#endif
\endverbatim
and declare that you are using the static class MyTransducerLibraryTransducer:
\verbatim
#if HAVE_MY_TRANSDUCER_LIBRARY
using hfst::implementations::MyTransducerLibraryTransducer;
#endif // #if HAVE_MY_TRANSDUCER_LIBRARY
\endverbatim
You must add the transducer type of the finite-state library that you are using
to TransducerImplementation, the union of possible transducer backend implementations:
\verbatim
#if HAVE_MY_TRANSDUCER_LIBRARY
hfst::implementations::MyFst * my_transducer_library;
#endif
\endverbatim
The transducer type is the only thing that HFST is directly aware of the new finite-state library.
All other functionalities are accessed through classes MyTransducerLibraryTransducer,
MyTransducerLibraryInputStream and MyTransducerLibraryOutputStream.
You also need and interface to the class MyTransducerLibraryTransducer:
\verbatim
#if HAVE_MY_TRANSDUCER_LIBRARY
static hfst::implementations::MyTransducerLibraryTransducer
my_transducer_library_interface;
#endif
\endverbatim
In file HfstTransducer.cc, you must define the interface between
HFST and your transducer library:
\verbatim
#if HAVE_MY_TRANSDUCER_LIBRARY
hfst::implementations::MyTransducerLibraryTransducer
HfstTransducer::my_transducer_library_interface;
#endif
\endverbatim
In file HfstInputStream.h union StreamImplementation needs a new value:
\verbatim
#if HAVE_MY_TRANSDUCER_LIBRARY
hfst::implementations::MyTransducerLibraryInputStream *
my_transducer_library;
#endif
\endverbatim
as well as enum TransducerType:
\verbatim
MY_TRANSDUCER_LIBRARY_, /* Your transducer type */
\endverbatim
In file HfstOutputStream.h union StreamImplementation needs a new value:
\verbatim
#if HAVE_MY_TRANSDUCER_LIBRARY
hfst::implementations::MyTransducerLibraryOutputStream *
my_transducer_library;
#endif
\endverbatim
The function declarations in file hfst_apply_schemas.h and their
implementations in file HfstApply.cc need an additional
argument which is a pointer to a function of the new implementation. See
the comments in the files for more information.
<BR>
<H2>Using your implementation in HFST</H2>
For all functions, constructors and destructors of classes
HfstTransducer, HfstInputStream and HfstOutputStream as well as
functions defined in file HfstApply.cc,
you must add a piece of code that calls the implementation of
that functionality in the class MyTransducerLibraryTransducer,
MyTransducerLibraryInputStream or MyTransducerLibraryOutputStream.
For some functions you have to call two or more MyTransducerLibraryTransducer
functions. However, usually more complex functions are implemented
with HFST API basic functions, so they do not have to be implemented
separately for each library. By default all functionalities throw
a FunctionNotImplementedException if the implementation
type requested is not handled as a separate case in the function.
This should make it easy for you to start adding your implementations
gradually.
You shoud go through carefully files HfstTransducer.cc, HfstInputStream.cc,
HfstOutputStream.cc and HfstApply.cc and add for each functionality
add a case that calls the implementation of the new finite-state library
if the implementation type requires it. We give here some examples of
the pattern that it used in HFST to handle different cases and choose
the right implementation.
An example of a constructor that creates an empty HfstTransducer:
\verbatim
HfstTransducer::HfstTransducer(ImplementationType type):
type(type),anonymous(false),is_trie(true), name("")
{
if (not is_implementation_type_available(type))
HFST_THROW(ImplementationTypeNotAvailableException);
switch (type)
{
#if HAVE_SFST
case SFST_TYPE:
implementation.sfst = sfst_interface.create_empty_transducer();
break;
#endif
#if HAVE_OPENFST
case TROPICAL_OFST_TYPE:
implementation.tropical_ofst =
tropical_ofst_interface.create_empty_transducer();
this->type = TROPICAL_OFST_TYPE;
break;
case LOG_OFST_TYPE:
implementation.log_ofst =
log_ofst_interface.create_empty_transducer();
break;
#endif
#if HAVE_FOMA
case FOMA_TYPE:
implementation.foma = foma_interface.create_empty_transducer();
break;
#endif
/* Add here your implementation. */
//#if HAVE_MY_TRANSDUCER_LIBRARY
//case MY_TRANSDUCER_LIBRARY_TYPE:
//implementation.my_transducer_library
// = my_transducer_library_interface.create_empty_transducer();
//break;
//#endif
case HFST_OL_TYPE:
case HFST_OLW_TYPE:
implementation.hfst_ol = hfst_ol_interface.create_empty_transducer
(type==HFST_OLW_TYPE?true:false);
break;
case ERROR_TYPE:
HFST_THROW(TransducerHasWrongTypeException);
default:
HFST_THROW(FunctionNotImplementedException);
}
}
\endverbatim
Many functions call a function in file HfstApply.cc that takes
as parameters pointers to all backend implementation functions
that are available. For example the function remove_epsilons
\verbatim
HfstTransducer &HfstTransducer::remove_epsilons()
{ is_trie = false;
return apply(
#if HAVE_SFST
&hfst::implementations::SfstTransducer::remove_epsilons,
#endif
#if HAVE_OPENFST
&hfst::implementations::TropicalWeightTransducer::remove_epsilons,
&hfst::implementations::LogWeightTransducer::remove_epsilons,
#endif
#if HAVE_FOMA
&hfst::implementations::FomaTransducer::remove_epsilons,
#endif
/* Add here your implementation. */
//#if HAVE_MY_TRANSDUCER_LIBRARY
//&hfst::implementations::MyTransducerLibraryTransducer::remove_epsilons,
//#endif
false ); }
\endverbatim
calls the function
\verbatim
HfstTransducer &apply(
#if HAVE_SFST
SFST::Transducer * (*sfst_funct)(SFST::Transducer *),
#endif
#if HAVE_OPENFST
fst::StdVectorFst * (*tropical_ofst_funct)(fst::StdVectorFst *),
hfst::implementations::LogFst * (*log_ofst_funct)(hfst::implementations::LogFst *),
#endif
#if HAVE_FOMA
fsm * (*foma_funct)(fsm *),
#endif
/* Add your library here */
//#if HAVE_MY_TRANSDUCER_LIBRARY
//my_namespace::MyFst * (*my_transducer_library_funct)(my_namespace::MyFst *),
//#endif
bool dummy /* makes sure there is always a parameter after the function pointer parameters,
* so commas between parameters are easier to handle */
);
\endverbatim
Then the function 'apply' chooses the right function pointer to use
according to the type of the transducer:
\verbatim
HfstTransducer &HfstTransducer::apply(
#if HAVE_SFST
SFST::Transducer * (*sfst_funct)(SFST::Transducer *),
#endif
#if HAVE_OPENFST
fst::StdVectorFst * (*tropical_ofst_funct)(fst::StdVectorFst *),
hfst::implementations::LogFst * (*log_ofst_funct)(hfst::implementations::LogFst *),
#endif
#if HAVE_FOMA
fsm * (*foma_funct)(fsm *),
#endif
/* Add your library. */
//#if HAVE_MY_TRANSDUCER_LIBRARY
//my_namespace::MyFst * (*my_transducer_library_funct)(my_namespace::MyFst *),
//#endif
bool foo )
{
(void)foo;
switch(this->type)
{
#if HAVE_SFST
case SFST_TYPE:
{
SFST::Transducer * sfst_temp =
sfst_funct(implementation.sfst);
delete implementation.sfst;
implementation.sfst = sfst_temp;
break;
}
#endif
#if HAVE_OPENFST
case TROPICAL_OFST_TYPE:
{
fst::StdVectorFst * tropical_ofst_temp =
tropical_ofst_funct(implementation.tropical_ofst);
delete implementation.tropical_ofst;
implementation.tropical_ofst = tropical_ofst_temp;
break;
}
case LOG_OFST_TYPE:
{
hfst::implementations::LogFst * log_ofst_temp =
log_ofst_funct(implementation.log_ofst);
delete implementation.log_ofst;
implementation.log_ofst = log_ofst_temp;
break;
}
#endif
#if HAVE_FOMA
case FOMA_TYPE:
{
fsm * foma_temp =
foma_funct(implementation.foma);
this->foma_interface.delete_foma(implementation.foma);
implementation.foma = foma_temp;
break;
}
#endif
/* Add your library here. */
//#if HAVE_MY_TRANSDUCER_LIBRARY
//case MY_TRANSDUCER_LIBRARY_TYPE:
//{
// my_namespace::MyFst * my_fst_temp =
// my_transducer_library_funct(implementation.my_transducer_library);
//delete implementation.my_transducer_library;
//implementation.my_transducer_library = my_fst_temp;
//break;
//}
//#endif
case ERROR_TYPE:
default:
HFST_THROW(TransducerHasWrongTypeException);
}
return *this;
}
\endverbatim
Finally, in file libhfst/src/Makefile.am you must add your library to the list HFST_HDRS:
\verbatim
implementations/MyTransducerLibraryTransducer.h
\endverbatim
<BR>
<H2>Configuring</H2>
The configuration file must be aware of the new implementation and the finite-state library.
You have to add the following pieces of code to the file configure.ac
(change "MY_TRANSDUCER_LIBRARY" etc. to the name of your transducer library):
\verbatim
AC_ARG_WITH([my_transducer_library],
[AS_HELP_STRING([--with-my-transducer-library],
[process unweighted fsts with my transducer library @<:@default=no@:>@])],
[],
[with_my_transducer_library=no])
AS_IF([test "x$with_my_transducer_library" != xno], [AC_DEFINE([HAVE_MY_TRANSDUCER_LIBRARY], [1],
[Define to compile my transducer library support in HFST])])
AM_CONDITIONAL([WANT_MY_TRANSDUCER_LIBRARY], [test x$with_my_transducer_library != xno])
\endverbatim
\verbatim
AS_IF([test "x$with_my_transducer_library" != "xno"],
[AC_CHECK_LIB([my_transducer_library], [main], [],
[AC_MSG_FAILURE([my transducer library test failed (--without-my-transducer-library to disable)])])])
\endverbatim
\verbatim
AS_IF([test "x$with_my_transducer_library" != "xno"],
[AC_CHECK_HEADERS([my_transducer_library/MyTransducerLibrary.h])])
\endverbatim
\verbatim
* with my transducer library: $with_my_transducer_library
\endverbatim
*/
namespace hfst {
/** \page QuickStart.html Quick Start to HFST
\section using_hfst Using HFST in your own code
After <a href="InstallHfst.html">installing</a> HFST on your computer,
include file HfstTransducer.h to the beginning of your program file and link to the
HFST library.
For example, if you have SFST installed on your computer, the following simple program
named test.cc
\verbatim
#include <cstdio>
#include "HfstTransducer.h"
using namespace hfst;
int main()
{
HfstTransducer tr1("foo", "bar", SFST_TYPE);
HfstTransducer tr2("bar", "baz", SFST_TYPE);
tr1.compose(tr2);
tr1.write_in_att_format(stdout);
}
\endverbatim
compiled with the command (this may vary on different computers)
\verbatim
gcc test.cc -lhfst -o test
\endverbatim
should print to standard out the following text when run:
\verbatim
0 1 foo baz
1
\endverbatim
<BR>
\section hfst_structure Structure of the API
The HFST API is written in the namespace #hfst that includes the following classes:
- HfstTransducer: A class for creating transducers and performing operations on them.
- HfstInputStream and HfstOutputStream: Classes for writing and reading binary transducers.
- implementations::HfstTransitionGraph: A class for creating transducers from scratch and iterating through their states and transitions.
- HfstTokenizer: A class used in creating transducers from UTF-8 strings.
and the following namespaces:
- \link hfst::rules rules\endlink: A namespace that contains functions to create two-level, replace, restriction and coercion rules.
- \link hfst::implementations implementations\endlink: A namespace that contains HFST's own transducer format.
<BR>
\section hfst_examples Examples of HFST functionalities
An example of creating a simple transducer from scratch
and converting between transducer formats and testing
transducer properties and handling exceptions:
\verbatim
using namespace hfst;
using implementations::HfstBasicTransducer;
using implementations::HfstBasicTransition;
/* Create a HFST basic transducer [a:b] with transition weight 0.3 and
final weight 0.5. */
HfstBasicTransducer t;
t.add_state(1);
t.add_transition(0, HfstBasicTransition(1, "a", "b", 0.3));
t.set_final_weight(1, 0.5);
/* Convert to tropical OpenFst format and push weights
toward final state. */
HfstTransducer T(t, TROPICAL_OFST_TYPE);
T.push_weights(TO_FINAL_STATE);
/* Convert back to HFST basic transducer. */
HfstBasicTransducer tc(T);
try {
/* Rounding might affect the precision. */
if (0.79 < tc.get_final_weight(1) &&
tc.get_final_weight(1) < 0.81) {
fprintf(stderr, "TEST OK\n");
exit(0);
}
fprintf(stderr, "TEST FAILED\n");
exit(1);
}
/* If the state does not exist or is not final */
catch (HfstException e) {
fprintf(stderr, "TEST FAILED: An exception thrown.\n");
exit(1);
}
\endverbatim
An example of creating transducers from strings, applying rules to them
and printing the string pairs recognized by the resulting transducer.
\verbatim
using namespace hfst;
ImplementationType type=FOMA_TYPE;
/* Create a simple lexicon transducer
[[foo bar foo] | [foo bar baz]]. */
HfstTokenizer tok;
tok.add_multichar_symbol("foo");
tok.add_multichar_symbol("bar");
tok.add_multichar_symbol("baz");
HfstTransducer words("foobarfoo", tok, type);
HfstTransducer t("foobarbaz", tok, type);
words.disjunct(t);
/* Create a rule transducer that optionally replaces
"bar" with "baz" between "foo" and "foo". */
HfstTransducerPair context
(HfstTransducer("foo", type),
HfstTransducer("foo", type) );
HfstTransducer mapping
("bar", "baz", type);
bool optional=true;
StringPairSet alphabet;
alphabet.insert(StringPair("foo", "foo"));
alphabet.insert(StringPair("bar", "bar"));
alphabet.insert(StringPair("baz", "baz"));
HfstTransducer rule = rules::replace_up
(context, mapping, optional, alphabet);
/* Apply the rule transducer to the lexicon. */
words.compose(rule).minimize();
/* Extract all string pairs from the result and print
them to stdout. */
HfstTwoLevelPaths results;
try {
words.extract_paths(results);
}
catch (TransducerIsCyclicException e)
{
/* This should not happen because transducer is not cyclic. */
fprintf(stderr, "TEST FAILED\n");
exit(1);
}
/* Go through all paths. */
for (HfstTwoLevelPaths::const_iterator it = results.begin();
it != results.end(); it++)
{
/* Go through each path. */
StringPairVector spv = it->second;
std::string istring("");
std::string ostring("");
for (StringPairVector::const_iterator IT = spv.begin();
IT != spv.end(); IT++)
{
istring.append(IT->first);
ostring.append(IT->second);
}
fprintf(stdout, "%s : %s\n",
istring,
ostring);
}
\endverbatim
An example of reading binary transducers from standard input,
converting them to SFST format and writing them to stdout
and in AT&T format to file "testfile.att":
\verbatim
HfstInputStream in;
HfstOutputStream out(SFST_TYPE);
FILE * file = fopen("testfile.att", "wb");
bool first_transducer=true;
while (not in.is_eof())
{
if (not first_transducer)
fprintf(file, "--\n"); /* AT&T format separator. */
HfstTransducer t(in);
HfstTransducer tc(t, SFST_TYPE);
out << tc;
tc.write_in_att_format(file);
first_transducer=false;
}
in.close();
out.close();
fclose(file);
\endverbatim
<BR>
*/
}
/** \mainpage
HFST - The Helsinki Finite-State Transducer technology is intended for creating and
manipulating weighted or unweighted synchronic transducers implementing regular relations.
UTF-8 is chosen as the character encoding used in the HFST software.
Currently, HFST has been implemented using the
<a href="https://www.cis.lmu.de/~schmid/tools/SFST/">SFST</a>,
<a href="http://www.openfst.org">OpenFst</a> and
<a href="https://github.com/mhulden/foma">foma</a> software libraries.
Other versions may be added in some future release.
SFST and foma implementations are unweighted and OpenFst implementation is weighted.
Some of the HFST interface functionalities have also been implemented for
HFST's own two transducer formats, HfstTransitionGraph and optimized lookup format.
The previous is useful for converting between transducer formats and storing transducers
in an implementation-independent format. The latter is used for fast lookup
of strings in a transducer.
All implementations work according to the same interface, so it is possible
to compile the same piece of code using different backend libraries.
It is also possible to <a href="AddingYourImplementation.html">add</a>
your own implementation under the HFST interface.
For a quick start to the HFST interface with examples, see
<a href="QuickStart.html">here</a>.
The examples given in this documentation use
<a href="http://www.fsmbook.com/">Xerox transducer notation</a>.
<br>
\section what_is_new What is new in version 3
- A third transducer library, <a href="https://github.com/mhulden/foma">foma</a>, is included
- <a href="AddingYourImplementation.html">Adding</a> your own implementation under the HFST interface is easy
- Unknown and identity \link hfst::String symbols\endlink are supported
\section features Features
- Create transducers and apply \link hfst::HfstTransducer operations\endlink on them
- Create transducers \link hfst::implementations::HfstTransitionGraph from scratch\endlink
- \link hfst::implementations::HfstTransitionGraph::const_iterator Iterate\endlink through a transducer's states and transitions
- Create transducers by \link hfst::HfstTokenizer tokenizing\endlink UTF-8 strings with multicharacter symbols
- Apply two-level, replace, restriction and coercion \link hfst::rules rules\endlink
\section tutorial Tutorial
- A <a href="QuickStart.html">quick start</a> to the HFST interface with examples
- <a href="AddingYourImplementation.html">Adding</a> your own implementation of HFST
- <a href="VersionDifferences.html">Differences</a> between HFST versions 2 and 3
- <a href="HfstWithSFSTOpenFstAndFoma.html">Using HFST</a> with SFST, OpenFst and foma
\section download_hfst Download
- <a href="http://sourceforge.net/projects/hfst/files/hfst">Download</a> and
<a href="InstallHfst.html">install</a> the HFST interface and command line tools
(NOTE: we have officially moved to <a href="https://hfst.github.io/index.html">Gihub</a>,
but for the time being the latest release is still available on Sourceforge.)
\section links Links
- <a href="https://github.com/hfst/hfst/wiki/Command-Line-Tools">Examples</a> of programs implemented using the HFST interface
- <a href="https://github.com/hfst/hfst/wiki">Documentation</a> of the HFST project
<br>
*/
/**
@dir libhfst/src
@brief A directory for the HFST interface.
The files in this directory contain the public interface
of HFST and internal code that is not implementation-specific.
*/
/**
@dir libhfst/src/implementations
@brief A directory for the different backend implementations.
The files in this directory act as a bridge between the HFST
interface and the backend libraries.
Basicly this directory contains two files per implementation,
for example the files SfstTransducer.h and SfstTransducer.cc
define the SFST implementation.
The implementation of HFST's own transducer format, HfstTransitionGraph
is also placed here.
*/
/**
@dir libhfst/src/implementations/optimized-lookup
@brief A directory for optimized lookup transducer
implementations.
*/
|