rsyntaxtree 0.7.1 → 0.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5801173697da7daaf1e8f8741b7af0cf6c296a6a
4
- data.tar.gz: 96806aede17441ce30718cfc983a4883166787f7
2
+ SHA256:
3
+ metadata.gz: b2b0490e0b866d5e82213a7e131af2eb12f4561b5d2132298324e2309303ad65
4
+ data.tar.gz: aa27a23a4c922af2c2d3f701ca6cc571fab074eecc3cd327c2296fd79b1d42ff
5
5
  SHA512:
6
- metadata.gz: 50f211f9a4d650270c502d5feafd69abe441becc400aba6df7f38c512ab8f2fb03af37ea983776566a907b5c3d7ffee9074bc24be9b0524c1d5443498212080a
7
- data.tar.gz: 5d38af416912e8b362713d771a5a48c484d123b053edd386fdb6f191e2e9d1677228fd51190bf9e312c01f496acef83fdac958db3e55808b8bfb91f143508b51
6
+ metadata.gz: c2885be41a0cda5efb42ee408f2ec24fc6da741a9460c50a4bb3376a7f70463748960de0be1c7a5f7822592d04c4b79b50f4e90ebe4b1c0964e2d4acffd28be8
7
+ data.tar.gz: b861f941a2082d9c02119bb404e96dd121b739315df0188d31382adccf6810f85b57289c3675fcb1e07bc287387720a33df8892c1e02a13f6f524f76e0b41d97
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem 'rmagick'
4
- gem 'trollop'
4
+ gem 'optimist'
data/README.md CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  **RSyntaxTree** is a graphical syntax tree generator written in the Ruby programming language inspired by [phpSyntaxTree](http://ironcreek.net/phpsyntaxtree/).
4
4
 
5
- - - -
6
-
7
5
  ### Web Interface
8
6
 
9
7
  Working web interface of is available at <https://yohasebe.com/rsyntaxtree>.
@@ -17,7 +15,7 @@ Working web interface of is available at <https://yohasebe.com/rsyntaxtree>.
17
15
  For the web interface, see Usage section of <https://yohasebe.com/rsyntaxtree>.
18
16
 
19
17
  For the command-line interface, type `$rsyntaxtree -h` after installation. Here's what you get:
20
-
18
+
21
19
  ```text
22
20
  RSyntaxTree, (linguistic) syntax tree generator written in Ruby.
23
21
 
@@ -27,14 +25,15 @@ where [options] are:
27
25
  -o, --outdir=<s> Output directory (default: ./)
28
26
  -f, --format=<s> Output format: png, pdf, or svg (default: png)
29
27
  -l, --leafstyle=<s> visual style of tree leaves: auto, triangle, bar, or nothing (default: auto)
30
- -n, --fontstyle=<s> Font style (available when ttf font is specified): sans, serif, mono, cjk (default: sans)
28
+ -n, --fontstyle=<s> Font style: sans, serif, cjk (default: sans)
31
29
  -t, --font=<s> Path to a ttf font used to generate tree (optional)
32
30
  -s, --fontsize=<i> Size: 8-26 (default: 16)
33
- -m, --margin=<i> Margin: 0-120 (default: 0)
34
31
  -c, --color=<s> Color text and bars: on or off (default: on)
35
32
  -y, --symmetrize=<s> Generate symmetrical, balanced tree: on or off (default: on)
36
33
  -a, --autosub=<s> Put subscript numbers to nodes: on or off (default: off)
37
- -v, --version Print version and exit
34
+ -m, --margin=<i> Margin: 0-120 (default: 0)
35
+ -v, --vheight=<f> Connector Height: 0.5-2.0 (default: 1.0)
36
+ -e, --version Print version and exit
38
37
  -h, --help Show this message
39
38
  ```
40
39
 
@@ -46,17 +45,19 @@ There are several modes in which the connectors between terminal nodes and their
46
45
 
47
46
  You can put a subscript to any node by putting the _ character between the main label and the subscript. For example, NP_TOP will be rendered as NP<sub>TOP</sub>. Or you can select the &#8220;Auto subscript&#8221; option so that nodes of the same label will be automatically numbered. (e.g. NP<sub>1</sub>, NP<sub>2</sub>)</p>
48
47
 
48
+ See https://yohasebe.com/rsyntaxtree for more detailed info about the syntax.
49
+
49
50
  ### Example
50
51
 
51
52
  Bracket notation (auto-subscript-on):
52
53
 
53
54
  ```text
54
- [S [NP RSyntaxTree^][VP [V generates][NP multilingual syntax trees]]]
55
+ [S [NP RSyntaxTree^][VP [V generates][NP [Adj multilingual] [NP syntax trees]]]]
55
56
  ```
56
57
 
57
58
  Resulting PNG
58
59
 
59
- ![RSyntaxTree generates multilingual syntax trees](https://i.gyazo.com/8d8cbce1e8d8d45c0dda0741009641fd.png)
60
+ ![RSyntaxTree generates multilingual syntax trees](https://i.gyazo.com/6bb68b0bdb35d7a10c4a11d5788d484f.png)
60
61
 
61
62
  ### Development
62
63
 
data/bin/rsyntaxtree CHANGED
@@ -4,10 +4,10 @@
4
4
  $LOAD_PATH << File.join( File.dirname(__FILE__), '../lib')
5
5
 
6
6
  require 'rsyntaxtree'
7
- require 'trollop'
7
+ require 'optimist'
8
8
 
9
- opts = Trollop::options do
10
- version "RSyntaxTree #{RSyntaxTree::VERSION} (c) 2018 Yoichiro Hasebe"
9
+ opts = Optimist::options do
10
+ version "RSyntaxTree #{RSyntaxTree::VERSION} (c) 2021 Yoichiro Hasebe"
11
11
  banner <<-EOS
12
12
  RSyntaxTree, (linguistic) syntax tree generator written in Ruby.
13
13
 
@@ -18,17 +18,19 @@ EOS
18
18
 
19
19
  opt :outdir, "Output directory",
20
20
  :default => "./"
21
- opt :format, "Output format: png, pdf, or svg",
21
+ opt :format, "Output format: png, gif, jpg, pdf, or svg",
22
22
  :default => "png"
23
23
  opt :leafstyle, "visual style of tree leaves: auto, triangle, bar, or nothing",
24
24
  :default => "auto"
25
- opt :fontstyle, "Font style (available when ttf font is specified): sans, serif, mono, cjk",
25
+ opt :fontstyle, "Font style (available when ttf font is specified): sans, serif, cjk",
26
26
  :default => "sans"
27
27
  opt :font, "Path to a ttf font used to generate tree (optional)", :type => String
28
28
  opt :fontsize, "Size: 8-26",
29
29
  :default => 16
30
30
  opt :margin, "Margin: 0-120",
31
31
  :default => 0
32
+ opt :vheight, "Connector Height: 0.5-2.0",
33
+ :default => 1.0
32
34
  opt :color, "Color text and bars: on or off",
33
35
  :default => "on"
34
36
  opt :symmetrize, "Generate symmetrical, balanced tree: on or off",
@@ -37,16 +39,17 @@ EOS
37
39
  :default => "off"
38
40
  end
39
41
 
40
- Trollop::die :outdir, "must be an exsting directory path" unless FileTest::directory?(opts[:outdir])
41
- Trollop::die :format, "must be png or svg" unless /\A(png|pdf|svg)\z/ =~ opts[:format]
42
- Trollop::die :leafstyle, "must be auto, triangle, bar, or nothing" unless /\A(auto|triangle|bar|nothing)\z/ =~ opts[:leafstyle]
43
- Trollop::die :fontstyle, "must be sans, serif, mono, cjk" unless /\A(sans|serif|mono|cjk)\z/ =~ opts[:fontstyle]
44
- Trollop::die :font, "must be path to an existing ttf font" if opts[:font] && !File::exists?(opts[:font])
45
- Trollop::die :fontsize, "must be in the range 8-26" unless opts[:fontsize] >= 8 && opts[:fontsize] <= 26
46
- Trollop::die :margin, "must be in the range 0-120" if opts[:margin] < 0 || opts[:margin] > 120
47
- Trollop::die :color, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:color]
48
- Trollop::die :symmetrize, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:symmetrize]
49
- Trollop::die :autosub, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:autosub]
42
+ Optimist::die :outdir, "must be an exsting directory path" unless FileTest::directory?(opts[:outdir])
43
+ Optimist::die :format, "must be png, jpg, gif, or svg" unless /\A(png|jpg|gif|pdf|svg)\z/ =~ opts[:format]
44
+ Optimist::die :leafstyle, "must be auto, triangle, bar, or nothing" unless /\A(auto|triangle|bar|nothing)\z/ =~ opts[:leafstyle]
45
+ Optimist::die :fontstyle, "must be sans, serif, cjk" unless /\A(sans|serif|cjk)\z/ =~ opts[:fontstyle]
46
+ Optimist::die :font, "must be path to an existing ttf font" if opts[:font] && !File::exists?(opts[:font])
47
+ Optimist::die :fontsize, "must be in the range of 8-26" unless opts[:fontsize] >= 8 && opts[:fontsize] <= 26
48
+ Optimist::die :color, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:color]
49
+ Optimist::die :symmetrize, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:symmetrize]
50
+ Optimist::die :autosub, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:autosub]
51
+ Optimist::die :margin, "must be in the range of 0-120" if opts[:margin] < 0 || opts[:margin] > 120
52
+ Optimist::die :vheight, "must be in the range of 0.5-2.0" if opts[:vheight] < 0.5 || opts[:vheight] > 2.0
50
53
 
51
54
  string_opts = {}
52
55
  opts.each do |key, value|
@@ -68,6 +71,10 @@ outfile = File.new(File.expand_path(string_opts[:outdir]) + "/syntree." + ext, "
68
71
  case ext
69
72
  when "png"
70
73
  outfile.write rsg.draw_png
74
+ when "jpg"
75
+ outfile.write rsg.draw_jpg
76
+ when "gif"
77
+ outfile.write rsg.draw_gif
71
78
  when "pdf"
72
79
  outfile.write rsg.draw_pdf
73
80
  when "svg"
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/lib/rsyntaxtree.rb CHANGED
@@ -12,25 +12,11 @@
12
12
  # excellent program phpSyntaxTree.
13
13
  # Copyright (c) 2007-2018 Yoichiro Hasebe <yohasebe@gmail.com>
14
14
  # Copyright (c) 2003-2004 Andre Eisenbach <andre@ironcreek.net>
15
- #
16
- # This program is free software; you can redistribute it and/or modify
17
- # it under the terms of the GNU General Public License as published by
18
- # the Free Software Foundation; either version 2 of the License, or
19
- # (at your option) any later version.
20
- #
21
- # This program is distributed in the hope that it will be useful,
22
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
23
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
- # GNU General Public License for more details.
25
- #
26
- # You should have received a copy of the GNU General Public License
27
- # along with this program; if not, write to the Free Software
28
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
15
 
30
16
  $LOAD_PATH << File.join( File.dirname(__FILE__), 'rsyntaxtree')
31
17
 
32
- require 'uri'
33
- require 'imgutils'
18
+ require 'cgi'
19
+ require 'utils'
34
20
  require 'element'
35
21
  require 'elementlist'
36
22
  require 'string_parser'
@@ -38,10 +24,14 @@ require 'tree_graph'
38
24
  require 'svg_graph'
39
25
  require 'error_message'
40
26
  require 'version'
41
- require 'pp'
42
27
 
43
28
  FONT_DIR = File.expand_path(File.dirname(__FILE__) + "/../fonts")
44
29
 
30
+ ETYPE_UNDEFINED = 0
31
+ ETYPE_NODE = 1
32
+ ETYPE_LEAF = 2
33
+ SUBSCRIPT_CONST = 0.7
34
+
45
35
  class RSGenerator
46
36
  def initialize(params = {})
47
37
  new_params = {}
@@ -49,7 +39,7 @@ class RSGenerator
49
39
  key = keystr.to_sym
50
40
  case key
51
41
  when :data
52
- data = URI.unescape(value)
42
+ data = CGI.unescape(value)
53
43
  data = data.gsub('-AMP-', '&')
54
44
  .gsub('-PERCENT-', "%")
55
45
  .gsub('-PRIME-', "'")
@@ -57,31 +47,43 @@ class RSGenerator
57
47
  .gsub('-OABRACKET-', '<')
58
48
  .gsub('-CABRACKET-', '>')
59
49
  new_params[key] = data
50
+ new_params[:multibyte] = data.contains_cjk?
60
51
  when :symmetrize, :color, :autosub
61
52
  new_params[key] = value && value != "off" ? true : false
62
53
  when :fontsize
63
54
  new_params[key] = value.to_i
64
55
  when :margin
65
56
  new_params[key] = value.to_i
57
+ when :vheight
58
+ new_params[key] = value.to_f
66
59
  when :fontstyle
67
60
  if value == "noto-sans" || value == "sans"
68
- new_params[:font] = FONT_DIR + "/NotoSansCJKjp-Regular.otf"
61
+ new_params[:font] = FONT_DIR + "/NotoSans-Regular.ttf"
62
+ new_params[:font_it] = FONT_DIR + "/NotoSans-Italic.ttf"
63
+ new_params[:font_bd] = FONT_DIR + "/NotoSans-Bold.ttf"
64
+ new_params[:font_bdit] = FONT_DIR + "/NotoSans-BoldItalic.ttf"
65
+ new_params[:font_cjk] = FONT_DIR + "/NotoSansCJKjp-Regular.otf"
66
+ new_params[:fontstyle] = "sans"
69
67
  elsif value == "noto-serif" || value == "serif"
70
- new_params[:font] = FONT_DIR + "/NotoSerifCJKjp-Regular.otf"
71
- elsif value == "noto-mono" || value == "mono"
72
- new_params[:font] = FONT_DIR + "/NotoSansMonoCJKjp-Regular.otf"
73
- elsif value == "western-sans"
74
- new_params[:font] = FONT_DIR + "/DroidSans.ttf"
75
- elsif value == "western-serif"
76
- new_params[:font] = FONT_DIR + "/DroidSerif-Regular.ttf"
68
+ new_params[:font] = FONT_DIR + "/NotoSerif-Regular.ttf"
69
+ new_params[:font_it] = FONT_DIR + "/NotoSerif-Italic.ttf"
70
+ new_params[:font_bd] = FONT_DIR + "/NotoSerif-Bold.ttf"
71
+ new_params[:font_bdit] = FONT_DIR + "/NotoSerif-BoldItalic.ttf"
72
+ new_params[:font_cjk] = FONT_DIR + "/NotoSerifCJKjp-Regular.otf"
73
+ new_params[:fontstyle] = "serif"
77
74
  elsif value == "cjk zenhei" || value == "cjk"
78
75
  new_params[:font] = FONT_DIR + "/wqy-zenhei.ttf"
76
+ new_params[:font_it] = FONT_DIR + "/NotoSans-Italic.ttf"
77
+ new_params[:font_bd] = FONT_DIR + "/NotoSans-Bold.ttf"
78
+ new_params[:font_bdit] = FONT_DIR + "/NotoSans-BoldItalic.ttf"
79
+ new_params[:font_cjk] = FONT_DIR + "/wqy-zenhei.ttf"
80
+ new_params[:fontstyle] = "sans"
79
81
  end
80
82
  else
81
83
  new_params[key] = value
82
84
  end
83
85
  end
84
-
86
+
85
87
  @params = {
86
88
  :symmetrize => true,
87
89
  :color => true,
@@ -89,49 +91,80 @@ class RSGenerator
89
91
  :fontsize => 18,
90
92
  :format => "png",
91
93
  :leafstyle => "auto",
92
- :font => FONT_DIR + "/NotoSansCKjp-Regular.otf",
93
94
  :filename => "syntree",
94
95
  :data => "",
95
- :margin => 0
96
+ :margin => 0,
97
+ :vheight => 1.0,
98
+ :fontstyle => "sans",
99
+ :font => "/NotoSansCJKjp-Regular.otf",
100
+ :font_it => "/NotoSans-Italic.ttf",
101
+ :font_bd => "/NotoSans-Bold.ttf",
102
+ :font_bdit => "/NotoSans-BoldItalic.ttf",
96
103
  }
97
-
104
+ @metrics = {
105
+ :e_width => 120,
106
+ :e_padd => 14,
107
+ :v_space => 20,
108
+ :h_space => 20,
109
+ :b_side => 10,
110
+ :b_topbot => 10
111
+ }
112
+
98
113
  @params.merge! new_params
114
+ @params[:fontsize] = @params[:fontsize] * 2
115
+ @params[:margin] = @params[:margin] * 2
116
+ @metrics[:v_space] = @metrics[:v_space] * @params[:vheight]
99
117
  end
100
118
 
101
119
  def self.check_data(text)
102
120
  sp = StringParser.new(text)
103
121
  sp.valid?
104
122
  end
105
-
123
+
106
124
  def draw_png
107
125
  @params[:format] = "png"
108
126
  draw_tree
109
127
  end
110
128
 
129
+ def draw_jpg
130
+ @params[:format] = "jpg"
131
+ draw_tree
132
+ end
133
+
134
+ def draw_gif
135
+ @params[:format] = "gif"
136
+ draw_tree
137
+ end
138
+
111
139
  def draw_pdf
112
140
  @params[:format] = "pdf"
113
141
  draw_tree
114
142
  end
115
-
116
- def draw_tree
117
- sp = StringParser.new(@params[:data])
118
- sp.parse
119
- sp.auto_subscript if @params[:autosub]
120
- elist = sp.get_elementlist
121
- graph = TreeGraph.new(elist,
122
- @params[:symmetrize], @params[:color], @params[:leafstyle], @params[:font], @params[:fontsize], @params[:format], @params[:margin])
123
- graph.to_blob(@params[:format])
124
- end
125
-
143
+
126
144
  def draw_svg
127
145
  @params[:format] = "svg"
128
146
  sp = StringParser.new(@params[:data].gsub('&', '&amp;').gsub('%', '&#37;'))
129
147
  sp.parse
130
148
  sp.auto_subscript if @params[:autosub]
131
149
  elist = sp.get_elementlist
132
- graph = SVGGraph.new(elist,
133
- @params[:symmetrize], @params[:color], @params[:leafstyle], @params[:font], @params[:fontsize])
150
+ graph = SVGGraph.new(elist, @metrics,
151
+ @params[:symmetrize], @params[:color], @params[:leafstyle], @params[:multibyte],
152
+ @params[:fontstyle], @params[:fontsize],
153
+ )
134
154
  graph.svg_data
135
155
  end
156
+
157
+ def draw_tree
158
+ sp = StringParser.new(@params[:data])
159
+ sp.parse
160
+ sp.auto_subscript if @params[:autosub]
161
+ elist = sp.get_elementlist
162
+ graph = TreeGraph.new(elist, @metrics,
163
+ @params[:symmetrize], @params[:color], @params[:leafstyle], @params[:multibyte],
164
+ @params[:fontstyle], @params[:font], @params[:font_it], @params[:font_bd], @params[:font_bdit],
165
+ @params[:font_cjk], @params[:fontsize], @params[:margin],
166
+ )
167
+ graph.to_blob(@params[:format])
168
+ end
136
169
  end
137
170
 
@@ -10,26 +10,8 @@
10
10
  # This file is part of RSyntaxTree, which is a ruby port of Andre Eisenbach's
11
11
  # excellent program phpSyntaxTree.
12
12
  #
13
- # Copyright (c) 2007-2018 Yoichiro Hasebe <yohasebe@gmail.com>
13
+ # Copyright (c) 2007-2021 Yoichiro Hasebe <yohasebe@gmail.com>
14
14
  # Copyright (c) 2003-2004 Andre Eisenbach <andre@ironcreek.net>
15
- #
16
- # This program is free software; you can redistribute it and/or modify
17
- # it under the terms of the GNU General Public License as published by
18
- # the Free Software Foundation; either version 2 of the License, or
19
- # (at your option) any later version.
20
- #
21
- # This program is distributed in the hope that it will be useful,
22
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
23
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
- # GNU General Public License for more details.
25
- #
26
- # You should have received a copy of the GNU General Public License
27
- # along with this program; if not, write to the Free Software
28
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
-
30
- ETYPE_UNDEFINED = 0
31
- ETYPE_NODE = 1
32
- ETYPE_LEAF = 2
33
15
 
34
16
  class Element
35
17
 
@@ -50,7 +32,7 @@ class Element
50
32
  @triangle = false # draw triangle instead of stright bar when in auto mode
51
33
  end
52
34
  end
53
-
35
+
54
36
  # Debug helper function
55
37
  def dump
56
38
  printf( "ID : %d\n", @id );
@@ -10,22 +10,8 @@
10
10
  # This file is part of RSyntaxTree, which is a ruby port of Andre Eisenbach's
11
11
  # excellent program phpSyntaxTree.
12
12
  #
13
- # Copyright (c) 2007-2018 Yoichiro Hasebe <yohasebe@gmail.com>
13
+ # Copyright (c) 2007-2021 Yoichiro Hasebe <yohasebe@gmail.com>
14
14
  # Copyright (c) 2003-2004 Andre Eisenbach <andre@ironcreek.net>
15
- #
16
- # This program is free software; you can redistribute it and/or modify
17
- # it under the terms of the GNU General Public License as published by
18
- # the Free Software Foundation; either version 2 of the License, or
19
- # (at your option) any later version.
20
- #
21
- # This program is distributed in the hope that it will be useful,
22
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
23
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
- # GNU General Public License for more details.
25
- #
26
- # You should have received a copy of the GNU General Public License
27
- # along with this program; if not, write to the Free Software
28
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
15
 
30
16
  require 'element'
31
17
 
@@ -36,7 +22,7 @@ class ElementList
36
22
  @elements = Array.new # The element array
37
23
  @iterator = -1 # Iterator index (used for get_first / get_next)
38
24
  end
39
-
25
+
40
26
  def add(element)
41
27
  @elements << element
42
28
  if(element.parent != 0)
@@ -132,6 +118,4 @@ class ElementList
132
118
  end
133
119
  return maxlevel + 1;
134
120
  end
135
-
136
121
  end
137
-