class NameSpotter

Constants

VERSION

Public Class Methods

english?(text) click to toggle source
# File lib/name-spotter.rb, line 14
def self.english?(text)
  tweets = text.split(/\s+/).inject([]) do |res, w|
    if w.match(/[A-Za-z]/)
      if res.empty? || res[-1].size >=15
        res << [w]
      else
        res[-1] << w
      end
    end
    res
  end
  eng, not_eng = tweets.shuffle[0...50].partition do |a|
    UnsupervisedLanguageDetection.is_english_tweet?(a.join(" "))
  end
  percentage = eng.size.to_f/(not_eng.size + eng.size)
  percentage > 0.5
end
new(client) click to toggle source
# File lib/name-spotter.rb, line 32
def initialize(client)
  @client = client
end
version() click to toggle source
# File lib/name-spotter/version.rb, line 4
def self.version
  VERSION
end

Public Instance Methods

find(input, format = nil) click to toggle source
# File lib/name-spotter.rb, line 36
def find(input, format = nil)
  text = to_text(input)
  names = @client.find(text)
  names = names.map{ |n| n.to_hash }
  return { names: names } unless format
  format == "json" ? to_json(names) : to_xml(names)
end

Private Instance Methods

to_json(names) click to toggle source
# File lib/name-spotter.rb, line 49
def to_json(names)
  return JSON.fast_generate({ names: names })
end
to_text(input) click to toggle source
# File lib/name-spotter.rb, line 45
def to_text(input)
  input
end
to_xml(names) click to toggle source
# File lib/name-spotter.rb, line 53
def to_xml(names)
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.names do
      names.each do |name|
        xml.verbatim       name[:verbatim]
        xml.scientificName name[:scientificName]
        xml.offsetStart    name[:offsetStart]
        xml.offsetEnd      name[:offsetEnd]
      end
    end
  end
  builder.to_xml
end