Module: SpaCy

Defined in:
lib/rbbt/nlp/spaCy.rb

Constant Summary collapse

TOKEN_PROPERTIES =
%w(lemma_ is_punct is_space shape_ pos_ tag_)
CHUNK_PROPERTIES =
%w(lemma_)

Class Method Summary collapse

Class Method Details

.chunk_dep_graph(text, reverse = false, lang = 'en_core_web_md') ⇒ Object



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/rbbt/nlp/spaCy.rb', line 116

def self.chunk_dep_graph(text, reverse = false, lang = 'en_core_web_md')
  associations = dep_graph(text, false, lang)

  chunks = self.chunk_segments(text, lang)
  tokens = self.segments(text, lang)
  index = Segment.index(tokens + chunks)

  chunks.each do |chunk|
    target_token_ids = chunk.dep.split(";").collect do|dep|
      type, target_pos = dep.split("->")
      index[target_pos.to_i]
    end.flatten

    target_tokens = target_token_ids.collect do |target_token_id|
      range = Range.new(*target_token_id.split(":").last.split("..").map(&:to_i))
      range.collect do |pos|
        index[pos]
      end.uniq
    end.flatten
    associations[chunk.segid] = target_tokens
  end

  if reverse
    old = associations.dup
    old.each do |s,ts|
      ts.each do |t|
        associations[t] ||= []
        associations[t] += [s] unless associations[t].include?(s)
      end
    end
  end

  associations
end

.chunk_segments(text, lang = 'en_core_web_sm') ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rbbt/nlp/spaCy.rb', line 67

def self.chunk_segments(text, lang = 'en_core_web_sm')
  docid = text.docid if Document === text
  corpus = text.corpus if Document === text 
  chunks = self.chunks(text, lang).collect do |chunk|
    info = {}
    CHUNK_PROPERTIES.each do |p|
      info[p] = chunk.instance_eval(p.to_s)
    end
    start = eend =  nil
    deps = []
    RbbtPython.iterate chunk.__iter__ do |token|
      start = token.idx if start.nil?
      eend = start + chunk.text.length if eend.nil?
      deps << token.idx.to_s + ":" + token.dep_ + "->" + token.head.idx.to_s if token.head.idx < start || token.head.idx > eend
    end
    info[:type] = "SpaCy"
    info[:offset] = chunk.__iter__.__next__.idx
    info[:dep] = deps * ";"
    info[:docid] = docid if docid
    info[:corpus] = corpus if corpus
    SpaCySpan.setup(chunk.text, info)
  end

  chunks
end

.chunks(text, lang = 'en_core_web_sm') ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rbbt/nlp/spaCy.rb', line 33

def self.chunks(text, lang = 'en_core_web_sm')

  tokens = []
  nlp = nlp(lang)

  doc = nlp.call(text)
  chunks = doc.noun_chunks.__iter__

  RbbtPython.iterate chunks do |item|
    tokens << item
  end

  tokens
end

.config(base, target = nil) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rbbt/nlp/spaCy.rb', line 166

def self.config(base, target = nil)
  TmpFile.with_file(base) do |baseconfig|
    if target
      CMD.cmd(:spacy, "init fill-config #{baseconfig} #{target}")
    else
      TmpFile.with_file do |tmptarget|
        CMD.cmd(:spacy, "init fill-config #{baseconfig} #{tmptarget}")
        Open.read(targetconfig)
      end
    end
  end
end

.dep_graph(text, reverse = false, lang = 'en_core_web_md') ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rbbt/nlp/spaCy.rb', line 93

def self.dep_graph(text, reverse = false, lang = 'en_core_web_md')
  tokens = self.segments(text, lang)
  index = Segment.index(tokens)
  associations = {}
  tokens.each do |token|
    type, target_pos = token.dep.split("->")
    target_tokens = index[target_pos.to_i]
    associations[token.segid] = target_tokens
  end

  if reverse
    old = associations.dup
    old.each do |s,ts|
      ts.each do |t|
        associations[t] ||= []
        associations[t] += [s] unless associations[t].include?(s)
      end
    end
  end

  associations
end

.nlp(lang = 'en_core_web_md') ⇒ Object



12
13
14
15
16
17
# File 'lib/rbbt/nlp/spaCy.rb', line 12

def self.nlp(lang = 'en_core_web_md')
  @@nlp ||= {}
  @@nlp[lang] ||= RbbtPython.run :spacy do
    spacy.load(lang)
  end
end

.paths(text, source, target, reverse = true, lang = 'en_core_web_md') ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rbbt/nlp/spaCy.rb', line 151

def self.paths(text, source, target, reverse = true, lang = 'en_core_web_md')
  graph = SpaCy.chunk_dep_graph(text, reverse, lang)

  chunk_index = Segment.index(SpaCy.chunk_segments(text, lang))

  source_id = chunk_index[source.offset.to_i].first || source.segid
  target_id = chunk_index[target.offset.to_i].first || target.segid

  path = Paths.dijkstra(graph, source_id, [target_id])

  return nil if path.nil?

  path.reverse
end

.segments(text, lang = 'en_core_web_sm') ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rbbt/nlp/spaCy.rb', line 48

def self.segments(text, lang = 'en_core_web_sm')
  docid = text.docid if Document === text
  corpus = text.corpus if Document === text 
  tokens = self.tokens(text, lang).collect do |token|
    info = {}
    TOKEN_PROPERTIES.each do |p|
      info[p] = token.instance_eval(p.to_s)
    end
    info[:type] = "SpaCy"
    info[:offset] = token.idx
    info[:dep] = token.dep_ + "->" + token.head.idx.to_s
    info[:docid] = docid if docid
    info[:corpus] = corpus if corpus
    SpaCyToken.setup(token.text, info)
  end

  tokens
end

.tokens(text, lang = 'en_core_web_sm') ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rbbt/nlp/spaCy.rb', line 19

def self.tokens(text, lang = 'en_core_web_sm')

  tokens = []

  nlp = nlp(lang)
  doc = nlp.call(text)

  doc.__len__.times do |i|
    tokens << doc.__getitem__(i)
  end

  tokens
end