Module: Spacy::SyntaxTree

Defined in:
lib/ruby-spacy/syntax_tree.rb

Overview

Converts spaCy parse results into rsyntaxtree bracket notation and renders them with the rsyntaxtree gem. This is an internal implementation module; the public API is Doc#syntax_tree and Spacy::Span#syntax_tree.

rsyntaxtree is a soft dependency: it is required (>= 2.4.0, for RSyntaxTree.escape) on the first call, not at load time.

Constant Summary collapse

FORMATS =
%i[bracket svg png pdf tikz json].freeze
STYLES =
%i[projection chunks].freeze
RENDER_DEFAULTS =

rsyntaxtree drawing defaults chosen for the wide, shallow trees produced here. User-supplied options take precedence (except hyphen, which the escaping depends on).

{
  hyphen: "literal",
  polyline: "on",
  tidy: "medium",
  leafstyle: "nothing",
  color: "modern"
}.freeze
PHRASE_LABEL =

Phrase label mapping from the head's POS tag

{
  "NOUN" => "NP", "PROPN" => "NP", "PRON" => "NP", "NUM" => "NP",
  "VERB" => "VP", "AUX" => "VP", "ADP" => "PP", "ADJ" => "AdjP",
  "ADV" => "AdvP", "DET" => "DP", "SCONJ" => "CP", "CCONJ" => "ConjP"
}.freeze
ENT_BACKGROUND =
"orange"

Class Method Summary collapse

Class Method Details

.generate(source, format: :bracket, style: :projection, morphology: false, entities: true, punctuation: false, **render_opts) ⇒ String

Returns the bracket notation (format: :bracket), the rendered output (SVG/TikZ/JSON text, or PNG/PDF binary), depending on format.

Parameters:

  • source (Doc, Span) —

    the document or span to convert

Returns:

  • (String) —

    the bracket notation (format: :bracket), the rendered output (SVG/TikZ/JSON text, or PNG/PDF binary), depending on format



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby-spacy/syntax_tree.rb', line 38

def generate(source, format: :bracket, style: :projection, morphology: false,
             entities: true, punctuation: false, **render_opts)
  ensure_rsyntaxtree!
  format = format.to_sym
  style = style.to_sym
  validate_options!(format, style, render_opts)

  bracket = bracket_for(source, style: style, morphology: morphology,
                        entities: entities, punctuation: punctuation)
  return bracket if format == :bracket

  # Right-to-left scripts are drawn mirrored (leaves run right to left)
  # unless the caller says otherwise. The notation itself is unaffected
  render_opts = { mirror: "on" }.merge(render_opts) if rtl?(source)
  render(bracket, format, render_opts)
end