Sha256: 2380e87aef39b4ae077e69835f6d721419c03bb804f12690adb7a4ea16aafd59

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

class SlackSmartBot
  def get_keywords(sentence, list_avoid: [])
    require "engtagger"
    keywords = []
    unless sentence.to_s.strip.empty?
      # Initialize the POS tagger
      tagger = EngTagger.new
      tagged_sentence = tagger.add_tags(sentence)
      unless tagged_sentence.nil?

        # Extract nouns and proper nouns from the sentence
        nouns = tagger.get_nouns(tagged_sentence).keys
        proper_nouns = tagger.get_proper_nouns(tagged_sentence).keys
        adjectives = tagger.get_adjectives(tagged_sentence).keys
        ids = sentence.scan(/([\w]+\-[\w\-]+)/)

        # Combine nouns and proper nouns to create the list of keywords
        keywords = (nouns + proper_nouns + adjectives + ids.flatten).uniq

        #delete all keywords that are one or two characters long
        keywords.delete_if { |keyword| keyword.length < 3 }
        # delete all keywords that are in the list_avoid /word/i
        if !list_avoid.empty?
          keywords.delete_if { |keyword| list_avoid.any? { |avoid| keyword.match?(/#{avoid}/i) } }
        end

        #remove special characters from the keywords
        keywords.map! { |keyword| keyword.gsub(/[^\w\-_]/i, "") }
      end
    end
    return keywords
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slack-smart-bot-1.15.25 lib/slack/smart-bot/utils/get_keywords.rb