本文整理汇总了Python中arches.app.models.concept.Concept.addvalue方法的典型用法代码示例。如果您正苦于以下问题:Python Concept.addvalue方法的具体用法?Python Concept.addvalue怎么用?Python Concept.addvalue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类arches.app.models.concept.Concept
的用法示例。
在下文中一共展示了Concept.addvalue方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_concepts
# 需要导入模块: from arches.app.models.concept import Concept [as 别名]
# 或者: from arches.app.models.concept.Concept import addvalue [as 别名]
def get_concepts(self, uris):
"""
Get a list of concepts given a list of AAT uris like http://vocab.getty.edu/aat/300380087
"""
concepts = []
langs = []
for lang in self.allowed_languages:
langs.append('\"%s\"' % (lang))
for uri in uris.split(','):
query = """
SELECT ?value ?type WHERE {
{
<%s> skos:prefLabel ?value .
BIND('prefLabel' AS ?type)
}
UNION
{
<%s> skos:scopeNote [rdf:value ?value] .
BIND('scopeNote' AS ?type)
}
FILTER (lang(?value) in (%s))
}""" % (uri, uri, ','.join(langs))
results = self.perform_sparql_query(query)
if len(results["results"]["bindings"]) > 0 :
concept = Concept()
concept.nodetype = 'Concept'
for result in results["results"]["bindings"]:
concept.addvalue({
'type': result["type"]["value"],
'value': result["value"]["value"],
'language': result["value"]["xml:lang"]
})
concepts.append(concept)
else:
raise Exception(_("<strong>Error in SPARQL query:</strong><br>Test this query directly by pasting the query below into the Getty's own SPARQL endpoint at <a href='http://vocab.getty.edu/sparql' target='_blank'>http://vocab.getty.edu/sparql</a><i><pre>%s</pre></i>Query returned 0 results, please check the query for errors. You may need to add the appropriate languages into the database for this query to work<br><br>") % (query.replace('<', '<').replace('>', '>')))
return concepts
示例2: save_concepts_from_skos
# 需要导入模块: from arches.app.models.concept import Concept [as 别名]
# 或者: from arches.app.models.concept.Concept import addvalue [as 别名]
def save_concepts_from_skos(self, graph):
"""
given an RDF graph, tries to save the concpets to the system
"""
baseuuid = uuid.uuid4()
allowed_languages = models.DLanguage.objects.values_list('pk', flat=True)
value_types = models.DValueType.objects.all()
skos_value_types = value_types.filter(namespace = 'skos')
skos_value_types_list = skos_value_types.values_list('valuetype', flat=True)
dcterms_value_types = value_types.filter(namespace = 'dcterms')
relation_types = models.DRelationType.objects.all()
skos_relation_types = relation_types.filter(namespace = 'skos')
# if the graph is of the type rdflib.graph.Graph
if isinstance(graph, Graph):
# Search for ConceptSchemes first
for scheme, v, o in graph.triples((None, RDF.type , SKOS.ConceptScheme)):
scheme_id = self.generate_uuid_from_subject(baseuuid, scheme)
concept_scheme = Concept({
'id': scheme_id,
'legacyoid': str(scheme),
'nodetype': 'ConceptScheme'
})
for predicate, object in graph.predicate_objects(subject = scheme):
if str(DCTERMS) in predicate and predicate.replace(DCTERMS, '') in dcterms_value_types.values_list('valuetype', flat=True):
if hasattr(object, 'language') and object.language not in allowed_languages:
newlang = models.DLanguage()
newlang.pk = object.language
newlang.languagename = object.language
newlang.isdefault = False
newlang.save()
allowed_languages = models.DLanguage.objects.values_list('pk', flat=True)
try:
# first try and get any values associated with the concept_scheme
value_type = dcterms_value_types.get(valuetype=predicate.replace(DCTERMS, '')) # predicate.replace(SKOS, '') should yield something like 'prefLabel' or 'scopeNote', etc..
if predicate == DCTERMS.title:
concept_scheme.addvalue({'value':object, 'language': object.language, 'type': 'prefLabel', 'category': value_type.category})
print 'Casting dcterms:title to skos:prefLabel'
if predicate == DCTERMS.description:
concept_scheme.addvalue({'value':object, 'language': object.language, 'type': 'scopeNote', 'category': value_type.category})
print 'Casting dcterms:description to skos:scopeNote'
except:
pass
if str(SKOS) in predicate:
if predicate == SKOS.hasTopConcept:
self.relations.append({'source': scheme_id, 'type': 'hasTopConcept', 'target': self.generate_uuid_from_subject(baseuuid, object)})
self.nodes.append(concept_scheme)
if len(self.nodes) == 0:
raise Exception('No ConceptScheme found in file.')
# Search for Concepts
for s, v, o in graph.triples((None, SKOS.inScheme , scheme)):
concept = Concept({
'id': self.generate_uuid_from_subject(baseuuid, s),
'legacyoid': str(s),
'nodetype': 'Concept'
})
# loop through all the elements within a <skos:Concept> element
for predicate, object in graph.predicate_objects(subject = s):
if str(SKOS) in predicate:
if hasattr(object, 'language') and object.language not in allowed_languages:
newlang = models.DLanguage()
newlang.pk = object.language
newlang.languagename = object.language
newlang.isdefault = False
newlang.save()
allowed_languages = models.DLanguage.objects.values_list('pk', flat=True)
relation_or_value_type = predicate.replace(SKOS, '') # this is essentially the skos element type within a <skos:Concept> element (eg: prefLabel, broader, etc...)
if relation_or_value_type in skos_value_types_list:
value_type = skos_value_types.get(valuetype=relation_or_value_type)
concept.addvalue({'value':object, 'language': object.language, 'type': value_type.valuetype, 'category': value_type.category})
elif predicate == SKOS.broader:
self.relations.append({'source': self.generate_uuid_from_subject(baseuuid, object), 'type': 'narrower', 'target': self.generate_uuid_from_subject(baseuuid, s)})
elif predicate == SKOS.narrower:
self.relations.append({'source': self.generate_uuid_from_subject(baseuuid, s), 'type': relation_or_value_type, 'target': self.generate_uuid_from_subject(baseuuid, object)})
elif predicate == SKOS.related:
self.relations.append({'source': self.generate_uuid_from_subject(baseuuid, s), 'type': relation_or_value_type, 'target': self.generate_uuid_from_subject(baseuuid, object)})
self.nodes.append(concept)
# insert and index the concpets
with transaction.atomic():
for node in self.nodes:
node.save()
# insert the concept relations
#.........这里部分代码省略.........
示例3: load_authority_file
# 需要导入模块: from arches.app.models.concept import Concept [as 别名]
# 或者: from arches.app.models.concept.Concept import addvalue [as 别名]
def load_authority_file(cursor, path_to_authority_files, filename, auth_file_to_entity_concept_mapping):
print filename.upper()
start = time()
value_types = models.ValueTypes.objects.all()
filepath = os.path.join(path_to_authority_files, filename)
unicodecsv.field_size_limit(sys.maxint)
errors = []
lookups = Lookups()
#create nodes for each authority document file and relate them to the authority document node in the concept schema
auth_doc_file_name = str(filename)
display_file_name = string.capwords(auth_doc_file_name.replace('_',' ').replace('AUTHORITY DOCUMENT.csv', '').strip())
if auth_doc_file_name.upper() != 'ARCHES RESOURCE CROSS-REFERENCE RELATIONSHIP TYPES.E32.CSV':
top_concept = Concept()
top_concept.id = str(uuid.uuid4())
top_concept.nodetype = 'Concept'
top_concept.legacyoid = auth_doc_file_name
top_concept.addvalue({'value':display_file_name, 'language': settings.LANGUAGE_CODE, 'type': 'prefLabel', 'category': 'label'})
lookups.add_relationship(source='00000000-0000-0000-0000-000000000001', type='hasTopConcept', target=top_concept.id)
else:
top_concept = Concept().get(id = '00000000-0000-0000-0000-000000000005')
top_concept.legacyoid = 'ARCHES RESOURCE CROSS-REFERENCE RELATIONSHIP TYPES.E32.csv'
lookups.add_lookup(concept=top_concept, rownum=0)
try:
with open(filepath, 'rU') as f:
rows = unicodecsv.DictReader(f, fieldnames=['CONCEPTID','PREFLABEL','ALTLABELS','PARENTCONCEPTID','CONCEPTTYPE','PROVIDER'],
encoding='utf-8-sig', delimiter=',', restkey='ADDITIONAL', restval='MISSING')
rows.next() # skip header row
for row in rows:
try:
if 'MISSING' in row:
raise Exception('The row wasn\'t parsed properly. Missing %s' % (row['MISSING']))
else:
legacyoid = row[u'CONCEPTID']
concept = Concept()
concept.id = legacyoid if is_uuid(legacyoid) == True else str(uuid.uuid4())
concept.nodetype = 'Concept'# if row[u'CONCEPTTYPE'].upper() == 'INDEX' else 'Collection'
concept.legacyoid = row[u'CONCEPTID']
concept.addvalue({'value':row[u'PREFLABEL'], 'language': settings.LANGUAGE_CODE, 'type': 'prefLabel', 'category': 'label'})
if row['CONCEPTTYPE'].lower() == 'collector':
concept.addvalue({'value':row[u'PREFLABEL'], 'language': settings.LANGUAGE_CODE, 'type': 'collector', 'category': 'label'})
if row[u'ALTLABELS'] != '':
altlabel_list = row[u'ALTLABELS'].split(';')
for altlabel in altlabel_list:
concept.addvalue({'value':altlabel, 'language': settings.LANGUAGE_CODE, 'type': 'altLabel', 'category': 'label'})
parent_concept_id = lookups.get_lookup(legacyoid=row[u'PARENTCONCEPTID']).id
lookups.add_relationship(source=parent_concept_id, type='narrower', target=concept.id, rownum=rows.line_num)
# don't add a member relationship between a top concept and it's children
if parent_concept_id != top_concept.id:
lookups.add_relationship(source=parent_concept_id, type='member', target=concept.id, rownum=rows.line_num)
# add the member relationship from the E55 type (typically) to their top members
if auth_doc_file_name in auth_file_to_entity_concept_mapping and row[u'PARENTCONCEPTID'] == auth_doc_file_name:
for entitytype_info in auth_file_to_entity_concept_mapping[auth_doc_file_name]:
lookups.add_relationship(source=entitytype_info['ENTITYTYPE_CONCEPTID'], type='member', target=concept.id, rownum=rows.line_num)
if row[u'PARENTCONCEPTID'] == '' or (row[u'CONCEPTTYPE'].upper() != 'INDEX' and row[u'CONCEPTTYPE'].upper() != 'COLLECTOR'):
raise Exception('The row has invalid values.')
lookups.add_lookup(concept=concept, rownum=rows.line_num)
except Exception as e:
errors.append('ERROR in row %s: %s' % (rows.line_num, str(e)))
except UnicodeDecodeError as e:
errors.append('ERROR: Make sure the file is saved with UTF-8 encoding\n%s\n%s' % (str(e), traceback.format_exc()))
except Exception as e:
errors.append('ERROR: %s\n%s' % (str(e), traceback.format_exc()))
if len(errors) > 0:
errors.insert(0, 'ERRORS IN FILE: %s\n' % (filename))
errors.append('\n\n\n\n')
try:
# try and open the values file if it exists
if exists(filepath.replace('.csv', '.values.csv')):
with open(filepath.replace('.csv', '.values.csv'), 'rU') as f:
rows = unicodecsv.DictReader(f, fieldnames=['CONCEPTID','VALUE','VALUETYPE','PROVIDER'],
encoding='utf-8-sig', delimiter=',', restkey='ADDITIONAL', restval='MISSING')
rows.next() # skip header row
for row in rows:
try:
if 'ADDITIONAL' in row:
raise Exception('The row wasn\'t parsed properly. Additional fields found %s. Add quotes to values that have commas in them.' % (row['ADDITIONAL']))
else:
row_valuetype = row[u'VALUETYPE'].strip()
if row_valuetype not in value_types.values_list('valuetype', flat=True):
valuetype = models.ValueTypes()
valuetype.valuetype = row_valuetype
valuetype.category = 'undefined'
valuetype.namespace = 'arches'
valuetype.save()
value_types = models.ValueTypes.objects.all()
concept = lookups.get_lookup(legacyoid=row[u'CONCEPTID'])
#.........这里部分代码省略.........
示例4: create_reference_data
# 需要导入模块: from arches.app.models.concept import Concept [as 别名]
# 或者: from arches.app.models.concept.Concept import addvalue [as 别名]
def create_reference_data(new_concepts, create_collections):
errors = []
candidates = Concept().get(id='00000000-0000-0000-0000-000000000006')
for arches_nodeid, concepts in new_concepts.iteritems():
collectionid = str(uuid.uuid4())
topconceptid = str(uuid.uuid4())
node = Node.objects.get(nodeid=arches_nodeid)
# if node.datatype is concept or concept-list create concepts and collections
if node.datatype in ['concept', 'concept-list']:
# create collection if create_collections = create, otherwise append to collection already assigned to node
if create_collections == True:
collection_legacyoid = node.name + '_' + str(node.graph_id) + '_import'
# check to see that there is not already a collection for this node
if node.config['rdmCollection'] != None:
errors.append({'type': 'WARNING', 'message': 'A collection already exists for the {0} node. Use the add option to add concepts to this collection.'.format(node.name)})
if len(errors) > 0:
self.errors += errors
collection = None
else:
# if there is no collection assigned to this node, create one and assign it to the node
try:
# check to see that a collection with this legacyid does not already exist
collection = Concept().get(legacyoid=collection_legacyoid)
errors.append({'type': 'WARNING', 'message': 'A collection with the legacyid {0} already exists.'.format(node.name + '_' + str(node.graph_id) + '_import')})
if len(errors) > 0:
self.errors += errors
except:
collection = Concept({
'id': collectionid,
'legacyoid': collection_legacyoid,
'nodetype': 'Collection'
})
collection.addvalue({'id': str(uuid.uuid4()), 'value': node.name + '_import', 'language': settings.LANGUAGE_CODE, 'type': 'prefLabel'})
node.config['rdmCollection'] = collectionid
node.save()
collection.save()
else:
# if create collection = add check that there is a collection associated with node, if no collection associated with node create a collection and associated with the node
try:
collection = Concept().get(id=node.config['rdmCollection'])
except:
collection = Concept({
'id': collectionid,
'legacyoid': node.name + '_' + str(node.graph_id) + '_import',
'nodetype': 'Collection'
})
collection.addvalue({'id': str(uuid.uuid4()), 'value': node.name + '_import', 'language': settings.LANGUAGE_CODE, 'type': 'prefLabel'})
node.config['rdmCollection'] = collectionid
node.save()
collection.save()
if collection != None:
topconcept_legacyoid = node.name + '_' + str(node.graph_id)
# Check if top concept already exists, if not create it and add to candidates scheme
try:
topconcept = Concept().get(legacyoid=topconcept_legacyoid)
except:
topconcept = Concept({
'id': topconceptid,
'legacyoid': topconcept_legacyoid,
'nodetype': 'Concept'
})
topconcept.addvalue({'id': str(uuid.uuid4()), 'value': node.name + '_import', 'language': settings.LANGUAGE_CODE, 'type': 'prefLabel'})
topconcept.save()
candidates.add_relation(topconcept, 'narrower')
# create child concepts and relate to top concept and collection accordingly
for conceptid, value in concepts.iteritems():
concept_legacyoid = value + '_' + node.name + '_' + str(node.graph_id)
# check if concept already exists, if not create and add to topconcept and collection
try:
conceptid = [concept for concept in topconcept.get_child_concepts(topconcept.id) if concept[1] == value][0][0]
concept = Concept().get(id=conceptid)
except:
concept = Concept({
'id': conceptid,
'legacyoid': concept_legacyoid,
'nodetype': 'Concept'
})
concept.addvalue({'id': str(uuid.uuid4()), 'value': value, 'language': settings.LANGUAGE_CODE, 'type': 'prefLabel'})
concept.save()
collection.add_relation(concept, 'member')
topconcept.add_relation(concept, 'narrower')
#if node.datatype is domain or domain-list create options array in node.config
elif node.datatype in ['domain-value', 'domain-value-list']:
for domainid, value in new_concepts[arches_nodeid].iteritems():
# check if value already exists in domain
if value not in [t['text'] for t in node.config['options']]:
domainvalue = {
"text": value,
"selected": False,
"id": domainid
}
node.config['options'].append(domainvalue)
node.save()
示例5: save_concepts_from_skos
# 需要导入模块: from arches.app.models.concept import Concept [as 别名]
# 或者: from arches.app.models.concept.Concept import addvalue [as 别名]
def save_concepts_from_skos(self, graph, overwrite_options='overwrite', staging_options='keep'):
"""
given an RDF graph, tries to save the concpets to the system
Keyword arguments:
overwrite_options -- 'overwrite', 'ignore'
staging_options -- 'stage', 'keep'
"""
baseuuid = uuid.uuid4()
allowed_languages = models.DLanguage.objects.values_list('pk', flat=True)
default_lang = settings.LANGUAGE_CODE
value_types = models.DValueType.objects.all()
skos_value_types = value_types.filter(Q(namespace = 'skos') | Q(namespace = 'arches'))
skos_value_types_list = list(skos_value_types.values_list('valuetype', flat=True))
skos_value_types = {valuetype.valuetype: valuetype for valuetype in skos_value_types}
dcterms_value_types = value_types.filter(namespace = 'dcterms')
# relation_types = models.DRelationType.objects.all()
# skos_relation_types = relation_types.filter(namespace = 'skos')
# if the graph is of the type rdflib.graph.Graph
if isinstance(graph, Graph):
# Search for ConceptSchemes first
for scheme, v, o in graph.triples((None, RDF.type , SKOS.ConceptScheme)):
scheme_id = self.generate_uuid_from_subject(baseuuid, scheme)
concept_scheme = Concept({
'id': scheme_id,
'legacyoid': str(scheme),
'nodetype': 'ConceptScheme'
})
for predicate, object in graph.predicate_objects(subject = scheme):
if str(DCTERMS) in predicate and predicate.replace(DCTERMS, '') in dcterms_value_types.values_list('valuetype', flat=True):
if not self.language_exists(object, allowed_languages):
allowed_languages = models.DLanguage.objects.values_list('pk', flat=True)
try:
# first try and get any values associated with the concept_scheme
value_type = dcterms_value_types.get(valuetype=predicate.replace(DCTERMS, '')) # predicate.replace(SKOS, '') should yield something like 'prefLabel' or 'scopeNote', etc..
val = self.unwrapJsonLiteral(object)
if predicate == DCTERMS.title:
concept_scheme.addvalue({'id': val['value_id'], 'value':val['value'], 'language': object.language or default_lang, 'type': 'prefLabel', 'category': value_type.category})
print 'Casting dcterms:title to skos:prefLabel'
elif predicate == DCTERMS.description:
concept_scheme.addvalue({'id': val['value_id'], 'value':val['value'], 'language': object.language or default_lang, 'type': 'scopeNote', 'category': value_type.category})
print 'Casting dcterms:description to skos:scopeNote'
except:
pass
if str(SKOS) in predicate:
#print predicate
if predicate == SKOS.hasTopConcept:
top_concept_id = self.generate_uuid_from_subject(baseuuid, object)
self.relations.append({'source': scheme_id, 'type': 'hasTopConcept', 'target': top_concept_id})
self.nodes.append(concept_scheme)
if len(self.nodes) == 0:
raise Exception('No ConceptScheme found in file.')
# Search for Concepts
for s, v, o in graph.triples((None, SKOS.inScheme , scheme)):
concept = Concept({
'id': self.generate_uuid_from_subject(baseuuid, s),
'legacyoid': str(s),
'nodetype': 'Concept'
})
# loop through all the elements within a <skos:Concept> element
for predicate, object in graph.predicate_objects(subject = s):
if str(SKOS) in predicate or str(ARCHES) in predicate:
if not self.language_exists(object, allowed_languages):
allowed_languages = models.DLanguage.objects.values_list('pk', flat=True)
relation_or_value_type = predicate.replace(SKOS, '').replace(ARCHES, '') # this is essentially the skos element type within a <skos:Concept> element (eg: prefLabel, broader, etc...)
if relation_or_value_type in skos_value_types_list:
value_type = skos_value_types[relation_or_value_type]
val = self.unwrapJsonLiteral(object)
concept.addvalue({'id': val['value_id'], 'value':val['value'], 'language': object.language or default_lang, 'type': value_type.valuetype, 'category': value_type.category})
elif predicate == SKOS.broader:
self.relations.append({'source': self.generate_uuid_from_subject(baseuuid, object), 'type': 'narrower', 'target': self.generate_uuid_from_subject(baseuuid, s)})
elif predicate == SKOS.narrower:
self.relations.append({'source': self.generate_uuid_from_subject(baseuuid, s), 'type': relation_or_value_type, 'target': self.generate_uuid_from_subject(baseuuid, object)})
elif predicate == SKOS.related:
self.relations.append({'source': self.generate_uuid_from_subject(baseuuid, s), 'type': relation_or_value_type, 'target': self.generate_uuid_from_subject(baseuuid, object)})
self.nodes.append(concept)
# Search for SKOS.Collections
#.........这里部分代码省略.........