1

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.

hope288
  • 725
  • 12
  • 23
  • I can tell you that Twitter API allows combinations with AND, OR, and NOT like the ones you are describing,as explained in the 1st link you provide. I didn't know the library you're using, but took a quick look to the document you provide and indeed it seems that the way to set the keywords with the method `set_keywords` stops you from adding complex combinations. As for alternatives, there are so many, and it's a matter of taste... `twython` is nice, although it does not go to a very high level. In `nltk` there will be soon one (at the moment you should point to github). Hope it helps. – lrnzcig Jul 01 '15 at 10:30

1 Answers1

1

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?

Leb
  • 15,483
  • 10
  • 56
  • 75
  • would the last query mean that dog would NOT be in the tweet containing "chocolate and cat" and it would NOT be in the tweet for "super AND ninja AND ghost AND person"? or would it mean that dog is not in the second line only (the part with "super AND ninja AND ghost AND person")? Also thanks! – hope288 Jul 06 '15 at 13:47
  • Glad to help. `q="chocolate cat -dog OR super ninja ghost person -dog"` would indicate that dog is not present in both of those statements "chocolate AND cat" also "super AND ninja AND ghost AND person". However, in `q="chocolate cat OR super ninja ghost person -dog"` dog is *could* be present in "chocolate AND cat" but *absent* in "super AND ninja AND ghost AND person". Did that clarify it? – Leb Jul 06 '15 at 14:39
  • Hey @Leb I just tried running this query: "nap cat -dog OR time breakfast -tired" but I am getting breakfast in a sentence with nap and cat. Here are some samples of my output: "the cat woke me at 530 for breakfast.. I couldn't get back to sleep. I see a nap in my future.. but not now. thanks cat!" "Me back form a lovely long cat nap..so who's going to be nice and make me breakfast? " Thanks for you help thus far! – hope288 Jul 28 '15 at 15:49
  • I edited my answer, let me know if it answers your question. – Leb Aug 01 '15 at 12:57
  • ohh ok I see what you mean, I guess it was a coincidence then. I was thinking that somehow it thought that I meant breakfast and cat had to be there since both sentences had cat when breakfast was mentioned. Thank you!! – hope288 Aug 05 '15 at 17:19