Class Taxonomy
java.lang.Object
smile.nlp.taxonomy.Taxonomy
A taxonomy is a tree of terms (aka concept) where leaves
must be named but intermediary nodes can be anonymous.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbfs()Returns all concept nodes in breadth-first order starting from the root.booleanReturns true if the given keyword exists in the taxonomy.intReturns the depth of a concept in the taxonomy (root has depth 0).voidVisits every concept node in depth-first pre-order, passing each node to the given consumer.getConcept(String keyword) Returns the concept node which synset contains the keyword.Returns all named concepts in the taxonomy.getRoot()Returns the root node of taxonomy tree.intheight()Returns the height of the taxonomy tree, i.e.booleanisAncestor(String ancestor, String descendant) Returns true if conceptancestoris an ancestor of conceptdescendantin the taxonomy.booleanisDescendant(String descendant, String ancestor) Returns true if conceptdescendantis a descendant of conceptancestorin the taxonomy.leaves()Returns all leaf concept nodes in the taxonomy (nodes with no children).level(int level) Returns all concept nodes at the given depth level (root is level 0).lowestCommonAncestor(String v, String w) Returns the lowest common ancestor (LCA) of concepts v and w.Returns the lowest common ancestor (LCA) of concepts v and w.intReturns the total number of concept nodes (anonymous or named) in the taxonomy tree, including the root.static TaxonomyParses a taxonomy from a simple indented-text format.shortestPath(String v, String w) Returns the shortest path between two concepts as an ordered list of concept nodes, fromvtow(both endpoints inclusive).shortestPath(Concept v, Concept w) Returns the shortest path between two concept nodes as an ordered list, fromvtow(both endpoints inclusive).intsize()Returns the total number of named concepts (keywords) in the taxonomy.Returns all keywords in the sub-tree rooted at the given concept (i.e.toString()Returns a multi-line string that prints the taxonomy tree using Unicode box-drawing characters, e.g.:
-
Constructor Details
-
Taxonomy
Constructor.- Parameters:
rootConcept- the keyword of root concept.
-
-
Method Details
-
getRoot
-
getConcept
-
getConcepts
-
lowestCommonAncestor
Returns the lowest common ancestor (LCA) of concepts v and w. The lowest common ancestor is defined between two nodes v and w as the lowest node that has both v and w as descendants (where we allow a node to be a descendant of itself).- Parameters:
v- a concept keyword.w- the other concept keyword.- Returns:
- the lowest common ancestor.
- Throws:
IllegalArgumentException- if either keyword is not in the taxonomy.
-
depth
Returns the depth of a concept in the taxonomy (root has depth 0).- Parameters:
keyword- the concept keyword.- Returns:
- the depth, or -1 if the keyword is not in the taxonomy.
-
size
public int size()Returns the total number of named concepts (keywords) in the taxonomy.- Returns:
- the number of keywords.
-
lowestCommonAncestor
Returns the lowest common ancestor (LCA) of concepts v and w. The lowest common ancestor is defined between two nodes v and w as the lowest node that has both v and w as descendants (where we allow a node to be a descendant of itself).- Parameters:
v- a concept.w- the other concept.- Returns:
- the lowest common ancestor.
-
height
public int height()Returns the height of the taxonomy tree, i.e. the maximum depth of any concept node (root has depth 0, so a single-node tree has height 0).- Returns:
- the tree height.
-
isAncestor
Returns true if conceptancestoris an ancestor of conceptdescendantin the taxonomy.- Parameters:
ancestor- the potential ancestor keyword.descendant- the potential descendant keyword.- Returns:
- true if
ancestoris an ancestor ofdescendant. - Throws:
IllegalArgumentException- if either keyword is not in the taxonomy.
-
isDescendant
Returns true if conceptdescendantis a descendant of conceptancestorin the taxonomy.- Parameters:
descendant- the potential descendant keyword.ancestor- the potential ancestor keyword.- Returns:
- true if
descendantis a descendant ofancestor. - Throws:
IllegalArgumentException- if either keyword is not in the taxonomy.
-
shortestPath
Returns the shortest path between two concepts as an ordered list of concept nodes, fromvtow(both endpoints inclusive). The path goes up fromvto their lowest common ancestor, then down tow.- Parameters:
v- a concept keyword.w- the other concept keyword.- Returns:
- the ordered list of concept nodes on the shortest path.
- Throws:
IllegalArgumentException- if either keyword is not in the taxonomy.
-
shortestPath
-
subtree
Returns all keywords in the sub-tree rooted at the given concept (i.e. the concept itself and all its descendants).- Parameters:
keyword- the root of the sub-tree.- Returns:
- list of all keywords in the sub-tree.
- Throws:
IllegalArgumentException- if the keyword is not in the taxonomy.
-
bfs
-
leaves
-
contains
Returns true if the given keyword exists in the taxonomy.- Parameters:
keyword- the keyword to look up.- Returns:
- true if the keyword is registered.
-
nodeCount
public int nodeCount()Returns the total number of concept nodes (anonymous or named) in the taxonomy tree, including the root.- Returns:
- the node count.
-
level
-
forEach
-
toString
-
of
Parses a taxonomy from a simple indented-text format. Each line isindent keyword[, keyword2, ...]where indent is multiples of 4 spaces or tabs. Indent level 0 is the root concept. Multiple keywords on the same line become synonyms of the same concept. Lines starting with#are comments and ignored. Example:# animal taxonomy animal mammal, warm-blooded dog, canine cat, feline reptile snake- Parameters:
text- the indented text.- Returns:
- the parsed taxonomy.
- Throws:
IllegalArgumentException- if the text is malformed.
-