Class: MindWords
- Inherits:
-
Object
- Object
- MindWords
- Defined in:
- lib/mindwords.rb
Instance Attribute Summary collapse
-
#filepath ⇒ Object
Returns the value of attribute filepath.
-
#lines ⇒ Object
Returns the value of attribute lines.
Instance Method Summary collapse
- #add(line) ⇒ Object
- #breadcrumb ⇒ Object
- #element(id) ⇒ Object
- #hashtags ⇒ Object
-
#initialize(raws = '', parent: nil, debug: false) ⇒ MindWords
constructor
A new instance of MindWords.
-
#reflect(raws) ⇒ Object
Accepts a list of words with the aim of returning a MindWords document using matched words with hashtags from the existing MindWords document.
- #save(file = @filepath) ⇒ Object
- #search(keyword) ⇒ Object
- #sort ⇒ Object
- #sort! ⇒ Object
- #tag_sort ⇒ Object
- #tag_sort! ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_hashtags ⇒ Object
- #to_outline(sort: true) ⇒ Object (also: #to_tree)
- #to_s(colour: false) ⇒ Object
- #to_words ⇒ Object
- #to_xml ⇒ Object
Constructor Details
#initialize(raws = '', parent: nil, debug: false) ⇒ MindWords
Returns a new instance of MindWords.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mindwords.rb', line 25 def initialize(raws='', parent: nil, debug: false) @parent, @debug = parent, debug s, type = RXFHelper.read raws @filepath = raws if type == :file or type == :dfs @lines = s.strip.gsub(/^\n/,'').lines @lines.shift if @lines.first =~ /<\?mindwords\?>/ end |
Instance Attribute Details
#filepath ⇒ Object
Returns the value of attribute filepath.
23 24 25 |
# File 'lib/mindwords.rb', line 23 def filepath @filepath end |
#lines ⇒ Object
Returns the value of attribute lines.
23 24 25 |
# File 'lib/mindwords.rb', line 23 def lines @lines end |
Instance Method Details
#add(line) ⇒ Object
37 38 39 |
# File 'lib/mindwords.rb', line 37 def add(line) @lines << line end |
#breadcrumb ⇒ Object
41 42 43 |
# File 'lib/mindwords.rb', line 41 def () @parent.attributes[:breadcrumb].split(/ +\/ +/) if @parent end |
#element(id) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/mindwords.rb', line 46 def element(id) doc = Rexle.new(to_xml()) e = doc.root.element("//*[@id='#{id}']") #e.attributes[:breadcrumb].to_s if e end |
#hashtags ⇒ Object
54 55 56 |
# File 'lib/mindwords.rb', line 54 def () @parent.attributes[:hashtags].split if @parent end |
#reflect(raws) ⇒ Object
Accepts a list of words with the aim of returning a MindWords document using matched words with hashtags from the existing MindWords document.
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 |
# File 'lib/mindwords.rb', line 70 def reflect(raws) h = to_h missing_words = [] # add the tags from the main list a = raws.strip.lines.map do |x| if h[x.chomp] then [x.chomp, h[x.chomp]] else missing_words << x nil end end.compact # add any linkage words from the tags # a.map(&:last).flatten(1).each do |s| a << [s, h[s]] if h[s] end # remove suplicates lines and transform it into a raw mindwords format # raws3 = a.uniq.map {|s,| [s, .map {|x| '#' + x }.join(' ')].join(' ') }.join("\n") [MindWords.new(raws3), missing_words] end |
#save(file = @filepath) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/mindwords.rb', line 58 def save(file=@filepath) return if @lines.empty? puts 'before save' if @debug File.write file, to_s() end |
#search(keyword) ⇒ Object
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 |
# File 'lib/mindwords.rb', line 102 def search(keyword) a = @lines.grep(/#{keyword}/i).map do |line| puts 'line: ' + line.inspect if @debug words = line.split r = words.grep /#{keyword}/i i = words.index r[0] [line, i] end return nil if a.empty? #return a[0][0] if a.length < 2 a2 = a.sort_by(&:last).map(&:first) puts 'a2: ' + a2.inspect if @debug e = element(keyword.downcase.gsub(/ +/,'-')) return nil if e.nil? MindWords.new(a2.join, parent: e, debug: @debug) end |
#sort ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mindwords.rb', line 129 def sort() s = @lines.sort.join def s.to_s() self.lines.map do |x| title, = x.split(/(?=#)/,2) title + .chomp.brown end.join("\n") end return s end |
#sort! ⇒ Object
142 143 144 145 |
# File 'lib/mindwords.rb', line 142 def sort!() @lines = sort().lines self end |
#tag_sort ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/mindwords.rb', line 147 def tag_sort() h = @lines.group_by {|x| x[/#\w+/]} s = h.sort.map {|key, value| value.sort }.join def s.to_s() self.lines.map do |x| title, = x.split(/(?=#)/,2) title + .chomp.brown end.join("\n") end return s end |
#tag_sort! ⇒ Object
163 164 165 166 |
# File 'lib/mindwords.rb', line 163 def tag_sort!() @lines = tag_sort().lines self end |
#to_a ⇒ Object
168 169 170 171 172 173 174 175 |
# File 'lib/mindwords.rb', line 168 def to_a() @lines.map do |x| s, = x.split(/(?= #)/,2) [s, .scan(/(?<=#)\w+/)] end end |
#to_h ⇒ Object
177 178 179 |
# File 'lib/mindwords.rb', line 177 def to_h() to_a.to_h end |
#to_hashtags ⇒ Object
181 182 183 |
# File 'lib/mindwords.rb', line 181 def () @hashtags end |
#to_outline(sort: true) ⇒ Object Also known as: to_tree
185 186 187 188 |
# File 'lib/mindwords.rb', line 185 def to_outline(sort: true) build() sort ? a2tree(tree_sort(LineTree.new(@outline).to_a)) : @outline end |
#to_s(colour: false) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/mindwords.rb', line 192 def to_s(colour: false) header = "<?mindwords?>\n\n" return header + @lines.map(&:chomp).join("\n") unless colour body = @lines.map do |x| title, = x.split(/(?=#)/,2) title + .chomp.brown end.join("\n") header + body end |
#to_words ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/mindwords.rb', line 206 def to_words() h = {} Rexle.new(to_xml).root.each_recursive do |e| h[e.attributes[:title]] = { breadcrumb: e.attributes[:breadcrumb], hashtags: e.attributes[:hashtags] } end h end |
#to_xml ⇒ Object
223 224 225 226 |
# File 'lib/mindwords.rb', line 223 def to_xml() build() unless @xml @xml end |