Class: MindWords

Inherits:
Object
  • Object
show all
Includes:
RXFReadWriteModule
Defined in:
lib/mindwords.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raws = '', parent: nil, debug: false) ⇒ MindWords

Returns a new instance of MindWords.



29
30
31
32
33
34
35
# File 'lib/mindwords.rb', line 29

def initialize(raws='', parent: nil, debug: false)

  @parent, @debug = parent, debug

  import(raws) if raws.length > 1

end

Instance Attribute Details

#filepathObject

Returns the value of attribute filepath.



27
28
29
# File 'lib/mindwords.rb', line 27

def filepath
  @filepath
end

#linesObject

Returns the value of attribute lines.



27
28
29
# File 'lib/mindwords.rb', line 27

def lines
  @lines
end

#words_missingObject (readonly)

Returns the value of attribute words_missing.



26
27
28
# File 'lib/mindwords.rb', line 26

def words_missing
  @words_missing
end

Instance Method Details

#add(s) ⇒ Object



37
38
39
40
41
# File 'lib/mindwords.rb', line 37

def add(s)

  @lines.concat s.strip.lines

end


43
44
45
# File 'lib/mindwords.rb', line 43

def breadcrumb()
  @parent.attributes[:breadcrumb].split(/ +\/ +/) if @parent
end

#element(id) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/mindwords.rb', line 52

def element(id)

  doc = Rexle.new(to_xml())
  e =  doc.root.element("//*[@id='#{id}']")
  #e.attributes[:breadcrumb].to_s if e

end

#hashtags(title = nil) ⇒ Object

If title supplied, searches for a requested title and returns the

associated hashtags

When no title is supplied, it will return the hashtags for the

parent element of a search result object


65
66
67
68
69
70
71
72
73
74
# File 'lib/mindwords.rb', line 65

def hashtags(title=nil)

  if title then
    found = search(title)
    found.hashtags() if found
  else
    @parent.attributes[:hashtags].split if @parent and @parent.attributes[:hashtags]
  end

end

#headingsObject



47
48
49
# File 'lib/mindwords.rb', line 47

def headings()
  breadcrumb[0..-2]
end

#import(raws) ⇒ Object



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
# File 'lib/mindwords.rb', line 76

def import(raws)

  s, type = RXFReader.read raws

  @filepath = raws if type == :file or type == :dfs
  rawlines = (s.strip.gsub(/(^\n|\r)/,'') + "\n").lines.uniq
  rawlines.shift if rawlines.first =~ /<\?mindwords\?>/

  # remove mindwords lines which don't have a hashtag
  lines = rawlines.reject do |line|

    found = line[/^\w+[^#]+$/]

    if found then
      puts ('no hashtag found on this line -> ' + line).warn
    end

    found

  end

  #--- handle indented text, indicated there are *groups* of words

  s2 = lines.join.gsub(/\n\s*$/,'')
  puts s2 if @debug
  a = s2.strip.split(/(?=^\w+)/)
  a2 = a.inject([]) do |r,x|

    if x =~ /\n\s+/ then

      a4 = x.lines[1..-1].map do |line|

        puts 'x.lines[0]: ' + x.lines[0].inspect if @debug
        hashtag = if x.lines[0][/ /] then
          x.lines[0].gsub(/\b\w/) {|x| x.upcase}.gsub(/ /,'')
        else
          x.lines[0]
        end

        "%s #%s" % [line.strip, hashtag]
      end

      r.concat a4

    else
      r << x
    end

  end

  #-- end of indented text handler

  @lines = a2.inject([]) do |r,line|

    # the following does 2 things:
    #      1. splits words separated by a bar (|) onto their own line
    #      2. prefixes a word with an underscore if the word is the
    #         same as the hashtag. That way it's not removed by the
    #         redundancy checker

    raw_words, raw_hashtags = line.split(/(?= #)/,2)
    words = raw_words.split(/ *\| */)
    hashtags = raw_hashtags.scan(/(?<=#)\w+/)

    words.each do |word|

      linex = (word +  raw_hashtags)
      r << (hashtags.include?(word) ? linex.sub!(/\b#{word}\b/, '_\0') \
            : linex)
    end

    r
  end
end

#linesplusObject

same as #lines but inludes the breadcrumb path; Helpful to identify which words don’t have a breadcrumb path.



160
161
162
163
164
165
166
167
# File 'lib/mindwords.rb', line 160

def linesplus()

  to_a.map do |word, _|
    r = search word
    r ? [word, r.breadcrumb] : [r, nil]
  end

end

#lookup(s) ⇒ Object

helpful when searching for a word itself using autosuggest



153
154
155
# File 'lib/mindwords.rb', line 153

def lookup(s)
  self.to_words.keys.sort.grep /^#{s}/i
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.



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
# File 'lib/mindwords.rb', line 182

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,tags| [s, tags.map {|x| '#' + x }.join(' ')].join(' ') }.join("\n")

  [MindWords.new(raws3), missing_words]

end

#save(file = @filepath) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/mindwords.rb', line 169

def save(file=@filepath)

  return if @lines.empty?

  puts 'before save' if @debug

  FileX.write file, to_s()

end

#search(keyword, succinct: true) ⇒ Object



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
# File 'lib/mindwords.rb', line 214

def search(keyword, succinct: true)

  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 MindWords.new(a2.uniq.join,  debug: @debug) if e.nil?

  # find and add any linkage support lines
  #

  a3 = []

  a2.each do |line|

    line.chomp.scan(/#[^ ]+/).each do |hashtag|

      puts 'hashtag: ' + hashtag.inspect  if @debug
      r2 = @lines.grep(/^#{hashtag[1..-1]} #/)
      a3 << r2.first if r2

    end
  end

  puts 'a2: ' + a2.inspect if @debug
  a2.concat a3

  if succinct then
    MindWords.new(a2.uniq.join, parent: e, debug: @debug)
  else
    MindWords.new(a2.uniq.join,  debug: @debug)
  end

end

#sortObject



264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/mindwords.rb', line 264

def sort()
  s = @lines.sort.join

  def s.to_s()
    self.lines.map do |x|
      title, hashtags = x.split(/(?=#)/,2)
      title + hashtags.chomp.brown
    end.join("\n")
  end

  return s
end

#sort!Object



277
278
279
280
# File 'lib/mindwords.rb', line 277

def sort!()
  @lines = sort().lines
  self
end

#tag_sortObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/mindwords.rb', line 282

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, hashtags = x.split(/(?=#)/,2)
      title + hashtags.chomp.brown
    end.join("\n")
  end

  return s

end

#tag_sort!Object



298
299
300
301
# File 'lib/mindwords.rb', line 298

def tag_sort!()
  @lines = tag_sort().lines
  self
end

#to_aObject



303
304
305
306
307
308
309
310
# File 'lib/mindwords.rb', line 303

def to_a()

  @lines.map do |x|
    s, rawtags = x.split(/(?= #)/,2)
    [s, rawtags.scan(/(?<=#)\w+/)]
  end

end

#to_hObject



312
313
314
# File 'lib/mindwords.rb', line 312

def to_h()
  to_a.to_h
end

#to_hashtagsObject



316
317
318
# File 'lib/mindwords.rb', line 316

def to_hashtags()
  @hashtags
end

#to_outline(sort: true) ⇒ Object Also known as: to_tree



320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/mindwords.rb', line 320

def to_outline(sort: true)

  build()

  if sort then
    a = LineTree.new(@outline).to_a
    puts ('a: ' + a.inspect).debug if @debug
    a2tree(tree_sort(a))
  else
    @outline
  end

end

#to_s(colour: false) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/mindwords.rb', line 336

def to_s(colour: false)

  header = "<?mindwords?>\n\n"
  return header + @lines.map(&:chomp).join("\n") unless colour

  body = @lines.map do |x|
      title, hashtags = x.split(/(?=#)/,2)
      title + hashtags.chomp.brown
  end.join("\n")

  header + body

end

#to_wordsObject



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/mindwords.rb', line 350

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_xmlObject



367
368
369
370
# File 'lib/mindwords.rb', line 367

def to_xml()
  build() unless @xml
  @xml
end