本文整理匯總了Python中projectconfig.ProjectConfiguration類的典型用法代碼示例。如果您正苦於以下問題:Python ProjectConfiguration類的具體用法?Python ProjectConfiguration怎麽用?Python ProjectConfiguration使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ProjectConfiguration類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: reverse_arc
def reverse_arc(collection, document, origin, target, type, attributes=None):
directory = collection
#undo_resp = {} # TODO
real_dir = real_directory(directory)
#mods = ModificationTracker() # TODO
projectconf = ProjectConfiguration(real_dir)
document = path_join(real_dir, document)
with TextAnnotations(document) as ann_obj:
# bail as quick as possible if read-only
if ann_obj._read_only:
raise AnnotationsIsReadOnlyError(ann_obj.get_document())
if projectconf.is_equiv_type(type):
Messager.warning('Cannot reverse Equiv arc')
elif not projectconf.is_relation_type(type):
Messager.warning('Can only reverse configured binary relations')
else:
# OK to reverse
found = None
# TODO: more sensible lookup
for ann in ann_obj.get_relations():
if (ann.arg1 == origin and ann.arg2 == target and
ann.type == type):
found = ann
break
if found is None:
Messager.error('reverse_arc: failed to identify target relation (from %s to %s, type %s) (deleted?)' % (str(origin), str(target), str(type)))
else:
# found it; just adjust this
found.arg1, found.arg2 = found.arg2, found.arg1
# TODO: modification tracker
json_response = {}
json_response['annotations'] = _json_from_ann(ann_obj)
return json_response
示例2: possible_arc_types
def possible_arc_types(collection, origin_type, target_type):
directory = collection
real_dir = real_directory(directory)
projectconf = ProjectConfiguration(real_dir)
response = {}
try:
possible = projectconf.arc_types_from_to(origin_type, target_type)
# TODO: proper error handling
if possible is None:
Messager.error('Error selecting arc types!', -1)
elif possible == []:
# nothing to select
response['html'] = generate_empty_fieldset()
response['keymap'] = {}
response['empty'] = True
else:
# XXX TODO: intentionally breaking this; KB shortcuts
# should no longer be sent here. Remove 'keymap' and
# 'html' args once clientside generation done.
arc_kb_shortcuts = {} #select_keyboard_shortcuts(possible)
response['keymap'] = {}
for k, p in arc_kb_shortcuts.items():
response['keymap'][k] = "arc_"+p
response['html'] = generate_arc_type_html(projectconf, possible, arc_kb_shortcuts)
except:
Messager.error('Error selecting arc types!', -1)
raise
return response
示例3: tag
def tag(collection, document, tagger):
pconf = ProjectConfiguration(real_directory(collection))
for tagger_token, _, _, tagger_service_url in pconf.get_annotator_config():
if tagger == tagger_token:
break
else:
raise UnknownTaggerError(tagger)
doc_path = path_join(real_directory(collection), document)
with TextAnnotations(path_join(real_directory(collection),
document)) as ann_obj:
url_soup = urlparse(tagger_service_url)
if url_soup.scheme == 'http':
Connection = HTTPConnection
elif url_soup.scheme == 'https':
# Delayed HTTPS import since it relies on SSL which is commonly
# missing if you roll your own Python, for once we should not
# fail early since tagging is currently an edge case and we
# can't allow it to bring down the whole server.
from httplib import HTTPSConnection
Connection = HTTPSConnection
else:
raise InvalidConnectionSchemeError(tagger_token, url_soup.scheme)
conn = None
try:
conn = Connection(url_soup.netloc)
req_headers = {
'Content-type': 'text/plain; charset=utf-8',
'Accept': 'application/json',
}
# Build a new service URL since the request method doesn't accept
# a parameters argument
service_url = url_soup.path + (
'?' + url_soup.query if url_soup.query else '')
try:
data = ann_obj.get_document_text().encode('utf-8')
req_headers['Content-length'] = len(data)
# Note: Trout slapping for anyone sending Unicode objects here
conn.request('POST',
# As per: http://bugs.python.org/issue11898
# Force the url to be an ascii string
str(url_soup.path),
data,
headers=req_headers)
except SocketError, e:
raise TaggerConnectionError(tagger_token, e)
resp = conn.getresponse()
# Did the request succeed?
if resp.status != 200:
raise TaggerConnectionError(tagger_token,
'%s %s' % (resp.status, resp.reason))
# Finally, we can read the response data
resp_data = resp.read()
finally:
示例4: get_base_types
def get_base_types(directory):
project_conf = ProjectConfiguration(directory)
keymap = project_conf.get_kb_shortcuts()
hotkey_by_type = dict((v, k) for k, v in keymap.iteritems())
# fill config for nodes for which annotation is configured
# calculate once only (this can get heavy)
all_connections = project_conf.all_connections()
event_hierarchy = project_conf.get_event_type_hierarchy()
event_types = _fill_type_configuration(event_hierarchy,
project_conf, hotkey_by_type, all_connections)
entity_hierarchy = project_conf.get_entity_type_hierarchy()
entity_types = _fill_type_configuration(entity_hierarchy,
project_conf, hotkey_by_type, all_connections)
relation_hierarchy = project_conf.get_relation_type_hierarchy()
relation_types = _fill_relation_configuration(relation_hierarchy,
project_conf, hotkey_by_type)
# make visual config available also for nodes for which there is
# no annotation config. Note that defaults (SPAN_DEFAULT etc.)
# are included via get_drawing_types() if defined.
unconfigured = [l for l in (project_conf.get_labels().keys() +
project_conf.get_drawing_types()) if
not project_conf.is_configured_type(l)]
unconf_types = _fill_visual_configuration(unconfigured, project_conf)
return event_types, entity_types, relation_types, unconf_types
示例5: get_attribute_types
def get_attribute_types(directory):
project_conf = ProjectConfiguration(directory)
entity_attribute_hierarchy = project_conf.get_entity_attribute_type_hierarchy()
entity_attribute_types = _fill_attribute_configuration(entity_attribute_hierarchy, project_conf)
relation_attribute_hierarchy = project_conf.get_relation_attribute_type_hierarchy()
relation_attribute_types = _fill_attribute_configuration(relation_attribute_hierarchy, project_conf)
event_attribute_hierarchy = project_conf.get_event_attribute_type_hierarchy()
event_attribute_types = _fill_attribute_configuration(event_attribute_hierarchy, project_conf)
return entity_attribute_types, relation_attribute_types, event_attribute_types
示例6: get_span_types
def get_span_types(directory):
project_conf = ProjectConfiguration(directory)
keymap = project_conf.get_kb_shortcuts()
hotkey_by_type = dict((v, k) for k, v in keymap.iteritems())
# fill config for nodes for which annotation is configured
event_hierarchy = project_conf.get_event_type_hierarchy()
event_types = _fill_type_configuration(event_hierarchy,
project_conf, hotkey_by_type)
entity_hierarchy = project_conf.get_entity_type_hierarchy()
entity_types = _fill_type_configuration(entity_hierarchy,
project_conf, hotkey_by_type)
attribute_hierarchy = project_conf.get_attribute_type_hierarchy()
attribute_types = _fill_attribute_configuration(attribute_hierarchy, project_conf)
relation_hierarchy = project_conf.get_relation_type_hierarchy()
relation_types = _fill_relation_configuration(relation_hierarchy,
project_conf, hotkey_by_type)
# make visual config available also for nodes for which there is
# no annotation config ...
unconfigured = [l for l in project_conf.get_labels() if
not project_conf.is_configured_type(l)]
# ... and include the defaults.
unconfigured += [VISUAL_SPAN_DEFAULT, VISUAL_ARC_DEFAULT]
unconf_types = _fill_visual_configuration(unconfigured, project_conf)
return event_types, entity_types, attribute_types, relation_types, unconf_types
示例7: get_attribute_types
def get_attribute_types(directory):
project_conf = ProjectConfiguration(directory)
keymap = project_conf.get_kb_shortcuts()
hotkey_by_type = dict((v, k) for k, v in keymap.iteritems())
entity_attribute_hierarchy = project_conf.get_entity_attribute_type_hierarchy()
entity_attribute_types = _fill_attribute_configuration(entity_attribute_hierarchy, project_conf, hotkey_by_type)
relation_attribute_hierarchy = project_conf.get_relation_attribute_type_hierarchy()
relation_attribute_types = _fill_attribute_configuration(relation_attribute_hierarchy, project_conf, hotkey_by_type)
event_attribute_hierarchy = project_conf.get_event_attribute_type_hierarchy()
event_attribute_types = _fill_attribute_configuration(event_attribute_hierarchy, project_conf, hotkey_by_type)
return entity_attribute_types, relation_attribute_types, event_attribute_types
示例8: tag
def tag(collection, document, tagger):
pconf = ProjectConfiguration(real_directory(collection))
for tagger_token, _, _, tagger_service_url in pconf.get_annotator_config():
if tagger == tagger_token:
break
else:
raise UnknownTaggerError(tagger)
doc_path = path_join(real_directory(collection), document)
with TextAnnotations(path_join(real_directory(collection),
document)) as ann_obj:
url_soup = urlparse(tagger_service_url)
if url_soup.scheme == 'http':
Connection = HTTPConnection
elif url_soup.scheme == 'https':
Connection = HTTPSConnection
else:
raise InvalidConnectionSchemeError(tagger_token, url_soup.scheme)
conn = None
try:
conn = Connection(url_soup.netloc)
req_headers = {
'Content-type': 'text/plain; charset=utf-8',
'Accept': 'application/json',
}
# Build a new service URL since the request method doesn't accept
# a parameters argument
service_url = url_soup.path + (
'?' + url_soup.query if url_soup.query else '')
try:
conn.request('POST', url_soup.path,
# The document text as body
ann_obj.get_document_text().encode('utf8'),
headers=req_headers)
except SocketError, e:
raise TaggerConnectionError(tagger_token, e)
resp = conn.getresponse()
# Did the request succeed?
if resp.status != 200:
raise TaggerConnectionError(tagger_token,
'%s %s' % (resp.status, resp.reason))
finally:
示例9: create_arc
def create_arc(collection, document, origin, target, type, attributes=None,
old_type=None, old_target=None, comment=None):
directory = collection
undo_resp = {}
real_dir = real_directory(directory)
mods = ModificationTracker()
projectconf = ProjectConfiguration(real_dir)
document = path_join(real_dir, document)
with TextAnnotations(document) as ann_obj:
# bail as quick as possible if read-only
# TODO: make consistent across the different editing
# functions, integrate ann_obj initialization and checks
if ann_obj._read_only:
raise AnnotationsIsReadOnlyError(ann_obj.get_document())
origin = ann_obj.get_ann_by_id(origin)
target = ann_obj.get_ann_by_id(target)
if projectconf.is_equiv_type(type):
ann =_create_equiv(ann_obj, projectconf, mods, origin, target,
type, attributes, old_type, old_target)
elif projectconf.is_relation_type(type):
ann = _create_relation(ann_obj, projectconf, mods, origin, target,
type, attributes, old_type, old_target)
else:
ann = _create_argument(ann_obj, projectconf, mods, origin, target,
type, attributes, old_type, old_target)
# process comments
if ann is not None:
_set_comments(ann_obj, ann, comment, mods,
undo_resp=undo_resp)
elif comment is not None:
Messager.warning('create_arc: non-empty comment for None annotation (unsupported type for comment?)')
mods_json = mods.json_response()
mods_json['annotations'] = _json_from_ann(ann_obj)
return mods_json
示例10: tag
def tag(collection, document, tagger):
pconf = ProjectConfiguration(real_directory(collection))
for tagger_token, _, _, tagger_service_url in pconf.get_annotator_config():
if tagger == tagger_token:
break
else:
raise UnknownTaggerError(tagger)
doc_path = path_join(real_directory(collection), document)
with TextAnnotations(path_join(real_directory(collection),
document)) as ann_obj:
try:
# Note: Can we actually fit a whole document in here?
quoted_doc_text = quote_plus(ann_obj.get_document_text())
resp = urlopen(tagger_service_url % quoted_doc_text, None,
QUERY_TIMEOUT)
except URLError:
raise TaggerConnectionError(tagger_token)
# TODO: Check for errors
json_resp = loads(resp.read())
mods = ModificationTracker()
for ann_data in json_resp.itervalues():
offsets = ann_data['offsets']
# Note: We do not support discontinuous spans at this point
assert len(offsets) == 1, 'discontinuous/null spans'
start, end = offsets[0]
_id = ann_obj.get_new_id('T')
tb = TextBoundAnnotationWithText(
start, end,
_id,
ann_data['type'],
ann_data['text']
)
mods.addition(tb)
ann_obj.add_annotation(tb)
mod_resp = mods.json_response()
mod_resp['annotations'] = _json_from_ann(ann_obj)
return mod_resp
示例11: suggest_span_types
def suggest_span_types(collection, document, start, end, text, model):
pconf = ProjectConfiguration(real_directory(collection))
for _, _, model_str, model_url in pconf.get_disambiguator_config():
if model_str == model:
break
else:
# We were unable to find a matching model
raise SimSemConnectionNotConfiguredError
try:
quoted_text = quote_plus(text)
resp = urlopen(model_url % quoted_text, None, QUERY_TIMEOUT)
except URLError:
# TODO: Could give more details
raise SimSemConnectionError
json = loads(resp.read())
preds = json['result'][text.decode('utf-8')]
selected_preds = []
conf_sum = 0
for cat, conf in preds:
selected_preds.append((cat, conf, ))
conf_sum += conf
if conf_sum >= CUT_OFF:
break
log_annotation(collection, document, 'DONE', 'suggestion',
[None, None, text, ] + [selected_preds, ])
# array so that server can control presentation order in UI
# independently from scores if needed
return { 'types': selected_preds,
'collection': collection, # echo for reference
'document': document,
'start': start,
'end': end,
'text': text,
}
示例12: _get_db_path
def _get_db_path(database, collection):
if collection is None:
# TODO: default to WORK_DIR config?
return None
else:
try:
conf_dir = real_directory(collection)
projectconf = ProjectConfiguration(conf_dir)
norm_conf = projectconf.get_normalization_config()
for entry in norm_conf:
dbname, dbpath = entry[0], entry[3]
if dbname == database:
return dbpath
# not found in config.
Messager.warning('DB ' + database + ' not defined in config for ' +
collection + ', falling back on default.')
return None
except Exception:
# whatever goes wrong, just warn and fall back on the default.
Messager.warning('Failed to get DB path from config for ' +
collection + ', falling back on default.')
return None
示例13: allowed_to_read
def allowed_to_read(real_path):
data_path = path_join('/', relpath(real_path, DATA_DIR))
# add trailing slash to directories, required to comply to robots.txt
if isdir(real_path):
data_path = '%s/' % ( data_path )
real_dir = dirname(real_path)
robotparser = ProjectConfiguration(real_dir).get_access_control()
if robotparser is None:
return True # default allow
try:
user = get_session().get('user')
except KeyError:
user = None
if user is None:
user = 'guest'
#display_message('Path: %s, dir: %s, user: %s, ' % (data_path, real_dir, user), type='error', duration=-1)
return robotparser.can_fetch(user, data_path)
示例14: get_span_types
def get_span_types(directory):
project_conf = ProjectConfiguration(directory)
keymap = project_conf.get_kb_shortcuts()
hotkey_by_type = dict((v, k) for k, v in keymap.iteritems())
event_hierarchy = project_conf.get_event_type_hierarchy()
event_types = _get_subtypes_for_type(event_hierarchy,
project_conf, hotkey_by_type, directory)
entity_hierarchy = project_conf.get_entity_type_hierarchy()
entity_types = _get_subtypes_for_type(entity_hierarchy,
project_conf, hotkey_by_type, directory)
attribute_hierarchy = project_conf.get_attribute_type_hierarchy()
attribute_types = _get_attribute_type_info(attribute_hierarchy, project_conf, directory)
relation_hierarchy = project_conf.get_relation_type_hierarchy()
relation_types = _get_subtypes_for_type(relation_hierarchy,
project_conf, hotkey_by_type, directory)
return event_types, entity_types, attribute_types, relation_types
示例15: create_arc
def create_arc(collection, document, origin, target, type, attributes=None,
old_type=None, old_target=None, comment=None):
directory = collection
undo_resp = {}
real_dir = real_directory(directory)
mods = ModificationTracker()
projectconf = ProjectConfiguration(real_dir)
document = path_join(real_dir, document)
with TextAnnotations(document) as ann_obj:
#Messager.info('Creating span collection(' + str(collection) + ') document(' + str(document) + ') origin(' + str(origin) + ') target(' + str(target) + ') exists(' + str(ann_obj.ann_exists_in_offset('Arg1:' + str(origin) + '_Arg2:' + str(target))) + ')' )
if ann_obj.ann_exists_in_offset('Arg1:' + str(origin) + '_Arg2:' + str(target)):
raise SpanOffsetOverlapError([str(ann_obj.get_ann_by_id(origin)).split()[-1], str(ann_obj.get_ann_by_id(target)).split()[-1]])
# bail as quick as possible if read-only
# TODO: make consistent across the different editing
# functions, integrate ann_obj initialization and checks
if ann_obj._read_only:
raise AnnotationsIsReadOnlyError(ann_obj.get_document())
origin = ann_obj.get_ann_by_id(origin)
target = ann_obj.get_ann_by_id(target)
# if there is a previous annotation and the arcs aren't in
# the same category (e.g. relation vs. event arg), process
# as delete + create instead of update.
if old_type is not None and (
projectconf.is_relation_type(old_type) !=
projectconf.is_relation_type(type) or
projectconf.is_equiv_type(old_type) !=
projectconf.is_equiv_type(type)):
_delete_arc_with_ann(origin.id, old_target, old_type, mods,
ann_obj, projectconf)
old_target, old_type = None, None
if projectconf.is_equiv_type(type):
ann =_create_equiv(ann_obj, projectconf, mods, origin, target,
type, attributes, old_type, old_target)
elif projectconf.is_relation_type(type):
ann = _create_relation(ann_obj, projectconf, mods, origin, target,
type, attributes, old_type, old_target)
else:
ann = _create_argument(ann_obj, projectconf, mods, origin, target,
type, attributes, old_type, old_target)
# process comments
if ann is not None:
_set_comments(ann_obj, ann, comment, mods,
undo_resp=undo_resp)
elif comment is not None:
Messager.warning('create_arc: non-empty comment for None annotation (unsupported type for comment?)')
mods_json = mods.json_response()
mods_json['annotations'] = _json_from_ann(ann_obj)
return mods_json