SYNOPSIS

  use Biblio::Thesaurus;

  $obj = thesaurusNew();
  $obj = thesaurusLoad('iso-file');
  $obj = thesaurusRetrieve('storable-file');
  $obj = thesaurusMultiLoad('iso-file1','iso-file2',...);

  $obj->save('iso-file');
  $obj->storeOn('storable-file');

  $obj->addTerm('term');
  $obj->addRelation('term','relation','term1',...,'termn');
  $obj->deleteTerm('term');

  $obj->isDefined('term');

  $obj->describe( { rel='NT', desc="Narrow Term", lang=>"UK" } );

  $obj->addInverse('Relation1','Relation2');

  $obj->order('rela1', 'rel2', ....);
  @order = $obj->order();

  $obj->languages('l1', 'l2', ....);
  @langs = $obj->languages();

  $obj->baselang('l');
  $lang = $obj->baselang();

  $obj->topName('term');
  $term = $obj->topName();

  $html = $obj->navigate(+{configuration},%parameters);

  $html = $obj->getHTMLTop();

  $output = $obj->downtr(\%handler);
  $output = $obj->downtr(\%handler,'term', ... );

  $obj->appendThesaurus("iso-file");
  $obj->appendThesaurus($tobj);

  $obj->tc('term', 'relation1', 'relation2');
  $obj->depth_first('term', 2, "NT", "UF")

  $latex = $obj->toTex( ...)
  $xml   = $obj->toXml( ...)

ABSTRACT

This module provides transparent methods to maintain Thesaurus files. The module uses a subset from \s-1ISO\s0 2788 which defines some standard features to be found on thesaurus files. The module also supports multilingual thesaurus and some extensions to the ISOs standard.

DESCRIPTION

A Thesaurus is a classification structure. We can see it as a graph where nodes are terms and the vertices are relations between terms.

This module provides transparent methods to maintain Thesaurus files. The module uses a subset from \s-1ISO\s0 2788 which defines some standard features to be found on thesaurus files. This \s-1ISO\s0 includes a set of relations that can be seen as standard but, this program can use user defined ones. So, it can be used on \s-1ISO\s0 or not \s-1ISO\s0 thesaurus files.

File Structure

Thesaurus used with this module are standard \s-1ASCII\s0 documents. This file can contain processing instructions, comments or term definitions. The instructions area is used to define new relations and mathematical properties between them.

We can see the file with this structure:

_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_ | | | HEADER | --> Can contain, only, processing instructions, |_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_| comment or empty lines. | | | Def Term 1 | --> Each term definition should be separated | | from each other with an empty line. | Def Term 2 | | | | ..... | | | | Def Term n | |_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_|

Comments can appear on any line. Meanwhile, the comment character (#) should be the first character on the line (with no spaces before). Comments line span to the end of the line (until the first carriage return).

Processing instructions lines, like comments, should start with the percent sign (%). We describe these instructions later on this document.

Terms definitions can't contain any empty line because they are used to separate definitions from each other. On the first line of term definition record should appear the defined term. Next lines defines relations with other terms. The first characters should be an abbreviation of the relation (on upper case) and spaces. Then, should appear a comma separated list of terms.

There can be more than one line with the same relation. Thesaurus module will concatenate the lists. If you want to continue a list on the next line you can repeat the relation term of leave some spaces between the start of the line and the terms list.

Here is an example:

Animal NT cat, dog, cow fish, ant NT camel BT Life being

cat BT Animal SN domestic animal to be kicked when anything bad occurs.

There can be defined a special term (\*(C`_top_\*(C'). It should be used when you want a top tree for thesaurus navigation. So, we normally define the \*(C`_top_\*(C' term with the more interesting terms to be navigated.

The \s-1ISO\s0 subset used are:

\s-1TT\s0 - Top Term

The broadest term we can define about the current term.

\s-1NT\s0 - Narrower Term

Terms more specific than current term.

\s-1BT\s0 - Broader Term

More generic terms than current term.

\s-1USE\s0 - Synonym

Another chances when finding a Synonym.

\s-1UF\s0 - Quasi-Synonym

Terms that are no synonyms of current term but can be used, sometimes with that meaning.

\s-1RT\s0 - Related Term

Related term that can't be inserted on any other category.

\s-1SN\s0 - Scope Note

Text. Note of context of the current term. Use for definitions or comments about the scope you are using that term.

Processing Instructions

Processing instructions, as said before, are written on a line starting with the percent sign. Current commands are:

top

When presenting a thesaurus, we need a term, to know where to start. Normally, we want the thesaurus to have some kind of top level, where to start navigating. This command specifies that term, the term that should be used when no term is specified. Example: %top Contents

Contents NT Biography ... RT ...

encoding

This command defines the encoding used in the thesaurus file. Example: %enc utf8

inverse

This command defines the mathematic inverse of the relation. That is, if you define \*(C`inverse A B\*(C' and you know that \*(C`foo\*(C' is related by \*(C`A\*(C' with \*(C`bar\*(C', then, \*(C`bar\*(C' is related by \*(C`B\*(C' with \*(C`foo\*(C'. Example: %inv BT NT %inverse UF USE

description

This command defines a description for some relation class. These descriptions are used when outputting thesaurus on \s-1HTML\s0. Example: %desc SN Note of Scope %description IOF Instance of If you are constructing a multilingual thesaurus, you will want to translate the relation class description. To do this, you should use the \*(C`description\*(C' command with the language in from of it: %desc[PT] SN Nota de Contexto %description[PT] IOF Instancia de

externals

This defines classes that does not relate terms but, instead, relate a term with some text (a scope note, an url, etc.). This can be used like this: %ext SN URL %externals SN URL Note that you can specify more than one relation type per line.

languages

This other command permits the construction of a multilingual thesaurus. \s-1TO\s0 specify languages classifiers (like \s-1PT\s0, \s-1EN\s0, \s-1FR\s0, and so on) you can use one of these lines: %lang PT EN FR %languages PT EN FR To describe (legend) the language names, you should use the description command, so, you could append: %description PT Portuguese %description EN English %description FR French

baselanguage

This one makes it possible to explicitly name the base language for the thesaurus. This command should be used with the \*(C`description\*(C' one, to describe the language name. Here is a simple example: %baselang PT %languages EN FR

%description PT Portuguese %description EN English %description FR French

I18N

Internationalization functions, \*(C`languages\*(C' and \*(C`setLanguage\*(C' should be used before any other function or constructor. Note that when loading a saved thesaurus, descriptions defined on that file will be not translated. That's important!

interfaceLanguages()

This function returns a list of languages that can be used on the current Thesaurus version.

interfaceSetLanguage( <lang-name> )

This function turns on the language specified. So, it is the first function you should call when using this module. By default, it uses Portuguese. Future version can change this, so you should call it any way.

API

This module uses a perl object oriented model programming, so you must create an object with one of the \*(C`thesaurusNew\*(C', \*(C`thesaurusLoad\*(C' or \*(C`thesaurusRetrieve\*(C' commands. Next commands should be called using the \s-1OO\s0 fashion.

Constructors

thesaurusNew

To create an empty thesaurus object. The returned newly created object contains the inversion properties from the \s-1ISO\s0 classes and some stub descriptions for the same classes.

thesaurusLoad

To use the \*(C`thesaurusLoad\*(C' function, you must supply a file name. This file name should correspond to the \s-1ISO\s0 \s-1ASCII\s0 file as defined on earlier sections. It returns the object with the contents of the file. If the file does not defined relations and descriptions about the \s-1ISO\s0 classes, they are added.

Also,

$obj = thesaurusLoad({ completed => 1}, 'iso-file');

can be used to say that the thesaurus needs not to be complete after load.

thesaurusMultiLoad

You can join different thesaurus \s-1ISO\s0 files using this function:

$obj = thesaurusMultiLoad('iso-file1','iso-file2',...);

appendThesaurus

You can also append a thesaurus \s-1ISO\s0 (or another thesaurus object) to a loaded thesaurus. For that, use one of:

$obj->appendThesaurus("iso-file"); $obj->appendThesaurus( $other_thesaurus_object );

thesaurusLoadM

This method is used to load a thesaurus on the meta-thesaurus format. This is still under development.

thesaurusRetrieve

Everybody knows that text access and parsing of files is not efficient. So, this module can save and load thesaurus from Storable files. This function should receive a file name from a file which was saved using the \*(C`storeOn\*(C' function.

Methods

save

This method dumps the object on an \s-1ISO\s0 \s-1ASCII\s0 file. Note that the sequence \*(C`thesaurusLoad\*(C', \*(C`save\*(C' is not the identity function. Comments are removed and processing instructions can be added. To use it, you should supply a file name.

Note: if the process fails, this method will return 0. Any other method die when failing to save on a file.

meta2str

This method returns the \s-1ISO\s0 ascii description of the metadata.

storeOn

This method saves the thesaurus object in Storable format. You should use it when you want to load with the \*(C`thesaurusRetrieve\*(C' function.

addTerm

You can add terms definitions using the perl \s-1API\s0. This method adds a term on the thesaurus. Note that if that term already exists, all its relations will be deleted.

all_terms

Returns an array with all terms for the thesaurus base language. \s-1NOTE:\s0 this function is deprecated. Use allTerms instead.

allTerms

Returns an array with all terms for the thesaurus base language.

topName

Returns the term in the top of the thesaurus, or defined a new one if called with an argument.

top_name

Deprecated. See \*(C`<topName\*(C'>;

addRelation

To add relations to a term, use this method. It can be called again and again. Previous inserted relations will not be deleted. This method can be used with a list of terms for the relation like:

$obj->addRelation('Animal','NT','cat','dog','cow','camel');

Note: After you add a big amount of relations, autocomplete the thesaurus using the $obj->complete() method. Completing after each relation addiction is time and cpu consuming.

hasRelation

Checks if a specific relation exists in the Thesaurus:

if ($obj->hasRelation('Animal','NT','cat')) { ... }

You can check if a term has a relation \*(L"X\*(R" with anything:

if ($obj->hasRelation('Animal','SN')) { ... }

deleteRelation

$obj->deleteRelation('Animal','NT','cat','dog','cow','camel');

deleteTerm

Use this method to remove all references of the term supplied. Note that all references will be deleted.

describe

You can use this method to describe some relation class. You can use it to change the description of an existing class (like the \s-1ISO\s0 ones) or to define a new class.

isDefined

Use this method to check if a term exists in the thesaurus.

setExternal

Use this method to define that a relation is \*(L"extern\*(R".

isExternal

Use this method to check if a relation is \*(L"extern\*(R".

isLanguage

Use this method to check if a relation is a Language.

getdefinition

Deprecated. Use \*(C`<getDefinition\*(C'

getDefinition

Returns the definition for a term. The definition is a feature structure containing the term information.

getDescription

Given a relation name and a language (or the default will be used), it returns the description for that relation.

relations

Call this method with a term, and it returns a list of the relations defined for that term.

addInverse

This method should be used to describe the inversion property to relation classes. Note that if there is some previous property about any of the relations, it will de deleted. If any of the relations does not exist, it will be added.

order

With this method you can define (and access) the order of classes. This order is used whenever you call a dump function, or the navigation \s-1CGI\s0.

navigate

This function is a some what type of \s-1CGI\s0 included on a object method. You must supply an associative array of \s-1CGI\s0 parameters. This method prints an \s-1HTML\s0 thesaurus for Web Navigation.

The typical thesaurus navigation \s-1CGI\s0 is:

#!/usr/bin/perl -w

use CGI qw/:standard/; use Biblio::Thesaurus;

print header; for (param()) { $arg{$_} = param($_) } $thesaurus = thesaurusLoad("thesaurus_file"); print $thesaurus->navigate(%arg);

This method can receive, as first argument, a reference to an associative array with some configuration variables like what relations to be expanded and what language to be used by default.

So, in the last example we could write

$thesaurus->navigate(+{expand=>['NT', 'USE'], lang =>'EN'})

meaning that the structure should show two levels of '\s-1NT\s0' and '\s-1USE\s0' relations, and that it should use the English language.

These options include:

capitalize

try to capitalize terms when they are the title of the page.

expand

a reference to a list of relations that should be expanded at first level; Defaults to the empty list.

title

can be \*(C`yes\*(C' or \*(C`no\*(C'. If it is \*(C`no\*(C', the current term will not be shown as a title; Defaults to \*(C`yes\*(C'.

scriptname

the name of the script the links should point on. Defaults to current page name.

level1hide

a reference to a list of relations to do not show on the first level. Defaults to the empty list. Useful to hide the '\s-1LEN\s0' relation when using Library::Simple.

level2size

the number of terms to be shown on each second level relation; Defaults to 0 (all terms).

level2hide

a reference to a list of relations to do not show on the second level. Defaults to the empty list.

topic_name

the name of the topic \s-1CGI\s0 parameter (default: \*(L"t\*(R")

dumpHTML

This method returns a big string containing all the thesaurus in \s-1HTML\s0. It is mainly used for debug.

getHTMLTop

This method returns the \s-1HTML\s0 needed for the top level of the browsing thesaurus. It can be useful when putting a top level on the first page of a portal.

complete

This function completes the thesaurus based on the invertibility properties. This operation is only needed when adding terms and relations by this \s-1API\s0. Whenever the system loads a thesaurus \s-1ISO\s0 file, it is completed.

baselang

Use this method to set or retrieve the base language of the thesaurus. If no baselang is provided, the value \*(L"_\*(R" is returned

downtr

The \*(C`downtr\*(C' method is used to produce something from a set of terms. When no term is given, the all thesaurus is taken. It should be passed as argument a term and an associative array (handler) with anonymous subroutines that process each relation. These functions can use the pre-instantiated variables $term, $rel, @terms. The handler can have three special functions: \*(C`-default\*(C' (default handler for relations that don't have a defined function in the handler), \*(C`-eachTerm\*(C' executed with each term output (received as $_), and \*(C`-end\*(C' executed over the output of the the other functions (received as $_),

If a \*(C`-order\*(C' array reference is provided, the correspondent order of the relations will be used.

Example:

$the->downtr( { NT => sub{ ""}, #Do nothing with NT relations -default => sub{ print "$rel", join(",",@terms) } }, "frog" );

print $thesaurus->downtr( {-default => sub { "\n$rel \t".join("\n\t",@terms)}, -eachTerm => sub { "\n_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_\|_ $term $_"}, -end => sub { "Thesaurus :\n $_ \nFIM\n"}, -order => ["BT","NT","RT"], });

Both functions return a output value: the concatenation of the internal values (but functions can also work with side effects)

depth_first

The \*(C`depth_first\*(C' method is used to get the list of terms (in fact the tree of terms) related with $term by relations @r up to the level $lev

$hashref = $the->depth_first($term ,$lev, @r)

$hashref = $the->depth_first("frog", 2, "NT","UF")

$lev should be an integer greater then 0.

tc transitive closure

The \*(C`tc\*(C' method is used to eval the transitive closure of the relations @r starting from a term $term

$the->tc($term , @r)

$the->tc("frog", "NT","UF")

terms

The \*(C`terms\*(C' method is used to get all the terms related by relations @r with $term

$the->terms($term , @r);

$the->terms("frog", "NT", "UF");

toTex

Writes a thesaurus in LaTeX format... The first argument is used to pass a tag substitution hash. It uses downtr function to make the translation; a downtr handler can be given to tune some transformations details...

print $thesaurus->toTex( {EN=>["\\\\\\emph{Ingles} -- ",""]}, {FR => sub{""}})

toXml

This method writes a thesaurus in \s-1XML\s0 format... The first argument is used fo pass a tag substitution hash. It uses downtr function to make the translation; a downtr handler can be given to tune some transformations details...

print $thesaurus->toXml();

toJson

Returns a \s-1JSON\s0 tree based on \s-1NT\s0 relation. Other relation can be supplied as an argument. Future versions might include language selection.

print $thesaurus->toJson();

toHash

Returns a Hash reference with a tree based on \s-1NT\s0 relation. Other relation can be supplied as an argument. Future versions might include language selection.

print $thesaurus->toHash();

AUTHOR

Alberto Simoes, <[email protected]>

JosX Joao Almeida, <[email protected]>

Sara Correia, <[email protected]>

This module is included in the Natura project. You can visit it at http://natura.di.uminho.pt, and access the \s-1SVN\s0 tree.

COPYRIGHT & LICENSE

Copyright 2000-2012 Project Natura.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

RELATED TO Biblio::Thesaurus…

The example thesaurus file (\*(C`examples/thesaurus\*(C'),

Manpages:

Biblio::WebPortal(3) Biblio::Catalog(3) Biblio::Catalog::Bibtex(3) perl(1) manpages.

loading from Iso 2788 =head2 building a thesaurus with internal constructors =head2 writing a thesaurus in another format