Reserved words in Google Custom Search API - google-custom-search

I am working with the Google Search API, and I am running into some trouble. This request (in Python, using the requests library) works fine
res = requests.get("https://www.googleapis.com/customsearch/v1", params={
"cx": <key1>,
"key": <key2>,
"alt": "json",
"num": 2,
"q": "cat sock ship hero monkey baby match"
})
and returns results with the syntax according to the documentation
However, this request does not work:
res = requests.get("https://www.googleapis.com/customsearch/v1", params={
"cx": <key1>,
"key": <key2>,
"alt": "json",
"num": 2,
"q": "cat sock ship hero monkey footnoteref baby match"
})
it returns this:
{'kind': 'customsearch#search',
'queries': {'request': [{'count': 2,
'cx': '<key>',
'inputEncoding': 'utf8',
'outputEncoding': 'utf8',
'safe': 'off',
'searchTerms': 'cat sock ship hero monkey baby footnoteref match',
'title': 'Google Custom Search - cat sock ship hero monkey baby footnoteref match',
'totalResults': '0'}]},
'searchInformation': {'formattedSearchTime': '0.22',
'formattedTotalResults': '0',
'searchTime': 0.218722,
'totalResults': '0'},
'spelling': {'correctedQuery': 'cat sock ship hero monkey baby footnote ref match',
'htmlCorrectedQuery': 'cat sock ship hero monkey baby <b><i>footnote ref</i></b> match'},
'url': {'template': 'https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json',
'type': 'application/json'}}
The only difference between the two queries is that the latter has the word "footnoteref" in it. I did not find in the documentation anything about this word and its impact on the API's behavior. What is happening? Is there a way to disable this behavior, or a list of reserved words? For now, I am just going to remove the offending word from the query, but I am afraid I am going to play a whack-a-mole game of removing words each time other offending word pops out.

I searched on google for both "cat sock ship hero monkey footnoteref baby match" and "cat sock ship hero monkey baby match".
You said that "cat sock ship hero monkey footnoteref baby match" doesn't return anything, and that's because Google actually suggests a different search: 'cat sock ship hero monkey baby footnote ref match'.
When you don't have results, you should remove a word from the search (I will start with the last word) and try again. Or you should just try with the suggested search, like: 'cat sock ship hero monkey baby footnote ref match'.
The search works fast, I suggest you to implement the following technique:
a) Your search contains less than 3-4 words. You should repeat the
search but add a new word from google's 'correctedQuery'
suggestion.
b) Your search contains more than 4 words. You should remove the last
word or a "link word" like "for", "and".. and repeat the search.
Good luck.

Related

How do I make SpaCy choose noun chunks separated by "and" or "," as one

I'm sorry about the title, I really didn't know how to phrase it, but hopefully this example will make it clear.
Basically,
For the following sentence:
Ashley and Brian are drinking water.
I want the noun chunk to be "Ashley and Brian" instead it is, "Ashley", "Brian"
Another example is:
Types of clothes include shirts, pants and trousers.
I want the noun chunk to be "shirts, pants and trousers" instead of "shirts" "pants" "trousers"
How do I solve this problem?
What you are describing is not a noun chunk. The conjuncts feature is closer to what you want.
This might not work for complex sentences, but at least it'll cover your examples and typical cases.
import spacy
nlp = spacy.load("en_core_web_sm")
texts = [
"Ashley and Brian are drinking water.",
"Types of clothes include shirts, pants and trousers.",
]
for text in texts:
print("-----")
print(text)
checked = 0
doc = nlp(text)
for tok in doc:
if tok.i < checked: continue
if tok.pos_ not in ('NOUN', 'PROPN'): continue
if tok.conjuncts:
print(doc[tok.left_edge.i:tok.right_edge.i+1])
checked = tok.right_edge.i + 1

Pine Script TradingView Query

I'm quite new to PINE and I'm hoping for some assistance, or to be pointed in the right direction. My goal is to script BUY/SELL signals which will use a PERCENTAGE of an available sum for use with a trading bot via TradingView.
For example, starting capital $100, buy signal comes through, which triggers buy of of 25% ($25) worth of X coin.
I have been unable to find the code to setup the more complicated percentage-based buy/sell orders. Perhaps I'm blind, if so, tell me! :) The below code provided by TradingView would use all available capital in the account to make 1 x buy trade... which is not ideal.
{
"action": "BUY",
"botId": "1234"
}
I have tried this to add % but to no avail:
{
"action": "BUY" percent_of_equity 25,
"botId": "1234"
}
Any suggestions? Am I missing anything obvious?
Related Articles:
Quadency
TradingView
The code above is not related to pine-script, but is part of alarm/webhook action.
If you want to declare equity in your (back)order, inside pine-script, you have to use strategy like something this:
strategy(title = "example",
shorttitle = "myscript",
overlay = true,
pyramiding = 1,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 50,
initial_capital = 1000)
This get the type as percent of equity, which is 50% declared on qty_value

Query for substrings from freeform STT input

I have a PostgreSQL database with vocabulary in a table.
I want to receive Speech to Text (STT) input and query my vocabulary table for matches.
This is tricky since STT is somewhat free-form.
Let's say the table contains the following vocabulary and phrases:
How are you?
Hi
Nice to meet you
Hill
Nice
And the user is prompted to speak: "Hi, nice to meet you"
I transcribe their input as it comes in as "Hi nice to meet you" and query my database for individual vocabulary matches. I want to return:
[
{
id: 2,
word: "Hi"
},
{
id: 3,
word: "Nice to meet you"
}
]
I could query with wildcards where word ilike '%${term}% but then I'd need to pass in the correct substring so it'd find the match, e.g., where word ilike '%Hi%, but this may incorrectly return Hill. I could also split the spoken input by space, giving me ["Hi", "nice", "to", "meet", you"], and loop through each word looking for a match, but this may return Nice rather than the phrase Nice to meet you.
Q: How can I correctly pass substrings to a query and return accurate results for free-form speech?
Two PostgreSQL functions could help you here:
to_tsvector: creates a text search list of tokens (lexemes: unit of lexical meaning)
to_tsquery for querying the vector for occurrences of certain words or phrases.
See Mastering PostgreSQL Tools: Full-Text Search and Phrase Search
If that's not enough you need to turn to natural language processing (NLP).
Something like PyTextRank could help (something that goes beyond the bag-of-words technique):
import spacy
import pytextrank
text = "Hi, how are you?"
# load a spaCy model, depending on language, scale, etc.
nlp = spacy.load("en_core_web_sm")
# add PyTextRank to the spaCy pipeline
tr = pytextrank.TextRank()
nlp.add_pipe(tr.PipelineComponent, name="textrank", last=True)
doc = nlp(text)
# examine the top-ranked phrases in the document
for p in doc._.phrases:
print("{:.4f} {:5d} {}".format(p.rank, p.count, p.text))
print(p.chunks)

How to translate sentences using vuei18n?

Currently I have been able to translate only words, but in the case of wanting to translate an entire sentence, I don't know how to do it.
taking the following example json
{
"Hello":"Hola"
"how":"como"
"You go":"te va"
"text 4": "texto 4"
"text 5: "texto 5"
}
So when entering all the text "Hola como te va texto 4 texto 5", I must get "Hello how do you go text 4 text 5" as translation,but I only manage to do it by words, example:
<p>{{$t("Hola")}}</p>
get in response in the browser 'Hola', indeed
Lucky you, I just learned this about a few weeks ago.
So what you need is actually exist in the documentation. I believe it is called Linked Local Message.
So here's an example for you..
{
"Hello":"Hola",
"how":"comp",
"You_go":"te va",
"text4": "texto 4",
"text5: "texto 5",
"sentence": "#:Hello #:how #:You_go #:text4 #:text5"
}
Then try
$t('sentence')
So it will actually linked to the "Hello" part of your json. So if you only have #:Hello at sentence it will result in Hola. It's kind of hard to read, but it's the syntax.
And if you didn't understood, I just made a fiddle for your experiment related to the feature. It has two languages that is en and test that represent whatever language you use in the example. Here is the link to jsFiddle
Hope it helps, feel free to ask as well :D

eBay API categoryId in findItemsAdvanced call returns wrong categories

I'm trying to use the categoryId in my findItemsAdvanced query:
api.execute('findItemsAdvanced', {
'keywords': 'laptop',
'categoryId': '51148'}
The results I get are, for example (printing the searchResult dictionary):
'itemId': {'value': '200971548007'}, 'isMultiVariationListing': .............
'primaryCategory': {'categoryId': {'value': '69202'}, 'categoryName': {'value': 'Air Conditioning'}}
....."
You can see that the result has a categoryId of 69202, and not 51148.
What am I doing wrong here? I'm just using the finding.py code at:
https://github.com/timotheus/ebaysdk-python
Thanks
Edit
I've done some tests. I extracted the XML that the SDK builds. If I call with:
'categoryId': '177'
The response is:
the request_xml is <?xml version='1.0' encoding='utf-8'?><findItemsAdvancedRequest
xmlns="http://www.ebay.com/marketplace/search/v1/services"><categoryId>177</categoryId>
<itemFilter><name>Condition</name><value>Used</value></itemFilter><itemFilter>
<name>LocatedIn</name><value>GB</value></itemFilter><keywords>laptop</keywords>
<paginationInput><entriesPerPage>100</entriesPerPage><pageNumber>1</pageNumber>
</paginationInput></findItemsAdvancedRequest>
and I get the same with
'categoryId': ['177']
I find this a bit odd, I thought the appropriate name for the XML categoryId was 'CategoryId' with a capital C. If I do that I don't get an error, but the result is not restricted to the categoryId requested.
Doing it like above, I still get the error:
Exception: findItemsAdvanced: Domain: Marketplace, Severity: Error,
errorId: 3, Invalid category ID.
The code below will do a keyword search for 'laptops' across the UK eBay site and restrict the search to the two categories Apple Laptops(111422) and PC Laptops & Netbooks(177). In addition the results are filtered to only show the first 25 used items that are priced between £200 and £400. The results are also sorted by price from high to low.
There are a few things to keep in mind about this example.
It assumes that you have already installed ebaysdk-python.
According to the eBay docs the categoryId field is a string and more than one category can be specified. An array is therefore used to hold the category ids that we are interested in.
Our request needs to search for items in the UK eBay site. We therefore pass EBAY-GB as the siteid parameter.
Category ids are different across each eBay site. For example the category PC Laptops & Netbooks(177) does not exist in Belgium. (Which incidently is the site that is used in the ebaysdk-python finding.py example.)
This example is also available as a Gist
import ebaysdk
from ebaysdk import finding
api = finding(siteid='EBAY-GB', appid='<REPLACE WITH YOUR OWN APPID>')
api.execute('findItemsAdvanced', {
'keywords': 'laptop',
'categoryId' : ['177', '111422'],
'itemFilter': [
{'name': 'Condition', 'value': 'Used'},
{'name': 'MinPrice', 'value': '200', 'paramName': 'Currency', 'paramValue': 'GBP'},
{'name': 'MaxPrice', 'value': '400', 'paramName': 'Currency', 'paramValue': 'GBP'}
],
'paginationInput': {
'entriesPerPage': '25',
'pageNumber': '1'
},
'sortOrder': 'CurrentPriceHighest'
})
dictstr = api.response_dict()
for item in dictstr['searchResult']['item']:
print "ItemID: %s" % item['itemId'].value
print "Title: %s" % item['title'].value
print "CategoryID: %s" % item['primaryCategory']['categoryId'].value
I hope the following will explain why performing a search on the Belgium site results in items that contain the category 177 even though this is not valid for Belgium but is valid for the UK.
Basically eBay allow sellers from one site to appear in the search results of another site as long as they meet the required criteria, such as offering international shipping. It allows sellers to sell to other countries without the need to actually list on those sites.
From the example XML that elelias provided I can see that a keyword search for 'laptop' was made on the Belgium site with the results filtered so that only items located in the UK was to be returned.
<itemFilter>
<name>LocatedIn</name>
<value>GB</value>
</itemFilter>
Because the search was limited to those located in the UK you won't see any Belgium items in the results. Since the items where listed on the UK site they will contain information relevant to the UK. For example the category id 177. eBay does not convert the information to make it relevant to the site that you are searching on.
It is important to remember that what ever you are trying to do with the Finding API can also be repeated using the actual advance search on eBay. For example it is possible to re-create the issue by performing a keyword search for used items on the Belgium site.
This url is the equivalent of your code that was performing the search without specifying the category 177. As you can see from the results it returns items that where listed on the UK site but which are appearing in the Belgium site. It you click on some of the items, for example, you can even see that it displays the UK category PC Laptops & Netbooks (177) even though this does not exist on the Belgium site. This matches the results form your code where it was returning 177 but would not let you specify the same value in the request as you was searching the Belgium site.
I hope this helps.
Because categoryId is repeatable. You will need to pass an array into the call. Something like this should work.
api.execute('findItemsAdvanced', {
'keywords': 'laptop',
'categoryId': [
{'51148'}
]
}
Note: See how the itemFilter element is an array in the sample file of the SDK.
'itemFilter': [
{'name': 'Condition',
'value': 'Used'},
{'name': 'LocatedIn',
'value': 'GB'},
],