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

Method search

pattern/web/__init__.py:2100–2146  ·  view source on GitHub ↗

Returns a list of results from DBPedia for the given SPARQL query. - type : SPARQL, - start: no maximum, - count: maximum 1000, There is a limit of 10 requests/second. Maximum query execution time is 120 seconds.

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

Source from the content-addressed store, hash-verified

2098 SearchEngine.__init__(self, license, throttle, language)
2099
2100 def search(self, query, type=SPARQL, start=1, count=10, sort=RELEVANCY, size=None, cached=False, **kwargs):
2101 """ Returns a list of results from DBPedia for the given SPARQL query.
2102 - type : SPARQL,
2103 - start: no maximum,
2104 - count: maximum 1000,
2105 There is a limit of 10 requests/second.
2106 Maximum query execution time is 120 seconds.
2107 """
2108 if type not in (SPARQL,):
2109 raise SearchEngineTypeError
2110 if not query or count < 1 or start < 1:
2111 return Results(DBPEDIA, query, type)
2112 # 1) Construct request URL.
2113 url = URL(DBPEDIA, method=GET)
2114 url.query = {
2115 "format": "json",
2116 "query": "%s OFFSET %s LIMIT %s" % (query,
2117 (start-1) * min(count, 1000),
2118 (start-0) * min(count, 1000)
2119 )
2120 }
2121 # 2) Parse JSON response.
2122 try:
2123 data = URL(url).download(cached=cached, timeout=30, **kwargs)
2124 data = json.loads(data)
2125 except HTTP400BadRequest, e:
2126 raise DBPediaQueryError, e.src.read().splitlines()[0]
2127 except HTTP403Forbidden:
2128 raise SearchEngineLimitError
2129 results = Results(DBPEDIA, url.query, type)
2130 results.total = None
2131 for x in data["results"]["bindings"]:
2132 r = Result(url=None)
2133 for k in data["head"]["vars"]:
2134 t1 = x[k].get("type", "literal") # uri | literal | typed-literal
2135 t2 = x[k].get("datatype", "?") # http://www.w3.org/2001/XMLSchema#float | int | date
2136 v = x[k].get("value")
2137 v = self.format(v)
2138 if t1 == "uri":
2139 v = DBPediaResource(v)
2140 if t2.endswith("float"):
2141 v = float(v)
2142 if t2.endswith("int"):
2143 v = int(v)
2144 dict.__setitem__(r, k, v)
2145 results.append(r)
2146 return results
2147
2148#--- FLICKR ----------------------------------------------------------------------------------------
2149# Flickr is a popular image hosting and video hosting website.

Callers

nothing calls this directly

Calls 9

ResultsClass · 0.85
URLClass · 0.85
ResultClass · 0.85
DBPediaResourceClass · 0.85
downloadMethod · 0.45
readMethod · 0.45
getMethod · 0.45
__setitem__Method · 0.45
appendMethod · 0.45

Tested by

no test coverage detected