| 1 | # Copyright (c) 2026 Philipp Schafft | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: module to interact with the words of Toki Pona | ||
| 6 | |||
| 7 | |||
| 8 | package Lingua::TokiPona::Word; | ||
| 9 | |||
| 10 | use v5.20; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use Data::Identifier v0.34; | ||
| 16 | use Data::Identifier::Generate; | ||
| 17 | use Data::Displaycolour; | ||
| 18 | |||
| 19 | our $VERSION = v0.03; | ||
| 20 | |||
| 21 | use parent qw(Data::Identifier::Interface::Known Lingua::Generic::Interface::Word); | ||
| 22 | |||
| 23 | use constant { | ||
| 24 | WK_WORD_GENERATOR => Data::Identifier->new(uuid => '45823114-5f38-47d8-a749-2e7f3ec819c3', tagname => 'toki-pona-word-generator')->register, | ||
| 25 | WK_WORD_NAMESPACE => Data::Identifier->new(uuid => '8d043e5e-b7da-4a37-a87a-26bb361288a1', tagname => 'toki-pona-word-namespace')->register, | ||
| 26 | WK_WORD_TYPE => Data::Identifier->new(uuid => 'baf7bee1-0889-4a84-afc2-4904857d0571', tagname => 'toki-pona-word')->register, | ||
| 27 | WK_LT_TOK => Data::Identifier->new(uuid => 'f21986c8-baa1-5b7e-b357-2a76285c4778', displayname => 'Toki Pona', request => 'tok')->register, | ||
| 28 | }; | ||
| 29 | |||
| 30 | my %_words; | ||
| 31 | my %_by_ise; | ||
| 32 | my %_by_ucsur; | ||
| 33 | |||
| 34 | __PACKAGE__->_new($_) foreach ( | ||
| 35 | # NOTE: Each list only contains the words not already in one of the lists above it. | ||
| 36 | qw( | ||
| 37 | a kin akesi ala alasa ale ali anpa ante anu awen e en esun ijo ike ilo insa jaki jan | ||
| 38 | jelo jo kala kalama kama kasi ken kepeken kili kiwen ko kon kule kulupu kute la lape | ||
| 39 | laso lawa len lete li lili linja lipu loje lon luka lukin oko lupa ma mama mani meli | ||
| 40 | mi mije moku moli monsi mu mun musi mute nanpa nasa nasin nena ni nimi noka o olin ona | ||
| 41 | open pakala pali palisa pan pana pi pilin pimeja pini pipi poka poki pona pu sama seli | ||
| 42 | selo seme sewi sijelo sike sin namako sina sinpin sitelen sona soweli suli suno supa suwi | ||
| 43 | tan taso tawa telo tenpo toki tomo tu unpa uta utala walo wan waso wawa weka wile | ||
| 44 | ), | ||
| 45 | qw(kijetesantakalu kin kipisi ku leko misikeke monsuta n soko tonsi), # Common | ||
| 46 | qw(epiku jasima lanpan linluwi majuna meso nimisin su), # Uncommon | ||
| 47 | qw( | ||
| 48 | apeja isipin jami kamalawala kapesi kiki kokosila konwe kulijo melome mijomi misa nja | ||
| 49 | ojuta oke omekapo owe pake penpo pika po powe puwa san soto sutopatikuna taki te teje | ||
| 50 | to unu usawi wa wasoweli wekama wuwojiti yupekosi | ||
| 51 | ), # Obscure | ||
| 52 | qw(suke toma), # Typo | ||
| 53 | qw(ete ewe kan ke kese kuntu likujo loka mulapisu neja pata peto polinpin pomotolo samu tuli umesu waleja), # nimi ku lili | ||
| 54 | qw(ju lu nu u), # Reserved words | ||
| 55 | ); | ||
| 56 | |||
| 57 | __PACKAGE__->new(string => $_)->{class}{stopword} = 1 foreach qw(a kin anu e en la li o pi seme); | ||
| 58 | |||
| 59 | __PACKAGE__->new(string => 'loje' )->Data::Displaycolour::mark(for => Data::Identifier->new(uuid => 'c9ec3bea-558e-4992-9b76-91f128b6cf29')); # red | ||
| 60 | __PACKAGE__->new(string => 'jelo' )->Data::Displaycolour::mark(for => Data::Identifier->new(uuid => '2892c143-2ae7-48f1-95f4-279e059e7fc3')); # yellow | ||
| 61 | __PACKAGE__->new(string => 'laso' )->Data::Displaycolour::mark(for => Data::Identifier->new(uuid => 'abcbf48d-c302-4be1-8c5c-a8de4471bcbb')); # cyan | ||
| 62 | __PACKAGE__->new(string => 'walo' )->Data::Displaycolour::mark(for => Data::Identifier->new(uuid => '1a2c23fa-2321-47ce-bf4f-5f08934502de')); # white | ||
| 63 | __PACKAGE__->new(string => 'pimeja')->Data::Displaycolour::mark(for => Data::Identifier->new(uuid => 'fade296d-c34f-4ded-abd5-d9adaf37c284')); # black | ||
| 64 | |||
| 65 | { | ||
| 66 | my %ucsur = ( | ||
| 67 | 0xF1900 => 'SITELEN PONA IDEOGRAPH A', | ||
| 68 | 0xF1901 => 'SITELEN PONA IDEOGRAPH AKESI', | ||
| 69 | 0xF1902 => 'SITELEN PONA IDEOGRAPH ALA', | ||
| 70 | 0xF1903 => 'SITELEN PONA IDEOGRAPH ALASA', | ||
| 71 | 0xF1904 => 'SITELEN PONA IDEOGRAPH ALE', | ||
| 72 | 0xF1905 => 'SITELEN PONA IDEOGRAPH ANPA', | ||
| 73 | 0xF1906 => 'SITELEN PONA IDEOGRAPH ANTE', | ||
| 74 | 0xF1907 => 'SITELEN PONA IDEOGRAPH ANU', | ||
| 75 | 0xF1908 => 'SITELEN PONA IDEOGRAPH AWEN', | ||
| 76 | 0xF1909 => 'SITELEN PONA IDEOGRAPH E', | ||
| 77 | 0xF190A => 'SITELEN PONA IDEOGRAPH EN', | ||
| 78 | 0xF190B => 'SITELEN PONA IDEOGRAPH ESUN', | ||
| 79 | 0xF190C => 'SITELEN PONA IDEOGRAPH IJO', | ||
| 80 | 0xF190D => 'SITELEN PONA IDEOGRAPH IKE', | ||
| 81 | 0xF190E => 'SITELEN PONA IDEOGRAPH ILO', | ||
| 82 | 0xF190F => 'SITELEN PONA IDEOGRAPH INSA', | ||
| 83 | 0xF1910 => 'SITELEN PONA IDEOGRAPH JAKI', | ||
| 84 | 0xF1911 => 'SITELEN PONA IDEOGRAPH JAN', | ||
| 85 | 0xF1912 => 'SITELEN PONA IDEOGRAPH JELO', | ||
| 86 | 0xF1913 => 'SITELEN PONA IDEOGRAPH JO', | ||
| 87 | 0xF1914 => 'SITELEN PONA IDEOGRAPH KALA', | ||
| 88 | 0xF1915 => 'SITELEN PONA IDEOGRAPH KALAMA', | ||
| 89 | 0xF1916 => 'SITELEN PONA IDEOGRAPH KAMA', | ||
| 90 | 0xF1917 => 'SITELEN PONA IDEOGRAPH KASI', | ||
| 91 | 0xF1918 => 'SITELEN PONA IDEOGRAPH KEN', | ||
| 92 | 0xF1919 => 'SITELEN PONA IDEOGRAPH KEPEKEN', | ||
| 93 | 0xF191A => 'SITELEN PONA IDEOGRAPH KILI', | ||
| 94 | 0xF191B => 'SITELEN PONA IDEOGRAPH KIWEN', | ||
| 95 | 0xF191C => 'SITELEN PONA IDEOGRAPH KO', | ||
| 96 | 0xF191D => 'SITELEN PONA IDEOGRAPH KON', | ||
| 97 | 0xF191E => 'SITELEN PONA IDEOGRAPH KULE', | ||
| 98 | 0xF191F => 'SITELEN PONA IDEOGRAPH KULUPU', | ||
| 99 | 0xF1920 => 'SITELEN PONA IDEOGRAPH KUTE', | ||
| 100 | 0xF1921 => 'SITELEN PONA IDEOGRAPH LA', | ||
| 101 | 0xF1922 => 'SITELEN PONA IDEOGRAPH LAPE', | ||
| 102 | 0xF1923 => 'SITELEN PONA IDEOGRAPH LASO', | ||
| 103 | 0xF1924 => 'SITELEN PONA IDEOGRAPH LAWA', | ||
| 104 | 0xF1925 => 'SITELEN PONA IDEOGRAPH LEN', | ||
| 105 | 0xF1926 => 'SITELEN PONA IDEOGRAPH LETE', | ||
| 106 | 0xF1927 => 'SITELEN PONA IDEOGRAPH LI', | ||
| 107 | 0xF1928 => 'SITELEN PONA IDEOGRAPH LILI', | ||
| 108 | 0xF1929 => 'SITELEN PONA IDEOGRAPH LINJA', | ||
| 109 | 0xF192A => 'SITELEN PONA IDEOGRAPH LIPU', | ||
| 110 | 0xF192B => 'SITELEN PONA IDEOGRAPH LOJE', | ||
| 111 | 0xF192C => 'SITELEN PONA IDEOGRAPH LON', | ||
| 112 | 0xF192D => 'SITELEN PONA IDEOGRAPH LUKA', | ||
| 113 | 0xF192E => 'SITELEN PONA IDEOGRAPH LUKIN', | ||
| 114 | 0xF192F => 'SITELEN PONA IDEOGRAPH LUPA', | ||
| 115 | 0xF1930 => 'SITELEN PONA IDEOGRAPH MA', | ||
| 116 | 0xF1931 => 'SITELEN PONA IDEOGRAPH MAMA', | ||
| 117 | 0xF1932 => 'SITELEN PONA IDEOGRAPH MANI', | ||
| 118 | 0xF1933 => 'SITELEN PONA IDEOGRAPH MELI', | ||
| 119 | 0xF1934 => 'SITELEN PONA IDEOGRAPH MI', | ||
| 120 | 0xF1935 => 'SITELEN PONA IDEOGRAPH MIJE', | ||
| 121 | 0xF1936 => 'SITELEN PONA IDEOGRAPH MOKU', | ||
| 122 | 0xF1937 => 'SITELEN PONA IDEOGRAPH MOLI', | ||
| 123 | 0xF1938 => 'SITELEN PONA IDEOGRAPH MONSI', | ||
| 124 | 0xF1939 => 'SITELEN PONA IDEOGRAPH MU', | ||
| 125 | 0xF193A => 'SITELEN PONA IDEOGRAPH MUN', | ||
| 126 | 0xF193B => 'SITELEN PONA IDEOGRAPH MUSI', | ||
| 127 | 0xF193C => 'SITELEN PONA IDEOGRAPH MUTE', | ||
| 128 | 0xF193D => 'SITELEN PONA IDEOGRAPH NANPA', | ||
| 129 | 0xF193E => 'SITELEN PONA IDEOGRAPH NASA', | ||
| 130 | 0xF193F => 'SITELEN PONA IDEOGRAPH NASIN', | ||
| 131 | 0xF1940 => 'SITELEN PONA IDEOGRAPH NENA', | ||
| 132 | 0xF1941 => 'SITELEN PONA IDEOGRAPH NI', | ||
| 133 | 0xF1942 => 'SITELEN PONA IDEOGRAPH NIMI', | ||
| 134 | 0xF1943 => 'SITELEN PONA IDEOGRAPH NOKA', | ||
| 135 | 0xF1944 => 'SITELEN PONA IDEOGRAPH O', | ||
| 136 | 0xF1945 => 'SITELEN PONA IDEOGRAPH OLIN', | ||
| 137 | 0xF1946 => 'SITELEN PONA IDEOGRAPH ONA', | ||
| 138 | 0xF1947 => 'SITELEN PONA IDEOGRAPH OPEN', | ||
| 139 | 0xF1948 => 'SITELEN PONA IDEOGRAPH PAKALA', | ||
| 140 | 0xF1949 => 'SITELEN PONA IDEOGRAPH PALI', | ||
| 141 | 0xF194A => 'SITELEN PONA IDEOGRAPH PALISA', | ||
| 142 | 0xF194B => 'SITELEN PONA IDEOGRAPH PAN', | ||
| 143 | 0xF194C => 'SITELEN PONA IDEOGRAPH PANA', | ||
| 144 | 0xF194D => 'SITELEN PONA IDEOGRAPH PI', | ||
| 145 | 0xF194E => 'SITELEN PONA IDEOGRAPH PILIN', | ||
| 146 | 0xF194F => 'SITELEN PONA IDEOGRAPH PIMEJA', | ||
| 147 | 0xF1950 => 'SITELEN PONA IDEOGRAPH PINI', | ||
| 148 | 0xF1951 => 'SITELEN PONA IDEOGRAPH PIPI', | ||
| 149 | 0xF1952 => 'SITELEN PONA IDEOGRAPH POKA', | ||
| 150 | 0xF1953 => 'SITELEN PONA IDEOGRAPH POKI', | ||
| 151 | 0xF1954 => 'SITELEN PONA IDEOGRAPH PONA', | ||
| 152 | 0xF1955 => 'SITELEN PONA IDEOGRAPH PU', | ||
| 153 | 0xF1956 => 'SITELEN PONA IDEOGRAPH SAMA', | ||
| 154 | 0xF1957 => 'SITELEN PONA IDEOGRAPH SELI', | ||
| 155 | 0xF1958 => 'SITELEN PONA IDEOGRAPH SELO', | ||
| 156 | 0xF1959 => 'SITELEN PONA IDEOGRAPH SEME', | ||
| 157 | 0xF195A => 'SITELEN PONA IDEOGRAPH SEWI', | ||
| 158 | 0xF195B => 'SITELEN PONA IDEOGRAPH SIJELO', | ||
| 159 | 0xF195C => 'SITELEN PONA IDEOGRAPH SIKE', | ||
| 160 | 0xF195D => 'SITELEN PONA IDEOGRAPH SIN', | ||
| 161 | 0xF195E => 'SITELEN PONA IDEOGRAPH SINA', | ||
| 162 | 0xF195F => 'SITELEN PONA IDEOGRAPH SINPIN', | ||
| 163 | 0xF1960 => 'SITELEN PONA IDEOGRAPH SITELEN', | ||
| 164 | 0xF1961 => 'SITELEN PONA IDEOGRAPH SONA', | ||
| 165 | 0xF1962 => 'SITELEN PONA IDEOGRAPH SOWELI', | ||
| 166 | 0xF1963 => 'SITELEN PONA IDEOGRAPH SULI', | ||
| 167 | 0xF1964 => 'SITELEN PONA IDEOGRAPH SUNO', | ||
| 168 | 0xF1965 => 'SITELEN PONA IDEOGRAPH SUPA', | ||
| 169 | 0xF1966 => 'SITELEN PONA IDEOGRAPH SUWI', | ||
| 170 | 0xF1967 => 'SITELEN PONA IDEOGRAPH TAN', | ||
| 171 | 0xF1968 => 'SITELEN PONA IDEOGRAPH TASO', | ||
| 172 | 0xF1969 => 'SITELEN PONA IDEOGRAPH TAWA', | ||
| 173 | 0xF196A => 'SITELEN PONA IDEOGRAPH TELO', | ||
| 174 | 0xF196B => 'SITELEN PONA IDEOGRAPH TENPO', | ||
| 175 | 0xF196C => 'SITELEN PONA IDEOGRAPH TOKI', | ||
| 176 | 0xF196D => 'SITELEN PONA IDEOGRAPH TOMO', | ||
| 177 | 0xF196E => 'SITELEN PONA IDEOGRAPH TU', | ||
| 178 | 0xF196F => 'SITELEN PONA IDEOGRAPH UNPA', | ||
| 179 | 0xF1970 => 'SITELEN PONA IDEOGRAPH UTA', | ||
| 180 | 0xF1971 => 'SITELEN PONA IDEOGRAPH UTALA', | ||
| 181 | 0xF1972 => 'SITELEN PONA IDEOGRAPH WALO', | ||
| 182 | 0xF1973 => 'SITELEN PONA IDEOGRAPH WAN', | ||
| 183 | 0xF1974 => 'SITELEN PONA IDEOGRAPH WASO', | ||
| 184 | 0xF1975 => 'SITELEN PONA IDEOGRAPH WAWA', | ||
| 185 | 0xF1976 => 'SITELEN PONA IDEOGRAPH WEKA', | ||
| 186 | 0xF1977 => 'SITELEN PONA IDEOGRAPH WILE', | ||
| 187 | 0xF1978 => 'SITELEN PONA IDEOGRAPH NAMAKO', | ||
| 188 | 0xF1979 => 'SITELEN PONA IDEOGRAPH KIN', | ||
| 189 | 0xF197A => 'SITELEN PONA IDEOGRAPH OKO', | ||
| 190 | 0xF197B => 'SITELEN PONA IDEOGRAPH KIPISI', | ||
| 191 | 0xF197C => 'SITELEN PONA IDEOGRAPH LEKO', | ||
| 192 | 0xF197D => 'SITELEN PONA IDEOGRAPH MONSUTA', | ||
| 193 | 0xF197E => 'SITELEN PONA IDEOGRAPH TONSI', | ||
| 194 | 0xF197F => 'SITELEN PONA IDEOGRAPH JASIMA', | ||
| 195 | 0xF1980 => 'SITELEN PONA IDEOGRAPH KIJETESANTAKALU', | ||
| 196 | 0xF1981 => 'SITELEN PONA IDEOGRAPH SOKO', | ||
| 197 | 0xF1982 => 'SITELEN PONA IDEOGRAPH MESO', | ||
| 198 | 0xF1983 => 'SITELEN PONA IDEOGRAPH EPIKU', | ||
| 199 | 0xF1984 => 'SITELEN PONA IDEOGRAPH KOKOSILA', | ||
| 200 | 0xF1985 => 'SITELEN PONA IDEOGRAPH LANPAN', | ||
| 201 | 0xF1986 => 'SITELEN PONA IDEOGRAPH N', | ||
| 202 | 0xF1987 => 'SITELEN PONA IDEOGRAPH MISIKEKE', | ||
| 203 | 0xF1988 => 'SITELEN PONA IDEOGRAPH KU', | ||
| 204 | 0xF1989 => 'SITELEN PONA IDEOGRAPH LEFTWARDS NI', | ||
| 205 | 0xF198A => 'SITELEN PONA IDEOGRAPH UPWARDS NI', | ||
| 206 | 0xF198B => 'SITELEN PONA IDEOGRAPH RIGHTWARDS NI', | ||
| 207 | 0xF198C => 'SITELEN PONA IDEOGRAPH ALTERNATE SEWI', | ||
| 208 | 0xF1990 => 'SITELEN PONA START OF CARTOUCHE', | ||
| 209 | 0xF1991 => 'SITELEN PONA END OF CARTOUCHE', | ||
| 210 | 0xF1992 => 'SITELEN PONA COMBINING CARTOUCHE EXTENSION', | ||
| 211 | 0xF1993 => 'SITELEN PONA START OF LONG PI', | ||
| 212 | 0xF1994 => 'SITELEN PONA COMBINING LONG PI EXTENSION', | ||
| 213 | 0xF1995 => 'SITELEN PONA STACKING JOINER', | ||
| 214 | 0xF1996 => 'SITELEN PONA SCALING JOINER', | ||
| 215 | 0xF1997 => 'SITELEN PONA START OF LONG GLYPH', | ||
| 216 | 0xF1998 => 'SITELEN PONA END OF LONG GLYPH', | ||
| 217 | 0xF1999 => 'SITELEN PONA COMBINING LONG GLYPH EXTENSION', | ||
| 218 | 0xF199A => 'SITELEN PONA START OF REVERSE LONG GLYPH', | ||
| 219 | 0xF199B => 'SITELEN PONA END OF REVERSE LONG GLYPH', | ||
| 220 | 0xF199C => 'SITELEN PONA MIDDLE DOT', | ||
| 221 | 0xF199D => 'SITELEN PONA COLON', | ||
| 222 | 0xF199E => 'SITELEN PONA COMBINING TALLY MARK', | ||
| 223 | 0xF19A0 => 'SITELEN PONA IDEOGRAPH PAKE', | ||
| 224 | 0xF19A1 => 'SITELEN PONA IDEOGRAPH APEJA', | ||
| 225 | 0xF19A2 => 'SITELEN PONA IDEOGRAPH MAJUNA', | ||
| 226 | 0xF19A3 => 'SITELEN PONA IDEOGRAPH POWE', | ||
| 227 | 0xF19A4 => 'SITELEN PONA IDEOGRAPH LINLUWI', | ||
| 228 | 0xF19A5 => 'SITELEN PONA IDEOGRAPH KIKI', | ||
| 229 | 0xF19A6 => 'SITELEN PONA IDEOGRAPH SU', | ||
| 230 | 0xF19A7 => 'SITELEN PONA IDEOGRAPH ISIPIN', | ||
| 231 | 0xF19A8 => 'SITELEN PONA IDEOGRAPH KAMALAWALA', | ||
| 232 | 0xF19A9 => 'SITELEN PONA IDEOGRAPH KAPESI', | ||
| 233 | 0xF19AA => 'SITELEN PONA IDEOGRAPH MELOME', | ||
| 234 | 0xF19AB => 'SITELEN PONA IDEOGRAPH MIJOMI', | ||
| 235 | 0xF19AC => 'SITELEN PONA IDEOGRAPH MISA', | ||
| 236 | 0xF19AD => 'SITELEN PONA IDEOGRAPH NIMISIN', | ||
| 237 | 0xF19AE => 'SITELEN PONA IDEOGRAPH NJA', | ||
| 238 | 0xF19AF => 'SITELEN PONA IDEOGRAPH OKE', | ||
| 239 | 0xF19B0 => 'SITELEN PONA IDEOGRAPH OMEKAPO', | ||
| 240 | 0xF19B1 => 'SITELEN PONA IDEOGRAPH PUWA', | ||
| 241 | 0xF19B2 => 'SITELEN PONA IDEOGRAPH SAN', | ||
| 242 | 0xF19B3 => 'SITELEN PONA IDEOGRAPH TAKI', | ||
| 243 | 0xF19B4 => 'SITELEN PONA IDEOGRAPH TE', | ||
| 244 | 0xF19B5 => 'SITELEN PONA IDEOGRAPH TO', | ||
| 245 | 0xF19B6 => 'SITELEN PONA IDEOGRAPH UNU', | ||
| 246 | 0xF19B7 => 'SITELEN PONA IDEOGRAPH USAWI', | ||
| 247 | 0xF19B8 => 'SITELEN PONA IDEOGRAPH WA', | ||
| 248 | 0xF19B9 => 'SITELEN PONA IDEOGRAPH WUWOJITI', | ||
| 249 | 0xF19BA => 'SITELEN PONA IDEOGRAPH YUPEKOSI', | ||
| 250 | ); | ||
| 251 | foreach my $cp (keys %ucsur) { | ||
| 252 | my ($str) = $ucsur{$cp} =~ /^SITELEN PONA IDEOGRAPH ([A-Z]+)\z/ or next; | ||
| 253 | my $word = $_words{lc $str} // next; | ||
| 254 | $word->{ucsur} = int $cp; | ||
| 255 | $_by_ucsur{$cp} = $word; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | # Not our actual constructor, see _new(). This is for the public API only | ||
| 261 | sub new { | ||
| 262 | my ($pkg, $type, $value, @opts) = @_; | ||
| 263 | |||
| 264 | croak 'No type given' unless defined $type; | ||
| 265 | croak 'No value given' unless defined $value; | ||
| 266 | croak 'Stray options passed' if scalar @opts; | ||
| 267 | |||
| 268 | if ($type eq 'from') { | ||
| 269 | if (ref $value) { | ||
| 270 | if ($value->isa(__PACKAGE__)) { | ||
| 271 | return $value; | ||
| 272 | } elsif ($value->isa('Data::Identifier')) { | ||
| 273 | $type = 'Data::Identifier'; | ||
| 274 | } elsif ($value->isa('Lingua::famibeib::Word')) { | ||
| 275 | $value = $value->as(__PACKAGE__); | ||
| 276 | return $value if scalar(@opts) == 0; | ||
| 277 | $value = $value->as('Data::Identifier'); | ||
| 278 | $type = 'Data::Identifier'; | ||
| 279 | } else { | ||
| 280 | $type = 'Data::Identifier'; | ||
| 281 | $value = Data::Identifier->new(from => $value); | ||
| 282 | } | ||
| 283 | } else { | ||
| 284 | $type = 'string'; | ||
| 285 | } | ||
| 286 | } elsif ($type eq 'ise') { | ||
| 287 | $type = 'Data::Identifier'; | ||
| 288 | $value = Data::Identifier->new(ise => $value); | ||
| 289 | } | ||
| 290 | |||
| 291 | if ($type eq 'string') { | ||
| 292 | return $_words{lc $value} // croak 'No such word'; | ||
| 293 | } elsif ($type eq 'ucsur') { | ||
| 294 | if ($value =~ /^U\+([0-9a-fA-F]{4,6})\z/) { | ||
| 295 | $value = hex($1); | ||
| 296 | } | ||
| 297 | return $_by_ucsur{$value} // croak 'No such word'; | ||
| 298 | } elsif ($type eq 'Data::Identifier') { | ||
| 299 | return $_by_ise{$value->uuid(default => undef, no_defaults => 1) // $value->ise} // croak 'No such word'; | ||
| 300 | } else { | ||
| 301 | croak 'Bad type: '.$type; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | |||
| 306 | # NOTE: Implemented via Lingua::Generic::Interface::Word | ||
| 307 | |||
| 308 | |||
| 309 | sub ucsur { | ||
| 310 | my ($self, %opts) = @_; | ||
| 311 | my $has_default = exists $opts{default}; | ||
| 312 | my $default = delete $opts{default}; | ||
| 313 | my $as = delete $opts{as}; | ||
| 314 | my $ucsur = $self->{ucsur}; | ||
| 315 | |||
| 316 | delete $opts{no_defaults}; | ||
| 317 | |||
| 318 | croak 'Stray options passed' if scalar keys %opts; | ||
| 319 | |||
| 320 | unless (defined $ucsur) { | ||
| 321 | return $opts{default} if exists $opts{default}; | ||
| 322 | croak 'No value found'; | ||
| 323 | } | ||
| 324 | |||
| 325 | return $ucsur unless defined $as; | ||
| 326 | |||
| 327 | $self->{ucsur_id} //= Data::Identifier::Generate->unicode_character(unicode => $ucsur)->register; | ||
| 328 | |||
| 329 | return $self->{ucsur_id}->as($as, so => $self); | ||
| 330 | } | ||
| 331 | |||
| 332 | |||
| 333 | # NOTE: Implemented via Lingua::Generic::Interface::Word | ||
| 334 | |||
| 335 | |||
| 336 | # NOTE: Implemented via Lingua::Generic::Interface::Word | ||
| 337 | |||
| 338 | |||
| 339 | sub has_type { | ||
| 340 | my ($self, %opts) = @_; | ||
| 341 | my $as = delete $opts{as}; | ||
| 342 | |||
| 343 | delete $opts{default}; | ||
| 344 | delete $opts{no_defaults}; | ||
| 345 | |||
| 346 | croak 'Stray options passed' if scalar keys %opts; | ||
| 347 | |||
| 348 | return WK_WORD_TYPE if !defined($as) || WK_WORD_TYPE->isa($as); | ||
| 349 | |||
| 350 | return WK_WORD_TYPE->as($as, so => $self); | ||
| 351 | } | ||
| 352 | |||
| 353 | |||
| 354 | sub natural_language { | ||
| 355 | my ($self) = @_; | ||
| 356 | return WK_LT_TOK; | ||
| 357 | } | ||
| 358 | |||
| 359 | # ---- Overridden methods ---- | ||
| 360 | |||
| 361 | |||
| 362 | |||
| 363 | # ---- Private helpers ---- | ||
| 364 | |||
| 365 | #@returns __PACKAGE__ | ||
| 366 | sub _new { | ||
| 367 | my ($pkg, $str) = @_; | ||
| 368 | my $self = bless {class => {}}, $pkg; | ||
| 369 | my $id; | ||
| 370 | |||
| 371 | $str = lc($str); # just to be on the safe side | ||
| 372 | |||
| 373 | $self->{string} = $str; | ||
| 374 | |||
| 375 | $id = Data::Identifier::Generate->generic( | ||
| 376 | request => $str, | ||
| 377 | displayname => $str, | ||
| 378 | tagname => $str, | ||
| 379 | style => 'id-based', | ||
| 380 | namespace => WK_WORD_NAMESPACE, | ||
| 381 | generator => WK_WORD_GENERATOR, | ||
| 382 | )->register; | ||
| 383 | |||
| 384 | $self->{id} = $id; | ||
| 385 | |||
| 386 | $_words{$str} = $self; | ||
| 387 | $_by_ise{$id->uuid} = $self; | ||
| 388 | return $self; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub as { | ||
| 392 | my ($self, $as, @opts) = @_; | ||
| 393 | my Data::Identifier $id = $self->{id}; | ||
| 394 | |||
| 395 | if (scalar(@opts) == 0) { | ||
| 396 | return $self if $self->isa($as); | ||
| 397 | return $id if $as eq 'Data::Identifier'; | ||
| 398 | |||
| 399 | if ($as eq 'Lingua::famibeib::Word') { | ||
| 400 | require Lingua::famibeib::Word; | ||
| 401 | return Lingua::famibeib::Word->new(from => $self); | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | return $id->as($as, @opts); | ||
| 406 | } | ||
| 407 | |||
| 408 | sub ise { goto &Data::Identifier::Interface::Simple::ise } # overridden using tail-call | ||
| 409 | |||
| 410 | sub _known_provider { | ||
| 411 | my ($pkg, $class, %opts) = @_; | ||
| 412 | |||
| 413 | croak 'Unsupported options passed' if scalar(keys %opts); | ||
| 414 | |||
| 415 | if ($class eq 'word' || $class eq 'words') { | ||
| 416 | return ([values %_words], rawtype => __PACKAGE__); | ||
| 417 | } elsif ($class eq 'stopword') { | ||
| 418 | return ([grep {$_->{class}{$class}} values %_words], rawtype => __PACKAGE__); | ||
| 419 | } elsif ($class eq ':all') { | ||
| 420 | return ([ | ||
| 421 | values(%_words), | ||
| 422 | WK_WORD_GENERATOR, WK_WORD_NAMESPACE, | ||
| 423 | WK_WORD_TYPE, | ||
| 424 | WK_LT_TOK, | ||
| 425 | ], rawtype => 'Data::Identifier::Interface::Simple'); | ||
| 426 | } | ||
| 427 | |||
| 428 | croak 'Unsupported class'; | ||
| 429 | } | ||
| 430 | |||
| 431 | 1; | ||
| 432 | |||
| 433 | __END__ | ||
| 434 | |||
| 435 | =pod | ||
| 436 | |||
| 437 | =encoding UTF-8 | ||
| 438 | |||
| 439 | =head1 NAME | ||
| 440 | |||
| 441 | Lingua::TokiPona::Word - module to interact with the words of Toki Pona | ||
| 442 | |||
| 443 | =head1 VERSION | ||
| 444 | |||
| 445 | version v0.03 | ||
| 446 | |||
| 447 | =head1 SYNOPSIS | ||
| 448 | |||
| 449 | use Lingua::TokiPona::Word; | ||
| 450 | |||
| 451 | my Lingua::TokiPona::Word $word = Lingua::TokiPona::Word->new(string => 'mi'); | ||
| 452 | |||
| 453 | say $word->as_string; | ||
| 454 | |||
| 455 | This module inherits from | ||
| 456 | L<Lingua::Generic::Interface::Word> (since v0.03), | ||
| 457 | L<Data::Identifier::Interface::Known> (since v0.01), | ||
| 458 | L<Data::Identifier::Interface::Simple> (since v0.01), | ||
| 459 | and L<Data::Identifier::Interface::Subobjects> (since v0.01). | ||
| 460 | |||
| 461 | =head1 OVERVIEW | ||
| 462 | |||
| 463 | Toki Pona is a constructed language that features a specifically small set of vocables. | ||
| 464 | This module abstracts those words. It aims to contain all known words (as of v0.02: which is currently not yet fully true). | ||
| 465 | |||
| 466 | As the vocabulary is so limited, this module does hold all the words in memory at all times. | ||
| 467 | This means that there are never two different object instances for the same words. | ||
| 468 | (Note that this may limit the usefulness of L<Data::Identifier::Interface::Subobjects>). | ||
| 469 | |||
| 470 | That said, synonyms are consider different words. | ||
| 471 | So this is round-trip safe (you always get the word you asked for, not one of it's synonyms). | ||
| 472 | |||
| 473 | To get a list of all known words use L</known>. | ||
| 474 | |||
| 475 | =head1 METHODS | ||
| 476 | |||
| 477 | =head2 new | ||
| 478 | |||
| 479 | my Lingua::TokiPona::Word $word = Lingua::TokiPona::Word->new($type => $value); | ||
| 480 | # e.g.: | ||
| 481 | my Lingua::TokiPona::Word $word = Lingua::TokiPona::Word->new(string => 'mi'); | ||
| 482 | |||
| 483 | (since v0.01) | ||
| 484 | |||
| 485 | Constructs a new word. | ||
| 486 | The word is normalised as part of this. | ||
| 487 | This method deduplicate instances. | ||
| 488 | |||
| 489 | Currently the following types (C<$type>) are supported: | ||
| 490 | |||
| 491 | =over | ||
| 492 | |||
| 493 | =item C<from> | ||
| 494 | |||
| 495 | (since v0.01) | ||
| 496 | |||
| 497 | Constructs a word from an object. | ||
| 498 | C<$value> should be a reference. | ||
| 499 | |||
| 500 | Currently references to the following types are supported: | ||
| 501 | L<Lingua::TokiPona::Word>, | ||
| 502 | or anything L<Data::Identifier/new> accepts via C<from>. | ||
| 503 | More types might be supported. | ||
| 504 | |||
| 505 | If C<$value> is not a reference the value is parsed as per C<string> if it looks like a word string (experimental since v0.01). | ||
| 506 | |||
| 507 | =item C<ise> | ||
| 508 | |||
| 509 | (since v0.02) | ||
| 510 | |||
| 511 | Constructs a word from a ISE (such as returned by L<Data::Identifier::Interface::Simple/ise>). | ||
| 512 | |||
| 513 | =item C<string> | ||
| 514 | |||
| 515 | (since v0.01) | ||
| 516 | |||
| 517 | Constructs a word from it's (latin) string representation. | ||
| 518 | |||
| 519 | =item C<ucsur> | ||
| 520 | |||
| 521 | (since v0.02) | ||
| 522 | |||
| 523 | Constructs a word from the UCSUR code point. | ||
| 524 | |||
| 525 | See L</ucsur> for details. | ||
| 526 | |||
| 527 | Accepts the numerical code point or standard Unicode notation (C<U+xxxx>). | ||
| 528 | |||
| 529 | =back | ||
| 530 | |||
| 531 | =head2 as_string | ||
| 532 | |||
| 533 | my $str = $word->as_string; | ||
| 534 | |||
| 535 | (since v0.01) | ||
| 536 | |||
| 537 | Returns the string representation of the word. | ||
| 538 | |||
| 539 | See also: L<Lingua::Generic::Interface::Word/as_string>. | ||
| 540 | |||
| 541 | =head2 ucsur | ||
| 542 | |||
| 543 | my $ucsur = $word->ucsur( [ %opts ] ); | ||
| 544 | |||
| 545 | (since v0.02) | ||
| 546 | |||
| 547 | Returns the Under-ConScript Unicode Registry (UCSUR) code point of the word or C<die>s if there is none. | ||
| 548 | |||
| 549 | Returns the numerical Unicode code point unless C<as> is given. | ||
| 550 | |||
| 551 | The following options are supported: | ||
| 552 | |||
| 553 | =over | ||
| 554 | |||
| 555 | =item C<as> | ||
| 556 | |||
| 557 | Returns the value as the passed type. | ||
| 558 | All types supported by L<Data::Identifier/as> are supported. | ||
| 559 | |||
| 560 | =item C<default> | ||
| 561 | |||
| 562 | The default value to return if no other value is available. | ||
| 563 | This can be set to C<undef> to let this method return C<undef> (not C<die>). | ||
| 564 | |||
| 565 | =item C<no_defaults> | ||
| 566 | |||
| 567 | This option is accepted but ignored. | ||
| 568 | |||
| 569 | =back | ||
| 570 | |||
| 571 | =head2 eq | ||
| 572 | |||
| 573 | my $bool = $word->eq($other); # $word must be non-undef | ||
| 574 | # or: | ||
| 575 | my $bool = Lingua::TokiPona::Word::eq($word, $other); # $word can be undef | ||
| 576 | |||
| 577 | (since v0.01) | ||
| 578 | |||
| 579 | Compares two words to be equal. | ||
| 580 | |||
| 581 | If both words are C<undef> they are considered equal. | ||
| 582 | |||
| 583 | If C<$word> or C<$other> is not an instance of L<Lingua::TokiPona::Word> or C<undef> | ||
| 584 | L</new> with the type C<from> is used. | ||
| 585 | |||
| 586 | The operators L<perlop/eq> and L<perlop/ne> are overloaded to this method. | ||
| 587 | |||
| 588 | See also: L<Lingua::Generic::Interface::Word/eq>. | ||
| 589 | |||
| 590 | =head2 cmp | ||
| 591 | |||
| 592 | my $val = $word->cmp($other); # $word must be non-undef | ||
| 593 | # or: | ||
| 594 | my $val = Lingua::TokiPona::Word::cmp($word, $other); # $word can be undef | ||
| 595 | |||
| 596 | (experimental since v0.01) | ||
| 597 | |||
| 598 | Compares the words similar to C<cmp>. This method can be used to order words. | ||
| 599 | To check for them to be equal see L</eq>. | ||
| 600 | |||
| 601 | The parameters are parsed the same way as L</eq>. | ||
| 602 | |||
| 603 | The operator L<perlop/cmp> is overloaded to this method. | ||
| 604 | |||
| 605 | If this method is used for sorting the exact resulting order is not defined. However: | ||
| 606 | |||
| 607 | =over | ||
| 608 | |||
| 609 | =item * | ||
| 610 | |||
| 611 | The order is stable | ||
| 612 | |||
| 613 | =item * | ||
| 614 | |||
| 615 | The order is the same for C<$a-E<gt>cmp($b)> as for C<- $b-E<gt>cmp($a)>. | ||
| 616 | |||
| 617 | =back | ||
| 618 | |||
| 619 | See also: L<Lingua::Generic::Interface::Word/cmp>. | ||
| 620 | |||
| 621 | =head2 has_type | ||
| 622 | |||
| 623 | my $type = $word->has_type( [ %opts ] ); | ||
| 624 | |||
| 625 | (since v0.02) | ||
| 626 | |||
| 627 | Returns the type of the word which will always be the same for all words: a Toki Pona word. | ||
| 628 | |||
| 629 | B<Note:> | ||
| 630 | If you want to know about other roles (e.g. particle vs. noun) this is the wrong method for you. | ||
| 631 | |||
| 632 | The following options are accepted: | ||
| 633 | |||
| 634 | =over | ||
| 635 | |||
| 636 | =item C<as> | ||
| 637 | |||
| 638 | Returns the value as the passed type. | ||
| 639 | All types supported by L<Data::Identifier/as> are supported. | ||
| 640 | |||
| 641 | =item C<default> | ||
| 642 | |||
| 643 | This option is accepted but ignored. | ||
| 644 | |||
| 645 | =item C<no_defaults> | ||
| 646 | |||
| 647 | This option is accepted but ignored. | ||
| 648 | |||
| 649 | =back | ||
| 650 | |||
| 651 | =head2 natural_language | ||
| 652 | |||
| 653 | my Data::Identifier $natural_language = $word->natural_language; | ||
| 654 | |||
| 655 | (since v0.03) | ||
| 656 | |||
| 657 | Returns the natural language this word is in. | ||
| 658 | |||
| 659 | For Toki Pona words it will always return Toki Pona. | ||
| 660 | However this method might be useful together with other modules from the C<Lingua> namespace. | ||
| 661 | |||
| 662 | See also: L<Lingua::Generic::Interface::Word/natural_language>. | ||
| 663 | |||
| 664 | =head2 known | ||
| 665 | |||
| 666 | my @list = Lingua::TokiPona::Word->known($class [, %opts ] ); | ||
| 667 | |||
| 668 | (since v0.01) | ||
| 669 | |||
| 670 | Returns the known items for the given class. | ||
| 671 | |||
| 672 | For details and supported options see L<Data::Identifier::Interface::Known/known>. | ||
| 673 | |||
| 674 | The following classes are supported: | ||
| 675 | |||
| 676 | =over | ||
| 677 | |||
| 678 | =item C<:all> | ||
| 679 | |||
| 680 | (since v0.01) | ||
| 681 | |||
| 682 | Returns all known things. | ||
| 683 | |||
| 684 | =item C<word> | ||
| 685 | |||
| 686 | (since v0.03) | ||
| 687 | |||
| 688 | Returns all known words. | ||
| 689 | |||
| 690 | =item C<words> | ||
| 691 | |||
| 692 | (since v0.02, deprecated since 0.03) | ||
| 693 | |||
| 694 | Deprecated alias for C<word>. | ||
| 695 | |||
| 696 | =item C<stopword> | ||
| 697 | |||
| 698 | (since v0.02) | ||
| 699 | |||
| 700 | Returns all known stopwords. | ||
| 701 | |||
| 702 | =back | ||
| 703 | |||
| 704 | =head1 AUTHOR | ||
| 705 | |||
| 706 | Philipp Schafft <lion@cpan.org> | ||
| 707 | |||
| 708 | =head1 COPYRIGHT AND LICENSE | ||
| 709 | |||
| 710 | This software is Copyright (c) 2026 by Philipp Schafft <lion@cpan.org>. | ||
| 711 | |||
| 712 | This is free software, licensed under: | ||
| 713 | |||
| 714 | The Artistic License 2.0 (GPL Compatible) | ||
| 715 | |||
| 716 | =cut |