2017-03-13 3 views
1

Twitter gemを使用して、画像を含む特定のハッシュタグを持つ最新のつぶやきのリストを生成しています。Twitter API:ハッシュタグの末尾にある句読点はどのように一致させるのですか?

これはうまくいっていますが、人が自分のつぶやきのハッシュタグに句読点を追加すると、APIは検索結果にその句点を含めません。 、説明するために、私は#sourceconを検索したとき、それは#sourcecon.または#sourcecon!ためのAPIを経由して別の検索を実行#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のハッシュタグの一部ではないものとして句読点を扱うことを知っています。 TwitterのAPIから:句読点が#hashtagまたは@mentionの一部とは見なされません

注ことなので、句読点を含むトラックの用語は、#ハッシュや@メンションのいずれかと一致しません。

しかし、それは、それはそれを完全に無視して、すべての結果を返すことを意味べきではありません(ツイートで追加句読点を含めるものも含めて?)を

ことをここで検索結果を取得する方法を誰もが知っています最後に句読点の有無にかかわらずハッシュタグの言及を含むでしょうか?

答えて

2

ツイッター検索では、句読点や特殊文字は検索語の一部とみなされるため、「#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!') 
+0

、それは私が何をしたいの反対のようなものです。私は分離することはない、句読点を持つものも含めて、ハッシュタグを使用して、すべての検索結果を取得しようとしています – dmanaster

+0

混乱のために申し訳ありません、答えを少しもっと意味をなさないように更新しました。 –

関連する問題