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
|
"""Functions for working with hypernym/hyponym taxonomies."""
from __future__ import annotations
import wn
from wn._util import flatten
from wn.constants import ADJ, ADJ_SAT
_FAKE_ROOT = "*ROOT*"
def roots(wordnet: wn.Wordnet, pos: str | None = None) -> list[wn.Synset]:
"""Return the list of root synsets in *wordnet*.
Arguments:
wordnet: The wordnet from which root synsets are found.
pos: If given, only return synsets with the specified part of
speech.
Example:
>>> import wn, wn.taxonomy
>>> ewn = wn.Wordnet("ewn:2020")
>>> len(wn.taxonomy.roots(ewn, pos="v"))
573
"""
return [ss for ss in _synsets_for_pos(wordnet, pos) if not ss.hypernyms()]
def leaves(wordnet: wn.Wordnet, pos: str | None = None) -> list[wn.Synset]:
"""Return the list of leaf synsets in *wordnet*.
Arguments:
wordnet: The wordnet from which leaf synsets are found.
pos: If given, only return synsets with the specified part of
speech.
Example:
>>> import wn, wn.taxonomy
>>> ewn = wn.Wordnet("ewn:2020")
>>> len(wn.taxonomy.leaves(ewn, pos="v"))
10525
"""
return [ss for ss in _synsets_for_pos(wordnet, pos) if not ss.hyponyms()]
def taxonomy_depth(wordnet: wn.Wordnet, pos: str) -> int:
"""Return the maximum depth of the taxonomy for the given part of speech.
Arguments:
wordnet: The wordnet for which the taxonomy depth will be
calculated.
pos: The part of speech for which the taxonomy depth will be
calculated.
Example:
>>> import wn, wn.taxonomy
>>> ewn = wn.Wordnet("ewn:2020")
>>> wn.taxonomy.taxonomy_depth(ewn, "n")
19
"""
seen: set[wn.Synset] = set()
depth = 0
for ss in _synsets_for_pos(wordnet, pos):
if all(hyp in seen for hyp in ss.hypernyms()):
continue
paths = ss.hypernym_paths()
if paths:
depth = max(depth, max(len(path) for path in paths))
seen.update(hyp for path in paths for hyp in path)
return depth
def _synsets_for_pos(wordnet: wn.Wordnet, pos: str | None) -> list[wn.Synset]:
"""Get the list of synsets for a part of speech. If *pos* is 'a' or
's', also include those for the other.
"""
synsets = wordnet.synsets(pos=pos)
if pos == ADJ:
synsets.extend(wordnet.synsets(pos=ADJ_SAT))
elif pos == ADJ_SAT:
synsets.extend(wordnet.synsets(pos=ADJ))
return synsets
def _hypernym_paths(
synset: wn.Synset,
simulate_root: bool,
include_self: bool,
) -> list[list[wn.Synset]]:
paths = list(synset.relation_paths("hypernym", "instance_hypernym"))
if include_self:
paths = [[synset, *path] for path in paths] or [[synset]]
if simulate_root and synset.id != _FAKE_ROOT:
root = wn.Synset.empty(
id=_FAKE_ROOT, _lexicon=synset._lexicon, _lexconf=synset._lexconf
)
paths = [[*path, root] for path in paths] or [[root]]
return paths
def hypernym_paths(
synset: wn.Synset,
simulate_root: bool = False,
) -> list[list[wn.Synset]]:
"""Return the list of hypernym paths to a root synset.
Arguments:
synset: The starting synset for paths to a root.
simulate_root: If :python:`True`, find the path to a simulated
root node.
Example:
>>> import wn, wn.taxonomy
>>> dog = wn.synsets("dog", pos="n")[0]
>>> for path in wn.taxonomy.hypernym_paths(dog):
... for i, ss in enumerate(path):
... print(" " * i, ss, ss.lemmas()[0])
Synset('pwn-02083346-n') canine
Synset('pwn-02075296-n') carnivore
Synset('pwn-01886756-n') eutherian mammal
Synset('pwn-01861778-n') mammalian
Synset('pwn-01471682-n') craniate
Synset('pwn-01466257-n') chordate
Synset('pwn-00015388-n') animal
Synset('pwn-00004475-n') organism
Synset('pwn-00004258-n') animate thing
Synset('pwn-00003553-n') unit
Synset('pwn-00002684-n') object
Synset('pwn-00001930-n') physical entity
Synset('pwn-00001740-n') entity
Synset('pwn-01317541-n') domesticated animal
Synset('pwn-00015388-n') animal
Synset('pwn-00004475-n') organism
Synset('pwn-00004258-n') animate thing
Synset('pwn-00003553-n') unit
Synset('pwn-00002684-n') object
Synset('pwn-00001930-n') physical entity
Synset('pwn-00001740-n') entity
"""
return _hypernym_paths(synset, simulate_root, False)
def min_depth(synset: wn.Synset, simulate_root: bool = False) -> int:
"""Return the minimum taxonomy depth of the synset.
Arguments:
synset: The starting synset for paths to a root.
simulate_root: If :python:`True`, find the depth to a
simulated root node.
Example:
>>> import wn, wn.taxonomy
>>> dog = wn.synsets("dog", pos="n")[0]
>>> wn.taxonomy.min_depth(dog)
8
"""
return min(
(len(path) for path in synset.hypernym_paths(simulate_root=simulate_root)),
default=0,
)
def max_depth(synset: wn.Synset, simulate_root: bool = False) -> int:
"""Return the maximum taxonomy depth of the synset.
Arguments:
synset: The starting synset for paths to a root.
simulate_root: If :python:`True`, find the depth to a
simulated root node.
Example:
>>> import wn, wn.taxonomy
>>> dog = wn.synsets("dog", pos="n")[0]
>>> wn.taxonomy.max_depth(dog)
13
"""
return max(
(len(path) for path in synset.hypernym_paths(simulate_root=simulate_root)),
default=0,
)
def _shortest_hyp_paths(
synset: wn.Synset,
other: wn.Synset,
simulate_root: bool,
) -> dict[tuple[wn.Synset, int], list[wn.Synset]]:
if synset == other:
return {(synset, 0): []}
from_self = _hypernym_paths(synset, simulate_root, True)
from_other = _hypernym_paths(other, simulate_root, True)
common = set(flatten(from_self)).intersection(flatten(from_other))
if not common:
return {}
# Compute depths of common hypernyms from their distances.
# Doing this now avoid more expensive lookups later.
depths: dict[wn.Synset, int] = {}
# subpaths accumulates paths to common hypernyms from both sides
subpaths: dict[wn.Synset, tuple[list[list[wn.Synset]], list[list[wn.Synset]]]]
subpaths = {ss: ([], []) for ss in common}
for which, paths in (0, from_self), (1, from_other):
for path in paths:
for dist, ss in enumerate(path):
if ss in common:
# synset or other subpath to ss (not including ss)
subpaths[ss][which].append(path[: dist + 1])
# keep maximum depth
depth = len(path) - dist - 1
if ss not in depths or depths[ss] < depth:
depths[ss] = depth
shortest: dict[tuple[wn.Synset, int], list[wn.Synset]] = {}
for ss in common:
from_self_subpaths, from_other_subpaths = subpaths[ss]
shortest_from_self = min(from_self_subpaths, key=len)
# for the other path, we need to reverse it and remove the pivot synset
# (ty doesn't infer the result of min() correctly, hence the ignore)
shortest_from_other = min(from_other_subpaths, key=len)[-2::-1] # type: ignore
shortest[(ss, depths[ss])] = shortest_from_self + shortest_from_other
return shortest
def shortest_path(
synset: wn.Synset,
other: wn.Synset,
simulate_root: bool = False,
) -> list[wn.Synset]:
"""Return the shortest path from *synset* to the *other* synset.
Arguments:
other: endpoint synset of the path
simulate_root: if :python:`True`, ensure any two synsets
are always connected by positing a fake root node
Example:
>>> import wn, wn.taxonomy
>>> dog = ewn.synsets("dog", pos="n")[0]
>>> squirrel = ewn.synsets("squirrel", pos="n")[0]
>>> for ss in wn.taxonomy.shortest_path(dog, squirrel):
... print(ss.lemmas())
['canine', 'canid']
['carnivore']
['eutherian mammal', 'placental', 'placental mammal', 'eutherian']
['rodent', 'gnawer']
['squirrel']
"""
pathmap = _shortest_hyp_paths(synset, other, simulate_root)
key = min(pathmap, key=lambda key: len(pathmap[key]), default=None)
if key is None:
raise wn.Error(f"no path between {synset!r} and {other!r}")
return pathmap[key][1:]
def common_hypernyms(
synset: wn.Synset,
other: wn.Synset,
simulate_root: bool = False,
) -> list[wn.Synset]:
"""Return the common hypernyms for the current and *other* synsets.
Arguments:
other: synset that is a hyponym of any shared hypernyms
simulate_root: if :python:`True`, ensure any two synsets
always share a hypernym by positing a fake root node
Example:
>>> import wn, wn.taxonomy
>>> dog = ewn.synsets("dog", pos="n")[0]
>>> squirrel = ewn.synsets("squirrel", pos="n")[0]
>>> for ss in wn.taxonomy.common_hypernyms(dog, squirrel):
... print(ss.lemmas())
['entity']
['physical entity']
['object', 'physical object']
['unit', 'whole']
['animate thing', 'living thing']
['organism', 'being']
['fauna', 'beast', 'animate being', 'brute', 'creature', 'animal']
['chordate']
['craniate', 'vertebrate']
['mammalian', 'mammal']
['eutherian mammal', 'placental', 'placental mammal', 'eutherian']
"""
from_self = _hypernym_paths(synset, simulate_root, True)
from_other = _hypernym_paths(other, simulate_root, True)
common = set(flatten(from_self)).intersection(flatten(from_other))
return sorted(common, key=lambda ss: ss.id)
def lowest_common_hypernyms(
synset: wn.Synset,
other: wn.Synset,
simulate_root: bool = False,
) -> list[wn.Synset]:
"""Return the common hypernyms furthest from the root.
Arguments:
other: synset that is a hyponym of any shared hypernyms
simulate_root: if :python:`True`, ensure any two synsets
always share a hypernym by positing a fake root node
Example:
>>> import wn, wn.taxonomy
>>> dog = ewn.synsets("dog", pos="n")[0]
>>> squirrel = ewn.synsets("squirrel", pos="n")[0]
>>> len(wn.taxonomy.lowest_common_hypernyms(dog, squirrel))
1
>>> wn.taxonomy.lowest_common_hypernyms(dog, squirrel)[0].lemmas()
['eutherian mammal', 'placental', 'placental mammal', 'eutherian']
"""
pathmap = _shortest_hyp_paths(synset, other, simulate_root)
# keys of pathmap are (synset, depth_of_synset)
max_depth: int = max([depth for _, depth in pathmap], default=-1)
if max_depth == -1:
return []
else:
return [ss for ss, d in pathmap if d == max_depth]
|