Class: PROIEL::Commands::Grep

Inherits:
PROIEL::Command show all
Defined in:
lib/proiel/cli/commands/grep.rb

Class Method Summary collapse

Methods inherited from PROIEL::Command

inherited, subclasses

Class Method Details

.init_with_program(prog) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/proiel/cli/commands/grep.rb', line 5

def init_with_program(prog)
  prog.command(:grep) do |c|
    c.syntax 'grep [options] pattern filename(s)'
    c.description 'Search the text'

    c.option 'level', '--level LEVEL', 'Select level to match. LEVEL should be "token" or "sentence" (default)'
    c.option 'nostrip', '--nostrip', 'Do not strip whitespace from start and beginning of strings'
    c.option 'nosubst', '--nosubst', 'Do not substitute whitespace sequences with a single space'
    c.option 'nocolour', '--nocolour', 'Do not colour code matches'
    c.option 'ignore-case', '-i', '--ignore-case', 'Ignore uppercase/lowercase'

    c.action { |args, options| process(args, options) }
  end
end

.match(s, citation, object_id, pattern, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/proiel/cli/commands/grep.rb', line 20

def match(s, citation, object_id, pattern, options)
  s.strip! unless options['nostrip']
  s.gsub!(/\s+/, ' ') unless options['nosubst']

  if s[pattern]
    s.gsub!(pattern) { |m| m.yellow } unless options['nocolour']

    puts "#{citation} (ID = #{object_id}) #{s}"
  end
end

.merge_citation_parts(citation_upper, citation_lower_start, citation_lower_end = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/proiel/cli/commands/grep.rb', line 31

def merge_citation_parts(citation_upper, citation_lower_start, citation_lower_end = nil)
  citation_lower =
    if citation_lower_start == citation_lower_end
      citation_lower_start
    else
      [citation_lower_start, citation_lower_end].compact.join('-')
    end

  [citation_upper, citation_lower].compact.join(' ')
end

.process(args, options) ⇒ Object



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
# File 'lib/proiel/cli/commands/grep.rb', line 90

def process(args, options)
  if args.empty?
    STDERR.puts 'Missing pattern. Use --help for more information.'
    exit 1
  end

  pattern_string = args.shift

  pattern =
    if options['ignore-case']
      Regexp.new(pattern_string, Regexp::IGNORECASE)
    else
      Regexp.new(pattern_string)
    end

  if args.empty?
    STDERR.puts 'Missing filename(s). Use --help for more information.'
    exit 1
  end

  options['level'] ||= 'sentence'

  unless %w(token sentence).include?(options['level'])
    STDERR.puts 'Invalid matching level. Use --help for more information.'
    exit 1
  end

  tb = PROIEL::Treebank.new

  args.each do |filename|
    STDERR.puts "Reading #{filename}...".green if options['verbose']

    tb.load_from_xml(filename)
  end

  tb.sources.each do |source|
    citation_upper = source.citation_part

    source.divs.each do |div|
      process_div(citation_upper, div, pattern, options)
    end
  end
end

.process_div(citation_upper, div, pattern, options) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/proiel/cli/commands/grep.rb', line 81

def process_div(citation_upper, div, pattern, options)
  case options['level']
  when 'sentence'
    process_div_for_sentences(citation_upper, div, pattern, options)
  when 'token'
    process_div_for_tokens(citation_upper, div, pattern, options)
  end
end

.process_div_for_sentences(citation_upper, div, pattern, options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/proiel/cli/commands/grep.rb', line 42

def process_div_for_sentences(citation_upper, div, pattern, options)
  div.sentences.each do |sentence|
    s = sentence.presentation_before || ''
    citation_lower_start = nil
    citation_lower_end = nil

    sentence.tokens.each do |token|
      unless token.is_empty?
        s += token.presentation_before || ''
        s += token.form || ''
        s += token.presentation_after || ''

        citation_lower_start = token.citation_part if citation_lower_start.nil?
        citation_lower_end = token.citation_part
      end
    end

    s += sentence.presentation_after || ''

    citation = merge_citation_parts(citation_upper, citation_lower_start, citation_lower_end)
    match(s, citation, sentence.id, pattern, options)
  end
end

.process_div_for_tokens(citation_upper, div, pattern, options) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/proiel/cli/commands/grep.rb', line 66

def process_div_for_tokens(citation_upper, div, pattern, options)
  div.sentences.each do |sentence|
    sentence.tokens.each do |token|
      unless token.is_empty?
        s = token.presentation_before || ''
        s += token.form || ''
        s += token.presentation_after || ''

        citation = merge_citation_parts(citation_upper, token.citation_part)
        match(s, citation, token.id, pattern, options)
      end
    end
  end
end