/usr/share/perl5/Biblio/Thesaurus.pm is in libbiblio-thesaurus-perl 0.43-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 | # -*- Mode: Perl; tab-width: 2; -*-
package Biblio::Thesaurus;
use 5.010;
use strict;
use warnings;
require Exporter;
use Storable;
use CGI qw/:standard/;
use Data::Dumper;
# Version
our $VERSION = '0.43';
# Module Stuff
our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw() ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
# We are working with an object oriented interface. This means, we only
# need to export constructors.
#
# The last three variables are used to down-translation sub (downtr)
our @EXPORT = qw(
&thesaurusLoad
&thesaurusLoadM
&thesaurusNew
&thesaurusRetrieve
&thesaurusMultiLoad
@terms $term $rel);
our ($casesen,$rel,@terms,$term);
##
#
#
sub top_name { topName(@_) }
sub topName {
my ($self, $name) = @_;
if($name){ $self->{name} = $name;}
else { return $self->{name};}
}
sub order {
my ($self,@names) = @_;
if(@names){ $self->{order} = [@names] ; }
else { defined $self->{order} ? (@{$self->{order}}) : () }
}
sub isLanguage{
my ($self,$l) = @_;
return defined $self->{languages}{$l}
}
sub languages{
my ($self,@names) = @_;
if(@names){ for (@names) { $self->{languages}{$_} = 1; }}
else { keys (%{$self->{languages}}) }
}
sub baselang {
my ($self,$name) = @_;
if($name){ $self->{$name} = $self->{$self->{baselang}};
delete $self->{$self->{baselang}};
$self->{baselang} = $name;}
else {return $self->{baselang};}
}
##
#
#
sub terms {
my ($self, $term, @rels) = @_;
my $base = $self->{baselang};
return () unless $self->isDefined($term);
$term = $self->_definition($term);
@rels = map { uc $_ } @rels;
return (map {
if (defined($self->{$base}{$term}{$_})) {
if (ref($self->{$base}{$term}{$_}) eq "ARRAY") {
@{$self->{$base}{$term}{$_}}
} else {
($self->{$base}{$term}{$_})
}
} else {
()
}
} @rels);
}
##
# Parece-me que não está a ser usada.
#
# sub external {
# my ($self,$term,$external) = @_;
# $external = uc($external);
# $term = $self->definition($term);
# return $self->{$self->{baselang}}{$term}{$external};
# }
###
#
#
sub all_terms { allTerms(@_) }
sub allTerms {
my $self = shift;
return sort keys %{$self->{$self->{baselang}}};
}
###
#
#
sub depth_first {
my ($self,$term,$niveis,@relat) = @_;
my %st=();
if ($niveis>=1) {
for ($self->terms($term,@relat)) {
$st{$_}=depth_first($self,$_,$niveis-1,@relat);
}
\%st; }
elsif($niveis == 0) {1}
else {1}
}
###
#
#
sub _default_norelations {
return {
'URL'=> 1,
'SN' => 1
};
}
###
#
#
sub _default_inversions {
+{ NT => 'BT', BT => 'NT', RT => 'RT', USE => 'UF', UF => 'USE' };
}
###
#
#
sub _translateTerm {
my ($self,$term,$lang,$dic) = @_;
$dic = {} unless $dic;
$lang = uc($lang);
# Se foi $lang definido como linguagem
if (defined($self->{languages}{$lang})) {
my $trad;
# Se existe a tradução
if (defined($trad = $self->{$self->{baselang}}{$term}{$lang})) {
return $trad;
}
}
if(defined $dic->{$term}) {return $dic->{ $term}}
if(defined $dic->{lcfirst($term)}) {return ucfirst($dic->{lcfirst($term)})}
if(defined $dic->{lc($term)}) {return uc($dic->{ lc($term)})}
return "[$self->{baselang}-$lang:".$self->getdefinition($term)."]";
}
###
#
#
sub appendThesaurus {
my ($self,$other) = @_;
# This way we handle full thesaurus objects or simple filename
unless (ref($other)) {
$other = thesaurusLoad($other);
}
my $new;
# Check if baselang is the same, or if some of them is undefined
if ($self->{baselang} eq $other->{baselang}) {
$new->{baselang} = $self->{baselang}
} elsif ($self->{baselang} eq "_") {
$new->{baselang} = $other->{baselang}
} elsif ($other->{baselang} eq "_") {
$new->{baselang} = $self->{baselang}
} else {
return undef;
}
# If some of the top is _top_, the other is choosed. If
# there are two different tops, use the first ($self) one
if ($other->{name} eq $self->{name}) {
$new->{name} = $self->{name}
} elsif ($other->{name} eq "_top_") {
$new->{name} = $self->{name}
} elsif ($self->{name} eq "_top_") {
$new->{name} = $other->{name}
} else {
$new->{name} = $self->{name}
}
# VERSION: current module version
$new->{version} = $VERSION;
sub _ffjoin {
# key, hash1ref, hash2ref
my ($c,$a,$b) = @_;
if (exists($a->{$c}) && exists($b->{$c})) {
return {%{$a->{$c}},%{$b->{$c}}};
} elsif (exists($a->{$c})) {
return {%{$a->{$c}}}
} elsif (exists($b->{$c})) {
return {%{$b->{$c}}}
} else {
return {}
}
}
# Inverses: join hash tables... in conflict, $self is used
$new->{inverses} = _ffjoin("inverses",$other,$self);
# Descriptions: in conflict, $self is used
$new->{descriptions} = _ffjoin("descriptions",$other,$self);
# Externals: union
$new->{externals} = _ffjoin("externals",$self,$other);
# Languages: union
$new->{languages} = _ffjoin("languages",$self,$other);
# delete($new->{languages}{"_"}) if ($new->{baselang} ne "_");
# Get terms for the new thesaurus
my @terms = _set_of(keys %{$self ->{$self ->{baselang}}},
keys %{$other->{$other->{baselang}}});
# Para cada termo do thesaurus...
for my $term (@terms) {
# existe em ambos...
if ($self->isDefined($term) && $other->isDefined($term)) {
my ($a_def,$b_def) = ($self->_definition($term),
$other->_definition($term));
my $def = $a_def;
$new->{defined}{lc($def)} = $def;
my @class = _set_of(keys %{$self ->{$self ->{baselang}}{$a_def}},
keys %{$other->{$other->{baselang}}{$b_def}});
# para cada uma das suas relações...
for my $class (@class) {
if ($class eq "_NAME_") {
# print STDERR Dumper($new->{$new->{baselang}}{$def});
# optar pela forma do thesaurus A
$new->{$new->{baselang}}{$def}{_NAME_} = $def;
} elsif ($new->{externals}{$class}) {
if (exists($self->{$self->{baselang}}{$a_def}{$class})) {
push @{$new->{$new->{baselang}}{$def}{$class}},
@{$self->{$self->{baselang}}{$a_def}{$class}};
}
if (exists($other->{$other->{baselang}}{$b_def}{$class})) {
push @{$new->{$new->{baselang}}{$def}{$class}},
@{$other->{$other->{baselang}}{$b_def}{$class}};
}
} elsif ($new->{languages}{$class}) {
$new->{$new->{baselang}}{$def}{$class} = "_";
} else {
if (exists($self ->{$self ->{baselang}}{$a_def}{$class}) &&
exists($other->{$other->{baselang}}{$b_def}{$class})) {
# Join lists
my %there;
@there{@{$self->{$self->{baselang}}{$a_def}{$class}}} =
1 x @{$self->{$self->{baselang}}{$a_def}{$class}};
push @{$new->{$new->{baselang}}{$def}{$class}}, keys %there;
for (@{$other->{$other->{baselang}}{$b_def}{$class}}) {
unless ($there{$_}) {
push @{$new->{$new->{baselang}}{$def}{$class}}, $_;
}
$there{$_} = 1;
}
} elsif (exists($self->{$self->{baselang}}{$a_def}{$class})) {
$new->{$new->{baselang}}{$def}{$class} =
$self->{$self->{baselang}}{$a_def}{$class};
} else { ## other->b_def->class
$new->{$new->{baselang}}{$def}{$class} =
$other->{$other->{baselang}}{$b_def}{$class};
}
}
}
} elsif ($self->isDefined($term)) {
$new->{defined}{lc($term)} = $self->_definition($term);
$new->{$new->{baselang}}{$term} = $self->{$self->{baselang}}{$term};
} else { ### $other->isDefined($term)
$new->{defined}{lc($term)} = $other->_definition($term);
$new->{$new->{baselang}}{$term} = $other->{$other->{baselang}}{$term};
}
}
return bless($new);
}
###
#
#
sub thesaurusMultiLoad {
my @files = @_;
my $self = thesaurusLoad(shift @files);
while(@files) {
$self->appendThesaurus(shift @files);
}
return $self;
}
###
#
#
sub top {
my $self = shift;
my $script = shift;
return "<ul>".join("\n",
map {"<li><a href=\"$script?t=$_\">$_</a></li>"}
@{$self->{$self->{baselang}}->{$self->{name}}->{NT}}). "</ul>";
}
###
#
#
sub _default_descriptions {
return {
'RT' => q/Related term/,
'TT' => q/Top term/,
'NT' => q/Narrower term/,
'BT' => q/Broader term/,
'USE' => q/Synonym/,
'UF' => q/Quasi synonym/,
'SN' => q/Scope note/,
};
}
sub setExternal {
my ($self,@rels) = @_;
for (@rels) {
$self->{externals}{uc($_)} = 1;
}
return $self;
}
sub isExternal {
my ($self,$ext) = @_;
return (defined($self->{externals}{uc($ext)}) &&
defined($self->{externals}{uc($ext)}) == 1);
}
###
#
#
sub thesaurusNew {
my $obj = {
# thesaurus => {},
inverses => _default_inversions(),
descriptions => _default_descriptions(),
externals => _default_norelations(),
name => '_top_',
baselang => '?',
languages => {},
version => $VERSION,
prefix => "",
};
# bless and return it! Amen!
return bless($obj);
}
###
#
#
sub storeOn {
store(@_);
}
###
#
#
sub thesaurusRetrieve {
my $file = shift;
my $obj = retrieve($file);
if (defined($obj->{version})) {
return $obj;
} else {
die("Rebuild your thesaurus with a recent Biblio::Thesaurus version");
}
}
###
#
#
sub _trurl {
my $t = shift;
$t =~ s/\s/+/g;
return $t;
}
###
#
#
sub getHTMLTop {
my $self = shift;
my $script = shift || $ENV{SCRIPT_NAME};
my $t = "<ul>";
$t.=join("\n",
map { "<li><a href=\"$script?t=" ._trurl($_). "\">$_</a></li>" }
@{$self->{$self->{baselang}}->{$self->{name}}->{NT}});
$t .= "</ul>";
return $t;
}
###
#
#
sub thesaurusLoad {
my %opt =();
# completed => 1
if(ref($_[0]) eq "HASH") {%opt = (%opt , %{shift(@_)}) } ;
my ($file,$self) = @_;
my %thesaurus;
unless($self){
$self->{inverses} = _default_inversions();
$self->{descriptions} = _default_descriptions();
$self->{externals} = _default_norelations();
$self->{name} = "_top_";
$self->{baselang} = "_";
$self->{languages} = {};
$self->{defined} = {};
$self->{version} = $VERSION; }
else {
$self->{defined} = {};
}
# Open the thesaurus file to load
open ISO, $file or die (q/Can't open thesaurus file/);
### binmode(ISO,"$opt{encoding}:") if($opt{encoding});
# While we have commands or comments or empty lines, continue...
while(($_ = <ISO>)=~/(^(%|#))|(^\s*$)/) {
chomp;
if (/^%\s*inv(?:erse)?\s+(\S+)\s+(\S+)/) {
# Treat the inv*erse command
$self->{inverses}{uc($1)} = uc($2);
$self->{inverses}{uc($2)} = uc($1);
} elsif (/^%\s*enc(oding)?\s+(\S+)/) {
$self->{encoding} = lc $2;
$self->{encoding} =~ s/_/-/g;
binmode ISO, ":encoding($self->{encoding})";
} elsif (/^%\s*tit(le)?\s+(.+)/) {
$self->{title} = $2;
} elsif (/^%\s*aut(hor)?\s+(.+)/) {
$self->{author} = $2;
} elsif (/^%\s*desc(ription)?\[(\S+)\]\s+(\S+)\s+(.*)$/) {
# Treat the desc*cription [lang] command.... 'RT EN'
$self->{descriptions}{uc($3)." ".uc($2)} = $3;
} elsif (/^%\s*desc(ription)?\s+(\S+)\s+(.*)$/) {
# Treat the desc*cription command
$self->{descriptions}{uc($2)} = $3;
} elsif (/^%\s*ext(ernals?)?\s+(.*)$/) {
# Treat the ext*ernals command
chomp(my $classes = uc($2));
for (split /\s+/, $classes) {
$self->{externals}{$_} = 1;
}
} elsif (/^%\s*lang(uages?)?\s+(.*)$/) {
# Treat the lang*uages command
chomp(my $classes = uc($2));
for (split /\s+/, $classes) {
$self->{languages}{$_} = 1;
}
} elsif (/^%\s*top\s+(.*)$/) {
$self->{name} = $1;
} elsif (/^%\s*baselang(uage)?\s+(\S+)/) {
$self->{baselang} = uc($2);
} elsif (/^%/) {
print STDERR "Unknown command: '$_'\n\n";
} else {
# It's a comment or an empty line: do nothing
}
}
# Redefine the record separator
my $old_sep = $/;
$/ = "";
# The last line wasn't a comment, a command or an empty line, so use it!
$_ .= <ISO>;
my $ncommands = $.-1;
# While there are definitions...
do {
# define local variables
my ($class,$term,$relations);
## Concat lines that continue back in one
s/\n[ \t]+/ /g; # Can't use \s because "\n" =~ m!\s!
# The first line contains the term to be defined
/(.+)(?:\n((.|\n)+)|\n?$)/;
$term = $1;
$relations = $2 || "";
# If the term is all spaces, go back...
if ($term =~ /^\s+$/) {
print STDERR "Term with only spaces ignored at block term ",$.-$ncommands,"\n\n";
$term = '#zbr'; # This makes the next loop think this is a comment and ignore it
}
# Let's see if the term is commented...
unless ($term =~ /^#/) {
$term = _term_normalize($term);
$term = $self->{defined}{lc($term)} if ($self->{defined}{lc($term)});
$thesaurus{$term}{_NAME_} = $term;
$self->{defined}{lc($term)} = $term;
# The remaining are relations
$_ = $relations;
# OK! The term is *not* commented...
# For each definition line...
$_.="\n" unless /\n$/;
while (/((([^#\s]+)|#)[ \t]*(.*)\n)/g) {
next unless $4;
# Is it commented?
unless ($2 eq "#") {
# it seems not... set the relation class
$class = uc($2); # || $class;... now multiline are handled before this
print STDERR "** WARNING **: '$1'\n" unless $class;
# See if $class has a description
$self->{descriptions}{$class} = ucfirst(lc($class)) unless defined $self->{descriptions}{$class};
## $descs->{$class}= ucfirst(lc($class)) unless(defined($descs->{$class}));
# divide the relation terms by comma unless it is a language or extern relation
if ( exists($self->{externals}{$class}) && defined($self->{externals}{$class}) ) {
## $thesaurus{$term}{$class}.= ($2?"$4":" $4");
## $thesaurus{$term}{$class}.= ($thesaurus{$term}{$class}?" $4":"$4");
push @{$thesaurus{$term}{$class}}, $4;
} elsif (exists($self->{languages}{$class}) && defined($self->{languages}{$class})) {
# $translations->{$class}->{_term_normalize($4)}.=$term;
$self->{$class}{$4}.=$term;
$self->{defined}{_term_normalize(lc($4))} = $term;
$thesaurus{$term}{$class} = $4;
} else {
push(@{$thesaurus{$term}{$class}}, map {
_term_normalize($_)
} split(/\s*,\s*/, $4));
}
}
}
}
} while(<ISO>);
# Close the ISO thesaurus file
close ISO;
# revert to the old record separator. Not needed, but beautifer.
$/ = $old_sep;
$self->{$self->{baselang}} = \%thesaurus;
$self->{languages}{$self->{baselang}} = 1;
# bless and return the thesaurus! Amen!
if (exists($opt{completed}) && $opt{completed}) {
return bless($self);
} else {
return complete(bless($self));
}
}
sub _lc{
if($casesen){$_[0]}
else {lc($_[0])}
}
sub thesaurusLoadM {
my $file = shift;
my ($t,$rs)= _treatMetas1(thesaurusLoad($file));
if(@$rs){
undef $t->{$t->{baselang}};
undef $t->{defined};
_treatMetas2(thesaurusLoad($file,$t),$rs);}
else{$t}
}
sub _treatMetas1 {
my $t = shift;
my @ts=();
my %r=();
if(@ts=$t->terms("_order_","NT")) { $t->order(@ts);
@r{@ts,"_order_"}=(@ts,1) }
if(@ts=$t->terms("_external_","NT")){ $t->setExternal(@ts);
@r{@ts,"_external_"}=(@ts,1) }
if(@ts=$t->terms("_top_","NT")) { $t->topName($ts[0]);
$r{"_top_"}=1 }
if(@ts=$t->terms("baselang_","NT")){ $t->baselang($ts[0]);
@r{@ts,"baselang_"}=(@ts,1) }
if(@ts=$t->terms("_language_","NT")){ $t->languages(@ts);
@r{@ts,"_language_"}=(@ts,1) }
if(@ts=$t->terms("_symmetric_","NT")){ for(@ts){ $t->addInverse($_,$_);}
@r{@ts,"_symmetric_"}=(@ts,1) }
# for each new relation describe it, add Invers and remove it as Term
if(@ts=$t->terms("_relation_","NT")){
$r{"_relation_"}=1 ;
$t->downtr(
{ SN => sub{ $t->describe({rel => $term, desc=>$terms[0]}) }, ## FALTA A LINGUA
INV => sub{ $t->addInverse($term,$terms[0])},
RANG => sub{ $t->setExternal($term)},
-order => ["SN","INV"],
-eachTerm => sub{ $r{$term}=$term },
}, @ts);
}
($t,[(keys %r)]);
}
sub _treatMetas2{
my ($t,$rs)= @_;
for (@$rs){ $t->deleteTerm($_)}
$t;
}
###
#
#
sub getDescription {
my ($obj, $rel, $lang) = @_;
if (defined($lang)) {
my $x = uc($rel)." ".uc($lang);
return exists($obj->{descriptions}->{$x})?$obj->{descriptions}->{$x}:"...";
} else {
my $x = uc($rel)." ".uc($obj->{baselang});
if (exists($obj->{descriptions}->{$x})) {
return $obj->{descriptions}->{$x};
} elsif (exists($obj->{descriptions}->{$rel})) {
return $obj->{descriptions}->{$rel};
} else {
return "...";
}
}
}
###
#
#
sub describe {
my ($obj, $conf) = @_;
my ($class, $desc, $lang);
return unless ($class = uc($conf->{rel}));
return unless ($desc = $conf->{desc});
if ($conf->{lang}) {
$lang = " ".uc($conf->{lang});
} else {
$lang = "";
}
$obj->{descriptions}->{$class.$lang}=$desc;
}
###
#
#
sub addInverse {
my ($obj,$a,$b) = @_;
$a = uc($a);
$b = uc($b);
$obj->{descriptions}{$a}="..." unless(defined($obj->{descriptions}{$a}));
$obj->{descriptions}{$b}="..." unless(defined($obj->{descriptions}{$b}));
for (keys %{$obj->{inverses}}) {
delete($obj->{inverses}{$_}) if (($obj->{inverses}{$_} eq $a) ||
($obj->{inverses}{$_} eq $b));
}
$obj->{inverses}{$a}=$b;
$obj->{inverses}{$b}=$a;
}
###
#
#
sub meta2str {
my $obj = shift;
my $term;
my %inverses = %{$obj->{inverses}};
my %descs = %{$obj->{descriptions}};
my $t = "";
# Save the 'encoding' command
#
$t.="\%encoding $obj->{encoding}\n\n" if defined $obj->{encoding} ;
# Save the 'title' command
#
$t.="\%title $obj->{title}\n\n" if defined $obj->{title};
# Save the 'author' command
#
$t.="\%author $obj->{author}\n\n" if defined $obj->{author};
# Save the externals commands
#
$t.= "\%externals " . join(" ",keys %{$obj->{externals}});
$t.="\n\n";
# Save the languages commands
#
$t.= "\%languages " . join(" ",keys %{$obj->{languages}});
$t.="\n\n";
# Save the 'top' command
#
$t.="\%top $obj->{name}\n\n" if $obj->{name} ne "_top_";
# Save the 'baselanguage' command
#
$t.="\%baselanguage $obj->{baselang}\n\n" if $obj->{baselang} ne "_";
# Save the inverses commands
#
for $term (keys %inverses) {
$t.= "\%inverse $term $inverses{$term}\n";
}
$t.="\n\n";
# Save the descriptions commands
#
for $term (keys %descs) {
if ( $term =~ /^(\w+)\s+(\w+)$/ ) {
$t.= "\%description[$2] $1 $descs{$term}\n";
} else {
$t.= "\%description $term $descs{$term}\n";
}
}
$t.="\n\n";
$t;
}
##
#
#
sub save {
my $obj = shift;
my $file = shift;
my ($term,$class);
my %thesaurus = %{$obj->{$obj->{baselang}}};
my $t = meta2str($obj); #save the metadata
# Save the thesaurus
#
for $term (keys %thesaurus) {
$t.= "\n$thesaurus{$term}{_NAME_}\n";
for $class ( keys %{$thesaurus{$term}} ) {
next if $class eq "_NAME_";
if(defined $obj->{languages}{$class}) {
$t.= "$class\t$thesaurus{$term}->{$class}\n";
} else {
# if save_compact, juntar por ',' as relacoes nao external
$t.= "$class\t$_\n" for (@{$thesaurus{$term}{$class}});
}
}
}
open F, ">$file" or return 0;
if (defined $obj->{encoding}) {
$obj->{encoding} = lc($obj->{encoding});
$obj->{encoding} =~ s/_/-/g;
binmode(F,":encoding($obj->{encoding})") ;
}
print F $t;
close F;
return 1;
}
###
#
#
sub navigate {
# The first element is the object reference
my $obj = shift;
# This is the script name
my $script = $ENV{SCRIPT_NAME} || "";
# Get the configuration hash
my $conf = {};
if (ref($_[0])) { $conf = shift }
my $expander = $conf->{expand} || [];
my @tmp = map {$obj->{inverses}{$_}} @$expander;
my $language = $conf->{lang} || undef;
my $second_level_limit = $conf->{level2size} || 0;
my $hide_on_first_level = $conf->{level1hide} || [];
my $hide_on_second_level = $conf->{level2hide} || \@tmp;
my $capitalize = $conf->{capitalize} || 0;
my $topic = $conf->{topic_name} || "t";
my %hide;
@hide{@$hide_on_first_level} = @$hide_on_first_level;
$script = $conf->{scriptname} if (exists($conf->{scriptname}));
my %param = @_;
my $term;
my $show_title = 1;
if (exists($param{$topic})) {
$param{$topic} =~ s/\+/ /g;
$term = $obj->getdefinition($param{$topic});
} else {
$show_title = 0 if exists($conf->{title}) && $conf->{title} eq "no";
if ($obj->isDefined($obj->{name})) {
$term = $obj->{defined}{lc($obj->{name})};
} else {
$term = '_top_';
}
}
my (@terms,$html);
# If we don't have the term, return only the title
return h2($term) unless ($obj->isDefined($term));
# Make the page title
$html = h2(capitalize($capitalize, $obj->_translateTerm($term,$language))) if $show_title;
# Get the external relations
my %norel = %{$obj->{externals}};
# Now print the relations
my $rel;
for $rel (keys %{$obj->{$obj->{baselang}}{$term}}) {
# next iteraction if the relation is the _NAME_
next if ($rel eq "_NAME_");
# Next if we want to hide it
next if exists $hide{$rel};
# This block jumps if it is an expansion relation
next if grep {$_ eq uc($rel)} @{$expander};
# The externs exceptions...
if (exists($norel{$rel})) {
# It's an external, so...
#
# Its description is "..."?
my $desc = $obj->getDescription($rel, $language);
$html .= join("<br/>\n", map { b($desc)." $_" } @{$obj->{$obj->{baselang}}{$term}{$rel}});
$html .= " ".br;
} elsif (exists($obj->{languages}{$rel})) {
## This empty block is used for languages translations
} else {
## OK! It's a simple relation
# There is a translation for the *relation* description?
my $desc = $obj->getDescription($rel, $language);
if ($desc eq "...") {
$html .= b($rel)." ";
} else {
$html.= b($desc)." ";
}
# Now, write each term with a thesaurus link
$html.= join(", ", map {
my $term = $_;
my $link = $term;
$link =~ s/\s/+/g;
$term = $obj->_translateTerm($term, $language);
a({ href=>"$script?$topic=$link"},$term)
} sort {lc($a)cmp lc($b)} @{$obj->{$obj->{baselang}}{$term}{$rel}});
$html.= br;
}
}
# Now, treat the expansion relations
for $rel (@{$expander}) {
$rel = uc($rel);
if (exists($obj->{$obj->{baselang}}{$term}{$rel})) {
@terms = sort {lc($a)cmp lc($b)} @{$obj->{$obj->{baselang}}{$term}{$rel}};
$html.= ul(li([map {
_thesaurusGetHTMLTerm($_, $obj, $script, $language,
$second_level_limit, $hide_on_second_level);
} @terms])) if (@terms);
}
}
return $html;
}
###
#
#
sub toTex{
my $self = shift;
my $_corres = shift || {};
my $mydt = shift || {};
# my $a;
my %descs = %{$self->{descriptions}};
my $procgr= sub {
my $r="";# my $a;
my $auxrel = $descs{$rel} || $rel;
$auxrel =~ s/_/ /g;
$auxrel = ucfirst(lc($auxrel));
my $ki = $_corres->{$rel}->[0] || "\\\\\\emph{$auxrel} -- " ;
my $kf = $_corres->{$rel}->[1] || "\n";
$r = "\\item[$ki]" .
join(' $\diamondsuit$ ',(sort {lc($a) cmp lc($b)} @terms)) if @terms;
};
$self->downtr(
{ '-default' => $procgr,
'-end' => sub{s/_/\\_/g;
"\\begin{description}\n$_\\end{description}\n"},
'-eachTerm' =>
sub{"\n\\item[$term]~\\begin{description}\n$_\\end{description}\n"},
(defined $self->{order}?(-order => $self->{order}):()),
(%$mydt) }
);
}
sub toXml{
my $self = shift;
my $_corres = shift || {};
my $mydt = shift || {};
my $a;
my $proc= sub {
my $r=""; my $a;
my $ki = $_corres->{$rel}->[0] || "$rel" ;
my $kf = $_corres->{$rel}->[1] || "/$rel";
for $a (@terms){ $r .= " <$ki>$a<$kf>\n";};
$r;
};
$self->downtr({
'-default' => $proc,
'-eachTerm' =>
sub{" <term>\n <$self->{baselang}>$term</$self->{baselang}>\n$_ </term>\n"},
'-end' => sub{"<thesaurus>\n$_</theasurus>\n"},
(%$mydt)
});
}
###
#
#
sub dumpHTML {
my $obj = shift;
my %thesaurus = %{$obj->{$obj->{baselang}}};
my $t = "";
for (keys %thesaurus) {
$t.=_thesaurusGetHTMLTerm($_,$obj,"",$obj->{baselang});
}
return $t;
}
###
#
#
sub relations {
my ($self,$term) = @_;
return sort grep { $_ !~ /^_/ } keys %{$self->{$self->{baselang}}->{$term}}
}
###
#
# Given a term, return it's information (second level for navigate)
sub _thesaurusGetHTMLTerm {
my ($term,$obj,$script,$language,$limit,$hide) = @_;
my @rels2hide = map {uc} (defined($hide))?@$hide:();
my %rels2hide;
@rels2hide{@rels2hide}=1;
# Put thesaurus and descriptions on handy variables
my %thesaurus = %{$obj->{$obj->{baselang}}};
my %descs = %{$obj->{descriptions}};
# Check if the term exists in the thesaurus
if ($obj->isDefined($term)) {
$term = $obj->{defined}{lc($term)};
my ($c,$t,$tterm);
my $link = $term;
$link =~ s/\s/+/g;
$tterm = $obj->_translateTerm($term,$language);
$t = b(a({href=>"$script?t=$link"},$tterm)). br . "<small><dl><dd>\n";
for $c (sort keys %{$thesaurus{$term}}) {
$c = uc($c);
next if exists($rels2hide{$c});
# jump if it is the name relation :)
next if ($c eq "_NAME_");
if (exists($obj->{externals}{$c})) {
# put an external relation
my $desc = $obj->getDescription($c,$language);
if ($desc eq "...") {
$t .= join("<br/>\n", map { div($_) } @{$thesaurus{$term}{$c}});
} else {
$t .= join("<br/>\n", map { b($desc)." $_" } @{$thesaurus{$term}{$c}});
}
} elsif (exists($obj->{languages}{$c})) {
# Jump the language relations
} else {
my $desc = $obj->getDescription($c,$language);
if ($desc eq "...") {
$t.= b($c)." ";
} else {
$t.= b($desc)." ";
}
my @termos = sort {lc($a)cmp lc($b)} ( @{$thesaurus{$term}{$c}} );
if (defined($limit) && $limit!=0 && @termos > $limit) {
while(@termos > $limit) { pop @termos; }
push @termos, "...";
}
if (defined($script)) {
@termos = map {my $link = $_;
if ($link eq "...") {
$link
} else {
$_ = $obj->_translateTerm($_,$language) || $_;
$link =~s/\s/+/g;
a({href=>"$script?t=$link"},$_)
}
} @termos;
}
$t.= join(", ", @termos) . br."\n";
}
}
$t.= "</dd></dl></small>\n";
return $t;
} else {
print STDERR "Can't find term '$term'\n";
return qq/Term $term is not defined\n/;
}
}
sub getdefinition { getDefinition(@_) }
sub getDefinition {
my $self = shift;
my $term = _term_normalize(lc(shift));
if ($self->isDefined($term)) {
return $self->{defined}{$term};
} else {
return $term;
}
}
###
#
#
sub isDefined {
my $obj = shift;
my $term = _term_normalize(lc(shift));
return defined($obj->{defined}{$term});
}
###
#
#
sub _definition {
my ($self,$term) = @_;
return $self->{defined}{_term_normalize(lc($term))};
}
###
#
#
sub complete {
my $obj = shift;
my $thesaurus = $obj->{$obj->{baselang}};
my %inverses = %{$obj->{inverses}};
my ($termo,$classe);
# para cada termo
for $termo (keys %$thesaurus) {
# $obj->{defined}{lc($termo)} = $termo;
# e para cada classe,
for $classe (keys %{$thesaurus->{$termo}}) {
# verificar se existem duplicados...
if (ref($thesaurus->{$termo}{$classe}) eq "ARRAY") {
my %h;
@h{@{$thesaurus->{$termo}{$classe}}} = @{$thesaurus->{$termo}{$classe}};
$thesaurus->{$termo}{$classe} = [ keys %h ];
# se tiver inverso,
if (defined($inverses{$classe})) {
# completar cada um dos termos relacionados
for (@{$thesaurus->{$termo}{$classe}}) {
# %thesaurus = _completa($obj,$_,$inverses{$classe},$termo,%thesaurus);
_completa($obj,$_,$inverses{$classe},$termo,$thesaurus);
}
}
}
}
}
$obj -> {$obj->{baselang}} = $thesaurus;
return $obj;
}
###
#
#
sub _completa {
## Yeah, obj and thesaurus can be redundanct, but it's better this way...
my ($obj,$palavra,$classe,$termo,$thesaurus) = @_;
my $t;
# Ver se existe a palavra e a classe no thesaurus
if ($obj->isDefined($palavra)) {
$t = $obj->{defined}{lc($palavra)};
if (defined($thesaurus->{$t}{$classe})) {
# se existe, o array palavras fica com os termos (para ver se ja' existe)
my @palavras = @{$thesaurus->{$t}{$classe}};
# ver se ja' existe
for (@palavras) {
return $thesaurus if (lc eq lc($termo));
}
}
# nao existe: aumentar
push @{$thesaurus->{$t}{$classe}}, $obj->{defined}{lc($termo)};
} else {
# nao existe: aumentar
$thesaurus->{$palavra}{_NAME_} = $palavra unless
defined($thesaurus->{$palavra}) && defined($thesaurus->{$palavra}{_NAME_});
$obj->{defined}{lc($palavra)} = $palavra;
push @{$thesaurus->{$palavra}{$classe}}, $obj->{defined}{lc($termo)};
}
return $thesaurus;
}
###
#
#
sub addTerm {
my $obj = shift;
my $term = _term_normalize(shift);
$obj->{$obj->{baselang}}{$term}{_NAME_} = $term;
$obj->{defined}{lc($term)} = $term;
}
sub hasRelation {
my ($obj, $term, $rel, $rterm) = @_;
$rel = uc($rel);
return 0 unless $obj->isDefined($term); # Check if term exists
$term = $obj->_definition($term);
my $has = 0;
if ($rterm) {
if (exists($obj->{externals}{$rel})) {
$has = 1 if (grep { $_ eq $rterm } @{$obj->{$obj->{baselang}}{$term}{$rel}});
} else {
$rterm = _term_normalize($rterm);
$has = 1 if (grep { $_ eq $rterm} @{$obj->{$obj->{baselang}}{$term}{$rel}});
}
} else {
$has = 1 if exists($obj->{$obj->{baselang}}{$term}{$rel});
}
return $has;
}
###
#
#
sub addRelation {
my ($obj, $term, $rel, @terms) = @_;
$rel = uc($rel);
$obj->{descriptions}{$rel} = "..."
unless defined($obj->{descriptions}{$rel});
unless ($obj->isDefined($term)) {
$obj->{defined}{lc(_term_normalize($term))} = _term_normalize($term);
}
$term = $obj->_definition($term);
if (exists($obj->{externals}{$rel})) {
push @{$obj->{$obj->{baselang}}{$term}{$rel}}, @terms;
} else {
push @{$obj->{$obj->{baselang}}{$term}{$rel}},
map {_term_normalize($_)} @terms;
for (@terms) {
$obj->addTerm($_) unless $obj->isDefined($_);
}
}
}
###
#
#
sub deleteRelation {
my ($self, $term, $rel, @terms) = @_;
$rel = uc($rel);
if (@terms) {
for my $oterm (@terms) {
$self->_deleteRelation($term, $rel, $oterm);
## Se existe inversa, do the same shit
if (exists $self->{inverses}{$rel}) {
$self->_deleteRelation($oterm, $self->{inverses}{$rel}, $term);
}
}
} else {
if (exists($self->{externals}{$rel})) {
$self->_deleteRelation($term, $rel);
} else {
@terms = $self->terms($term,$rel);
return unless @terms;
$self->deleteRelation($term, $rel, @terms);
}
}
}
###
#
#
sub _deleteRelation {
my ($obj, $term, $rel, $oterm) = @_;
# return if the term is not defined
return unless $obj->isDefined($term);
$term = $obj->_definition($term);
if ($oterm) {
# if we have a full relation (term,rel,term), then it is not an external relation
return if exists($obj->{externals}{$rel});
$oterm = _term_normalize($oterm);
$obj->{$obj->{baselang}}{$term}{$rel} = [ grep { $_ ne $oterm } @{$obj->{$obj->{baselang}}{$term}{$rel}}];
} else {
delete($obj->{$obj->{baselang}}{$term}{$rel});
}
}
###
#
#
sub deleteTerm {
my $obj = shift;
my $term = _term_normalize(shift);
my $t2=$term;
$term = $obj->_definition($term);
my ($t,$c);
warn("'$t2' => '$term'\n") && return unless defined($term);
if (defined($obj->{$obj->{baselang}}{$term})){
delete($obj->{$obj->{baselang}}{$term});
delete($obj->{defined}{lc($term)});
}
else {warn ("'$term' not found...\n");}
foreach $t (keys %{$obj->{$obj->{baselang}}}) {
foreach $c (keys %{$obj->{$obj->{baselang}}{$t}}) {
my @a = ();
if ( ref($obj->{$obj->{baselang}}{$t}{$c}) eq "ARRAY") {
foreach (@{$obj->{$obj->{baselang}}{$t}{$c}}) {
push(@a,$_) unless($_ eq $term);
}
$obj->{$obj->{baselang}}{$t}{$c}=\@a;
}
}
}
}
###
#
#
sub downtr {
my $self = shift;
my $handler = shift;
die("bad use of downtr method; args should be: hashRef, termlist")
unless(ref($handler) eq "HASH");
my @tl = @_ ; #lc(shift);
@tl = (sort
{lc($a) cmp lc($b)}
keys %{$self->{$self->{baselang}}}) unless (@tl);
my $r2 = ""; #final result
my $c;
for my $t (@tl){
my $r = "";
$term = $t;
if (defined( $handler->{"_NAME_"})){
$r .= &{$handler->{"_NAME_"}};
}
my @rels = (keys %{$self->{$self->{baselang}}->{$t}});
my %rels = ();
@rels{@rels} = @rels;
my $order = defined $handler->{-order} ? $handler->{-order} :
( defined $self->{order} ? $self->{order} : []);
delete(@rels{@$order});
@rels = ( @{$order}, (sort keys(%rels) ));
for $c (@rels) {
next unless $self->{$self->{baselang}}{$t}{$c};
next if ($c eq "_NAME_");
# Set environment variables to downtr function
#
# rel...
#
$rel = $c;
#
# List of terms...
#
if ($self->{languages}->{$rel}) {
@terms = ( $self->{$self->{baselang}}{$t}{$rel} );
} else {
@terms = @{$self->{$self->{baselang}}{$t}{$rel}};
}
#
# Current term...
#
$term = $t;
if (exists($handler->{$rel})) {
$r .= &{$handler->{$rel}} // "";
} elsif (exists($handler->{-default})) {
$r .= &{$handler->{-default}} // "";
} else {
$r .= "\n$rel\t".join(", ",@terms);
}
}
for($r){
if (exists($handler->{'-eachTerm'})) {
my $ans = &{$handler->{'-eachTerm'}};
$r2 .= ($ans)?$ans:"";
} else {
$r2 .= $_;
}
}
}
if (defined($handler->{-end})) {
for($r2){
$_ = &{$handler->{'-end'}}
}
}
$r2;
}
###
#
#
sub tc{
my ($self,$term,@relations) = @_;
my %x = _tc_aux($self, $term, {}, @relations);
return (keys %x);
}
###
#
#
sub toHash {
my ($self, $rel) = @_;
$rel //= "NT";
$rel = [$rel] unless ref($rel);
my $top = $self->topName;
return +{ $top => $self->_toHash($top, $rel, [$top]) };
}
sub _toHash {
my ($self, $term, $rel, $stack) = @_;
my $h = $self->depth_first($term, 1, @$rel);
if (keys %$h) {
for (keys %$h) {
$h->{$_} = $self->_toHash($_, $rel, [@$stack, $_]);
}
} else {
$h = join("::", @$stack);
}
return $h;
}
##
#
#
sub toJson {
my ($self, $rel) = @_;
$rel //= "NT";
$rel = [$rel] unless ref($rel);
my $top = $self->topName;
$self->_toJson($top, $rel);
}
sub _toJson {
my ($self, $term, $rel) = @_;
my $h = $self->depth_first($term, 1, @$rel);
my $json = "{ \"data\": \"$term\", \"attr\":{id:\"$term\"}";
if (keys %$h) {
$json .= ", \"children\": [";
$json .= join(", ", map { $self->_toJson($_, $rel) } keys %$h);
$json .= "]"
}
$json .= "}";
}
###
#
#
sub _tc_aux {
my ($self,$term,$vis,@relat) = @_;
$term = $self->getdefinition($term);
my %r = ( $term => 1 );
for ($self->terms($term,@relat)) {
next if exists $vis->{$_};
$vis->{$_}++;
%r = (%r, $_ => 1, _tc_aux($self,$_,@relat)) unless $r{$_};
}
return %r;
}
###
#
#
sub _term_normalize {
my $t = shift;
$t =~ s/^\s*(.*?)\s*$/$1/;
$t =~ s/\s\s+/ /g;
return $t;
}
sub capitalize {
my $op = shift;
my $text = shift;
if ($op) {
$text = join(" ",map {ucfirst} split /\s+/, $text);
}
return $text;
}
# remove duplicados de uma lista
sub _set_of {
my %set = ();
$set{$_} = 1 for @_;
return keys %set;
}
1;
__END__
=encoding UTF-8
=head1 NAME
Biblio::Thesaurus - Perl extension for managing ISO thesaurus
=head1 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( ...)
=head1 ABSTRACT
This module provides transparent methods to maintain Thesaurus files.
The module uses a subset from ISO 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.
=head1 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 ISO 2788 which defines some standard
features to be found on thesaurus files. This ISO 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 ISO or not ISO thesaurus files.
=head1 File Structure
Thesaurus used with this module are standard ASCII 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
(B<#>) 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 (B<%>). 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_>). It should be
used when you want a top tree for thesaurus navigation. So,
we normally define the C<_top_> term with the more interesting
terms to be navigated.
The B<ISO> subset used are:
=over 4
=item B<TT> - Top Term
The broadest term we can define about the current term.
=item B<NT> - Narrower Term
Terms more specific than current term.
=item B<BT> - Broader Term
More generic terms than current term.
=item B<USE> - Synonym
Another chances when finding a Synonym.
=item B<UF> - Quasi-Synonym
Terms that are no synonyms of current term but can be used,
sometimes with that meaning.
=item B<RT> - Related Term
Related term that can't be inserted on any other category.
=item B<SN> - Scope Note
Text. Note of context of the current term. Use for definitions or
comments about the scope you are using that term.
=back
=head2 Processing Instructions
Processing instructions, as said before, are written on a line starting
with the percent sign. Current commands are:
=over 4
=item B<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 ...
=item B<enc>oding
This command defines the encoding used in the thesaurus file.
Example:
%enc utf8
=item B<inv>erse
This command defines the mathematic inverse of the relation. That
is, if you define C<inverse A B> and you know that C<foo> is
related by C<A> with C<bar>, then, C<bar> is related by C<B>
with C<foo>.
Example:
%inv BT NT
%inverse UF USE
=item B<desc>ription
This command defines a description for some relation class. These
descriptions are used when outputting thesaurus on HTML.
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>
command with the language in from of it:
%desc[PT] SN Nota de Contexto
%description[PT] IOF Instancia de
=item B<ext>ernals
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.
=item B<lang>uages
This other command permits the construction of a multilingual thesaurus. TO
specify languages classifiers (like PT, EN, FR, 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 B<description>
command, so, you could append:
%description PT Portuguese
%description EN English
%description FR French
=item B<baselang>uage
This one makes it possible to explicitly name the base language for the
thesaurus. This command should be used with the C<description> 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
=back
=head2 I18N
Internationalization functions, C<languages> and C<setLanguage> 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.
=head1 API
This module uses a perl object oriented model programming, so you must
create an object with one of the C<thesaurusNew>, C<thesaurusLoad> or
C<thesaurusRetrieve> commands. Next commands should be called using
the B<OO> fashion.
=head1 Constructors
=head2 thesaurusNew
To create an empty thesaurus object. The returned newly created object
contains the inversion properties from the ISO classes and some stub
descriptions for the same classes.
=head2 thesaurusLoad
To use the C<thesaurusLoad> function, you must supply a file name.
This file name should correspond to the ISO ASCII 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 ISO classes, they are added.
Also,
$obj = thesaurusLoad({ completed => 1}, 'iso-file');
can be used to say that the thesaurus needs B<not> to be complete after load.
=head2 thesaurusMultiLoad
You can join different thesaurus ISO files using this function:
$obj = thesaurusMultiLoad('iso-file1','iso-file2',...);
=head2 appendThesaurus
You can also append a thesaurus ISO (or another thesaurus object) to a
loaded thesaurus. For that, use one of:
$obj->appendThesaurus("iso-file");
$obj->appendThesaurus( $other_thesaurus_object );
=head2 thesaurusLoadM
This method is used to load a thesaurus on the meta-thesaurus
format. This is still under development.
=head2 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> function.
=head1 Methods
=head2 save
This method dumps the object on an ISO ASCII file. Note that the
sequence C<thesaurusLoad>, C<save> 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.
=head2 meta2str
This method returns the ISO ascii description of the metadata.
=head2 storeOn
This method saves the thesaurus object in Storable format. You should
use it when you want to load with the C<thesaurusRetrieve> function.
=head2 addTerm
You can add terms definitions using the perl API. This method adds a
term on the thesaurus. Note that if that term already exists, all its
relations will be deleted.
=head2 all_terms
Returns an array with all terms for the thesaurus base language.
NOTE: this function is deprecated. Use allTerms instead.
=head2 allTerms
Returns an array with all terms for the thesaurus base language.
=head2 topName
Returns the term in the top of the thesaurus, or defined a new one if
called with an argument.
=head2 top_name
Deprecated. See C<<topName>>;
=head2 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.
=head2 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 "X" with anything:
if ($obj->hasRelation('Animal','SN')) { ... }
=head2 deleteRelation
$obj->deleteRelation('Animal','NT','cat','dog','cow','camel');
=head2 deleteTerm
Use this method to remove all references of the term supplied. Note
that B<all> references will be deleted.
=head2 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 ISO ones)
or to define a new class.
=head2 isDefined
Use this method to check if a term exists in the thesaurus.
=head2 setExternal
Use this method to define that a relation is "extern".
=head2 isExternal
Use this method to check if a relation is "extern".
=head2 isLanguage
Use this method to check if a relation is a Language.
=head2 getdefinition
Deprecated. Use C<<getDefinition>
=head2 getDefinition
Returns the definition for a term. The definition is a feature
structure containing the term information.
=head2 getDescription
Given a relation name and a language (or the default will be used), it
returns the description for that relation.
=head2 relations
Call this method with a term, and it returns a list of the relations
defined for that term.
=head2 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.
=head2 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 CGI.
=head2 navigate
This function is a some what type of CGI included on a object
method. You must supply an associative array of CGI parameters. This
method prints an HTML thesaurus for Web Navigation.
The typical thesaurus navigation CGI 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 'NT' and 'USE'
relations, and that it should use the English language.
These options include:
=over 4
=item capitalize
try to capitalize terms when they are the title of the page.
=item expand
a reference to a list of relations that should be expanded at first
level; Defaults to the empty list.
=item title
can be C<yes> or C<no>. If it is C<no>, the current term will not be
shown as a title; Defaults to C<yes>.
=item scriptname
the name of the script the links should point on. Defaults to current
page name.
=item 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 'LEN' relation when
using Library::Simple.
=item level2size
the number of terms to be shown on each second level relation;
Defaults to 0 (all terms).
=item level2hide
a reference to a list of relations to do not show on the second
level. Defaults to the empty list.
=item topic_name
the name of the topic CGI parameter (default: "t")
=back
=head2 dumpHTML
This method returns a big string containing all the thesaurus in
HTML. It is mainly used for debug.
=head2 getHTMLTop
This method returns the HTML 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.
=head2 complete
This function completes the thesaurus based on the invertibility
properties. This operation is only needed when adding terms and
relations by this API. Whenever the system loads a thesaurus ISO file,
it is completed.
=head2 baselang
Use this method to set or retrieve the base language of the thesaurus.
If no baselang is provided, the value "_" is returned
=head2 downtr
The C<downtr> 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 C<$term>, C<$rel>, C<@terms>.
The handler can have three special functions:
C<-default> (default handler for relations that don't have a defined function
in the handler),
C<-eachTerm> executed with each term output (received as C<$_>), and
C<-end> executed over the output of the the other functions (received as C<$_>),
If a C<-order> 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)
=head2 depth_first
The C<depth_first> method is used to get the list of terms (in fact the
tree of terms) related with C<$term> by relations C<@r> up to the level C<$lev>
$hashref = $the->depth_first($term ,$lev, @r)
$hashref = $the->depth_first("frog", 2, "NT","UF")
C<$lev> should be an integer greater then 0.
=head2 tc transitive closure
The C<tc> method is used to eval the transitive closure of the relations
C<@r> starting from a term C<$term>
$the->tc($term , @r)
$the->tc("frog", "NT","UF")
=head2 terms
The C<terms> method is used to get all the terms related by relations C<@r>
with C<$term>
$the->terms($term , @r);
$the->terms("frog", "NT", "UF");
=head2 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{""}})
=head2 toXml
This method writes a thesaurus in XML 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();
=head2 toJson
Returns a JSON tree based on NT relation. Other relation can be
supplied as an argument. Future versions might include language
selection.
print $thesaurus->toJson();
=head2 toHash
Returns a Hash reference with a tree based on NT relation. Other
relation can be supplied as an argument. Future versions might include
language selection.
print $thesaurus->toHash();
=head1 AUTHOR
Alberto Simoes, <albie@alfarrabio.di.uminho.pt>
José Joao Almeida, <jj@di.uminho.pt>
Sara Correia, <sara.correia@portugalmail.com>
This module is included in the Natura project. You can visit it at
http://natura.di.uminho.pt, and access the SVN tree.
=head1 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.
=head1 SEE ALSO
The example thesaurus file (C<examples/thesaurus>),
Manpages:
Biblio::WebPortal(3)
Biblio::Catalog(3)
Biblio::Catalog::Bibtex(3)
perl(1) manpages.
=cut
__DATA__
=head2 loading from Iso 2788
=head2 building a thesaurus with internal constructors
=head2 writing a thesaurus in another format
|