本文整理汇总了Python中philologic.DB.DB.query方法的典型用法代码示例。如果您正苦于以下问题:Python DB.query方法的具体用法?Python DB.query怎么用?Python DB.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类philologic.DB.DB
的用法示例。
在下文中一共展示了DB.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_metadata_token_count
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def get_metadata_token_count(environ,start_response):
status = '200 OK'
headers = [('Content-type', 'application/json; charset=UTF-8'),("Access-Control-Allow-Origin","*")]
start_response(status,headers)
config = f.WebConfig()
db = DB(config.db_path + '/data/')
request = WSGIHandler(db, environ)
results = ''
frequencies = json.loads(environ['wsgi.input'].read())
word_counts = []
c = db.dbh.cursor()
count = 0
for label, m in frequencies.iteritems():
args = []
query_metadata = {}
for metadata in m['metadata']:
query_metadata[metadata] = m['metadata'][metadata].encode('utf-8')
hits = db.query(**query_metadata)
total_count = 0
for hit in hits:
total_count += int(hit['word_count'])
try:
frequencies[label]['count'] = round(float(m['count']) / total_count * 1000000, 3)
except:
count += 1
frequencies[label]['count'] = 0
yield json.dumps(frequencies)
示例2: lookup_word_service
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def lookup_word_service(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'application/json; charset=UTF-8'), ("Access-Control-Allow-Origin", "*")]
start_response(status, headers)
config = WebConfig(os.path.abspath(os.path.dirname(__file__)).replace('scripts', ''))
db = DB(config.db_path + '/data/')
request = WSGIHandler(environ, config)
cursor = db.dbh.cursor()
if request.report == "concordance":
hits = db.query(request["q"], request["method"], request["arg"], **request.metadata)
context_size = config['concordance_length'] * 3
hit = hits[int(request.position)]
bytes = hit.bytes
hit_span = hit.bytes[-1] - hit.bytes[0]
length = context_size + hit_span + context_size
bytes, start_byte = adjust_bytes(bytes, length)
end_byte = start_byte + length
filename = hit.filename
token = request.selected
elif request.report == "navigation":
token = request.selected
philo_id = request.philo_id.split(" ")
text_obj = db[philo_id]
start_byte, end_byte = int(text_obj.start_byte), int(text_obj.end_byte)
filename = text_obj.filename
# print >> sys.stderr, "WORD LOOKUP FROM NAVIGATION", request.philo_id,request.selected, start_byte, end_byte, filename
else:
pass
# print >> sys.stderr, "TOKEN", token, "BYTES: ", start_byte, end_byte, "FILENAME: ", filename, "POSITION", request.position
token_n = 0
yield lookup_word(db, cursor, token, token_n, start_byte, end_byte, filename)
示例3: term_group
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def term_group(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'application/json; charset=UTF-8'),
("Access-Control-Allow-Origin", "*")]
start_response(status, headers)
config = WebConfig(os.path.abspath(os.path.dirname(__file__)).replace('scripts', ''))
db = DB(config.db_path + '/data/')
request = WSGIHandler(environ, config)
if not request["q"]:
dump = json.dumps({"original_query": "", "term_groups": []})
else:
hits = db.query(request["q"], request["method"], request["arg"], sort_order=request["sort_order"], **request.metadata)
parsed = parse_query(request.q)
group = group_terms(parsed)
all_groups = split_terms(group)
term_groups = []
for g in all_groups:
term_group = ''
not_started = False
for kind, term in g:
if kind == 'NOT':
if not_started is False:
not_started = True
term_group += ' NOT '
elif kind == 'OR':
term_group += '|'
elif kind == "TERM":
term_group += ' %s ' % term
elif kind == "QUOTE":
term_group += ' %s ' % term
term_group = term_group.strip()
term_groups.append(term_group)
dump = json.dumps({"term_groups": term_groups, "original_query": request.original_q})
yield dump.encode('utf8')
示例4: get_frequency
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def get_frequency(environ,start_response):
status = '200 OK'
headers = [('Content-type', 'application/json; charset=UTF-8'),("Access-Control-Allow-Origin","*")]
start_response(status,headers)
config = f.WebConfig()
db = DB(config.db_path + '/data/')
request = WSGIHandler(db, environ)
if request.q == '' and request.no_q:
if request.no_metadata:
hits = db.get_all(db.locals['default_object_level'])
else:
hits = db.query(**request.metadata)
else:
hits = db.query(request["q"],request["method"],request["arg"],**request.metadata)
results = r.generate_frequency(hits, request, db, config)
yield json.dumps(results)
示例5: lookup_word_service
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def lookup_word_service(environ, start_response):
status = "200 OK"
headers = [("Content-type", "application/json; charset=UTF-8"), ("Access-Control-Allow-Origin", "*")]
start_response(status, headers)
config = f.WebConfig()
db = DB(config.db_path + "/data/")
request = WSGIHandler(db, environ)
cursor = db.dbh.cursor()
if request.report == "concordance":
hits = db.query(request["q"], request["method"], request["arg"], **request.metadata)
context_size = config["concordance_length"] * 3
hit = hits[int(request.position)]
bytes = hit.bytes
hit_span = hit.bytes[-1] - hit.bytes[0]
length = context_size + hit_span + context_size
bytes, byte_start = adjust_bytes(bytes, length)
byte_end = byte_start + length
filename = hit.filename
token = request.selected
elif request.report == "navigation":
token = request.selected
philo_id = request.philo_id.split(" ")
text_obj = db[philo_id]
byte_start, byte_end = int(text_obj.byte_start), int(text_obj.byte_end)
filename = text_obj.filename
# print >> sys.stderr, "WORD LOOKUP FROM NAVIGATION", request.philo_id,request.selected, byte_start, byte_end, filename
else:
pass
# print >> sys.stderr, "TOKEN", token, "BYTES: ", byte_start, byte_end, "FILENAME: ", filename, "POSITION", request.position
token_n = 0
yield lookup_word(db, cursor, token, token_n, byte_start, byte_end, filename)
示例6: term_group
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def term_group(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'application/json; charset=UTF-8'),
("Access-Control-Allow-Origin", "*")]
start_response(status, headers)
config = f.WebConfig()
db = DB(config.db_path + '/data/')
request = WSGIHandler(db, environ)
hits = db.query(request["q"], request["method"], request["arg"],
**request.metadata)
parsed = parse_query(request.q)
group = group_terms(parsed)
all_groups = split_terms(group)
term_groups = []
for g in all_groups:
term_group = ''
not_started = False
for kind, term in g:
if kind == 'NOT':
if not_started == False:
not_started = True
term_group += ' NOT '
elif kind == 'OR':
term_group += '|'
elif kind == "TERM":
term_group += ' %s ' % term
elif kind == "QUOTE":
term_group += ' %s ' % term
term_group = term_group.strip()
term_groups.append(term_group)
yield json.dumps(term_groups)
示例7: bibliography_results
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def bibliography_results(request, config):
"""Fetch bibliography results"""
db = DB(config.db_path + '/data/')
if request.no_metadata:
hits = db.get_all(db.locals['default_object_level'], request["sort_order"])
else:
hits = db.query(sort_order=request["sort_order"], **request.metadata)
if request.simple_bibliography == "all": # request from simple landing page report which gets all biblio in load order
hits.finish()
start = 1
end = len(hits)
page_num = end
else:
start, end, page_num = page_interval(request.results_per_page, hits, request.start, request.end)
bibliography_object = {
"description": {
"start": start,
"end": end,
"n": page_num,
"results_per_page": request.results_per_page
},
"query": dict([i for i in request]),
"default_object": db.locals['default_object_level']
}
results = []
result_type = "doc"
for hit in hits[start - 1:end]:
citation_hrefs = citation_links(db, config, hit)
metadata_fields = {}
for metadata in db.locals['metadata_fields']:
metadata_fields[metadata] = hit[metadata]
result_type = hit.object_type
if request.simple_bibliography == "all":
citation = citations(hit, citation_hrefs, config, report="simple_landing")
else:
citation = citations(hit, citation_hrefs, config, report="bibliography", result_type=result_type)
if config.dictionary_bibliography is False or result_type == "doc":
results.append({
'citation': citation,
'citation_links': citation_hrefs,
'philo_id': hit.philo_id,
"metadata_fields": metadata_fields,
"object_type": result_type
})
else:
context = get_text_obj(hit, config, request, db.locals["token_regex"], images=False)
results.append({
'citation': citation,
'citation_links': citation_hrefs,
'philo_id': hit.philo_id,
"metadata_fields": metadata_fields,
"context": context,
"object_type": result_type
})
bibliography_object["results"] = results
bibliography_object['results_length'] = len(hits)
bibliography_object['query_done'] = hits.done
bibliography_object['result_type'] = result_type
return bibliography_object, hits
示例8: get_frequency
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def get_frequency(environ,start_response):
status = '200 OK'
headers = [('Content-type', 'application/json; charset=UTF-8'),("Access-Control-Allow-Origin","*")]
start_response(status,headers)
config = f.WebConfig()
db = DB(config.db_path + '/data/')
request = WSGIHandler(db, environ)
hits = db.query(request["q"],request["method"],request["arg"],**request.metadata)
field, word_frequency_object = generate_word_frequency(hits,request,db,config)
yield json.dumps(word_frequency_object, indent=2)
示例9: word_property_filter
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def word_property_filter(environ,start_response):
config = f.WebConfig()
db = DB(config.db_path + '/data/')
request = WSGIHandler(db, environ)
headers = [('Content-type', 'application/json; charset=UTF-8'),("Access-Control-Allow-Origin","*")]
start_response('200 OK',headers)
hits = db.query(request["q"],request["method"],request["arg"],**request.metadata)
filter_results = filter_words_by_property(hits, config.db_path, request, db, config)
yield json.dumps(filter_results)
示例10: collocation
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def collocation(environ,start_response):
config = f.WebConfig()
db = DB(config.db_path + '/data/')
request = WSGIHandler(db, environ)
headers = [('Content-type', 'application/json; charset=UTF-8'),("Access-Control-Allow-Origin","*")]
start_response('200 OK',headers)
hits = db.query(request["q"],"cooc",request["arg"],**request.metadata)
hits.finish()
collocation_object = fetch_collocation(hits, request, db, config)
yield json.dumps(collocation_object)
示例11: term_list
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def term_list(environ, start_response):
status = "200 OK"
headers = [("Content-type", "application/json; charset=UTF-8"), ("Access-Control-Allow-Origin", "*")]
start_response(status, headers)
config = f.WebConfig()
db = DB(config.db_path + "/data/")
q = WSGIHandler(db, environ)
hits = db.query(q["q"], q["method"], q["arg"], **q.metadata)
expanded_terms = get_expanded_query(hits)
yield json.dumps(expanded_terms[0])
示例12: get_more_context
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def get_more_context(environ, start_response):
status = "200 OK"
headers = [("Content-type", "application/json; charset=UTF-8"), ("Access-Control-Allow-Origin", "*")]
start_response(status, headers)
config = f.WebConfig()
db = DB(config.db_path + "/data/")
request = WSGIHandler(db, environ)
hit_num = int(request.hit_num)
hits = db.query(request["q"], request["method"], request["arg"], **request.metadata)
context_size = config["concordance_length"] * 3
hit_context = r.fetch_concordance(db, hits[hit_num], config.db_path, context_size)
yield json.dumps(hit_context)
示例13: generate_word_frequency
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def generate_word_frequency(request, config):
"""reads through a hitlist. looks up request["field"] in each hit, and builds up a list of
unique values and their frequencies."""
db = DB(config.db_path + '/data/')
hits = db.query(request["q"], request["method"], request["arg"], **request.metadata)
field = request["field"]
counts = {}
frequency_object = {}
more_results = True
start_time = timeit.default_timer()
last_hit_done = request.start
try:
for n in hits[request.start:]:
key = get_word_attrib(n, field, db)
if not key:
# NULL is a magic value for queries, don't change it
# recklessly.
key = "NULL"
if key not in counts:
counts[key] = 0
counts[key] += 1
elapsed = timeit.default_timer() - start_time
last_hit_done += 1
if elapsed > 5:
break
table = {}
for k, v in counts.iteritems():
url = make_absolute_query_link(config,
request,
start="0",
end="0",
report="word_property_filter",
word_property=field,
word_property_value=k)
table[k] = {'count': v, 'url': url}
frequency_object['results'] = table
frequency_object["hits_done"] = last_hit_done
if last_hit_done == len(hits):
frequency_object['more_results'] = False
else:
frequency_object['more_results'] = True
except IndexError:
frequency_object['results'] = {}
frequency_object['more_results'] = False
frequency_object['results_length'] = len(hits)
frequency_object['query'] = dict([i for i in request])
return frequency_object
示例14: term_list
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def term_list(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'application/json; charset=UTF-8'),
("Access-Control-Allow-Origin", "*")]
start_response(status, headers)
config = f.WebConfig()
db = DB(config.db_path + '/data/')
request = WSGIHandler(db, environ)
hits = db.query(request["q"], request["method"], request["arg"],
**request.metadata)
hits.finish()
expanded_terms = get_expanded_query(hits)
yield json.dumps(expanded_terms[0])
示例15: get_frequency
# 需要导入模块: from philologic.DB import DB [as 别名]
# 或者: from philologic.DB.DB import query [as 别名]
def get_frequency(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'application/json; charset=UTF-8'),
("Access-Control-Allow-Origin", "*")]
start_response(status, headers)
config = f.WebConfig()
db = DB(config.db_path + '/data/')
request = WSGIHandler(db, environ)
setattr(request, 'frequency_field', json.dumps(
eval('"%s"' % request.frequency_field)))
if request.q == '' and request.no_q:
if request.no_metadata:
hits = db.get_all(db.locals['default_object_level'])
else:
hits = db.query(**request.metadata)
else:
hits = db.query(request["q"], request["method"], request["arg"],
**request.metadata)
hits.finish()
results = r.generate_frequency(hits, request, db, config)
results['results'] = sorted(results['results'].iteritems(),
key=lambda x: x[1]['count'],
reverse=True)
yield json.dumps(results)