pythonpython-3.xtwitterpython-twitter

Complex queries with twitter API- python wrapper


Are complex queries possible using the twitter API? For example in the example queries shown in the dev site can I have my query be something like:

("chocolate" AND "cat") OR ("super ninja" AND "ghost" AND "person" but NOT "dog")

The queries in the site look really simple and I'm wondering if you can have multiple at the same time, AND as well as OR in a query or is it mutually exclusive?

I tried googling around but I could only find one word example queries (useless). does this mean that I can join the developer documentation examples from the site and mesh them in one query like so:

q="chocolate cat -dog OR super ninja ghost person -dog"

is the above query the same result as the first statement with the AND/OR?

I have been using TwitterSearch library but I can only do queries such as:

"chocolate" AND "cat"

"chocolate" OR "dog"

I might be able to do the following but haven't tested yet:

("chocolate" OR "cat") AND "person"

(page 22 TwitterSearch library doc).

Also the library allows for NOT but I can't tell if I can use both AND and OR then a NOT. This is also on page 22 under 'excepting keywords'

This is not complex enough to my taste, I am open to any other libraries/wrapper that have more complexity in their query searches but I am not sure if twitter even has this complexity in their API.

Thank you,and any links you have would be much appreciated as well.


Solution

  • Yes you are correct, you're able to combine the query operators in any way that fits your criteria.

    q="chocolate cat -dog OR super ninja ghost person -dog"

    However, in that one you're specifically remove dog in the first one, but you are not in:

    ("chocolate" AND "cat") OR ("super ninja" AND "ghost" AND "person" but NOT "dog")

    Don't you mean to have

    q="chocolate cat OR super ninja ghost person -dog"

    Either way, yes that will filter out the tweets based on the operators provided.

    From your comment:

    You are getting exactly what you're specifying.

    If we look at the "nap cat -dog" sentence first, you're filtering all tweets that must contain nap and cat in the same tweet excluding dog. They also contain any other word which in your case you're getting breakfast because you have no restriction for that in "nap cat -dog" filter.

    The second filter works separately from the first hence the OR statement. In there again you're saying all tweets that contain time and breakfast, but not dog. The second filter could have nap and cat in it.

    If you want to somehow link them then you'll need:

    q = "nap cat -dog -time -breakfast OR time breakfast - nap -cat -dog"

    That filter should prevent overlapping of tweets which I'm assuming what you're after. Correct?