【问题标题】:Twitter API: How do I match punctuation at the end of a hashtag?Twitter API:如何匹配主题标签末尾的标点符号?
【发布时间】:2017-08-03 19:28:06
【问题描述】:

我正在使用Twitter gem 生成最近的推文列表,其中包含包含图像的特定主题标签。

它工作正常,但我注意到当人们在推文中将标点符号附加到主题标签时,API 不会将它们包含在我的搜索结果中。举例来说,当我搜索#sourcecon 时,它不包括使用#sourcecon! 的推文

通过 API 对 #sourcecon.#sourcecon! 运行单独搜索没有帮助 - 它会忽略标点并生成相同的列表。

我的代码在这里:

twitter_client.search("'#sourcecon' filter:images", result_type: "recent", :since_id => last_tweet).collect

twitter_client.search("'#sourcecon!' filter:images", result_type: "recent", :since_id => last_tweet).collect

我知道 Twitter 将标点符号视为不属于主题标签的一部分。来自推特 API:

请注意,标点符号不被视为#hashtag 或@mention 的一部分,因此包含标点符号的轨道术语不会匹配#hashtags 或@mentions。

但这不应该意味着它会完全忽略它并返回所有结果(包括那些在推文中包含附加标点符号的结果?)

有谁知道如何在此处获得搜索结果,其中包括在结尾处包含和不包含标点符号的主题标签的提及?

【问题讨论】:

    标签: ruby twitter


    【解决方案1】:

    使用 twitter 搜索时,标点符号和特殊字符将被视为您正在搜索的术语的一部分,因此请搜索“#twitter!”将返回“#twitter!”、“twitter?”、“#twitter”等。您可以做的是检查搜索是否包含任何类型的标点符号,如果包含,您可以对数组进行排序以首先添加这些推文.

    require 'twitter'
    
    module TwitterSearch
      extend self
    
      @twiiter_client = Twitter::REST::Client.new do |config|
        config.consumer_key        = ""
        config.consumer_secret     = ""
        config.access_token        = ""
        config.access_token_secret = ""
      end
    
      # search returns  
      # Check out what @researchgoddess is up to at #sourcecon! 
      # What a welcome from @SourceCon! Thanks @CareerBuilder for hosting.#   
      # RT @JRoberts257: Happy hour at #SourceCon! Thanks @CareerBuilder for 
      # Happy hour at #SourceCon! Thanks @CareerBuilder for sponsoring. ht
      # @RT @cybsearchjoe: #SourceCon is rocking
      # etc 
    
      def search(text)
        tweets = @twitter_client.search("#{text} filter:images", result_type: "recent").take(30).collect do |tweet|
            "#{tweet.text}"
        end
        # looks to see if there is puncuation at the end of the text "!.?{}[]" It will ignore the # at the beginning 
        tweets = sort_tweets(text, tweets) if text[1..text.length] =~ /[[:punct:]]/
        puts tweets 
      end
    
    
      # sorts tweets based off index given in match_phrase 
      def sort_tweets(text, tweets)
        tweets.sort do |phrase, other_phrase| 
          match_phrase(phrase, text, tweets) <=> match_phrase(other_phrase, text, tweets) 
        end
      end
    
      # if phrase matches punc_text(text) the phrase will be inserted at the beginning of the array else it will return its previous index. 
      def match_phrase(phrase, text, tweets)
        phrase.match(/#{punc_text(text)}/i).nil? ? tweets.index(phrase) + 1 : 0 
      end
    
      # adds backslash to punctuation '#sourcecon//?|!|.'
      def punc_text(text)
        text[1..text.length].gsub(/([[:punct:]])/){|punc| "\\#{punc}"}
      end
    end
    
    TwitterSearch.search('#sourcecon!')
    

    【讨论】:

    • 如果我理解正确,这与我想要做的相反。我正在尝试获取使用主题标签的所有内容的搜索结果,包括带有标点符号的内容,而不是将它们分开。
    • 抱歉造成混淆,更新了答案以使其更有意义。
    猜你喜欢
    • 2017-09-30
    • 1970-01-01
    • 2018-06-22
    • 2017-05-14
    • 1970-01-01
    • 2012-01-20
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多