MCPcopy Index your code
hub / github.com/clips/pattern / search

Method search

pattern/web/__init__.py:2402–2446  ·  view source on GitHub ↗

Returns a list of results from Productwiki for the given query. Each Result.reviews is a list of (review, score)-items. - type : SEARCH, - start: maximum undefined, - count: 20, - sort : RELEVANCY. There is no daily limit.

(self, query, type=SEARCH, start=1, count=10, sort=RELEVANCY, size=None, cached=True, **kwargs)

Source from the content-addressed store, hash-verified

2400 SearchEngine.__init__(self, license or PRODUCTWIKI_LICENSE, throttle, language)
2401
2402 def search(self, query, type=SEARCH, start=1, count=10, sort=RELEVANCY, size=None, cached=True, **kwargs):
2403 """ Returns a list of results from Productwiki for the given query.
2404 Each Result.reviews is a list of (review, score)-items.
2405 - type : SEARCH,
2406 - start: maximum undefined,
2407 - count: 20,
2408 - sort : RELEVANCY.
2409 There is no daily limit.
2410 """
2411 if type != SEARCH:
2412 raise SearchEngineTypeError
2413 if not query or start < 1 or count < 1:
2414 return Results(PRODUCTWIKI, query, type)
2415 # 1) Construct request URL.
2416 url = PRODUCTWIKI+"?"
2417 url = URL(url, method=GET, query={
2418 "key": self.license or "",
2419 "q": query,
2420 "page" : start,
2421 "op": "search",
2422 "fields": "proscons", # "description,proscons" is heavy.
2423 "format": "json"
2424 })
2425 # 2) Parse JSON response.
2426 kwargs.setdefault("unicode", True)
2427 kwargs.setdefault("throttle", self.throttle)
2428 data = URL(url).download(cached=cached, **kwargs)
2429 data = json.loads(data)
2430 results = Results(PRODUCTWIKI, query, type)
2431 results.total = None
2432 for x in data.get("products", [])[:count]:
2433 r = Result(url=None)
2434 r.__dict__["title"] = u(x.get("title"))
2435 r.__dict__["text"] = u(x.get("text"))
2436 r.__dict__["reviews"] = []
2437 reviews = x.get("community_review") or {}
2438 for p in reviews.get("pros", []):
2439 r.reviews.append((p.get("text", ""), int(p.get("score")) or +1))
2440 for p in reviews.get("cons", []):
2441 r.reviews.append((p.get("text", ""), int(p.get("score")) or -1))
2442 r.__dict__["score"] = int(sum(score for review, score in r.reviews))
2443 results.append(r)
2444 # Highest score first.
2445 results.sort(key=lambda r: r.score, reverse=True)
2446 return results
2447
2448#for r in Products().search("tablet"):
2449# print r.title

Callers

nothing calls this directly

Calls 9

ResultsClass · 0.85
URLClass · 0.85
ResultClass · 0.85
sumFunction · 0.85
setdefaultMethod · 0.45
downloadMethod · 0.45
getMethod · 0.45
appendMethod · 0.45
sortMethod · 0.45

Tested by

no test coverage detected