1 # This Python file uses the following encoding: utf-8
3 Created on Apr 29, 2011
8 from util
.cached
import Cacheable
9 class OntologyBuilderFramework(Cacheable
):
10 """A framework for building domain ontologies, assuming that the framework for doing such is:
11 C{TERMS}: identify terms (from a corpus or such)
12 C{SYNONYMS}: identify synonyms amongst the terms
13 C{CONCEPTS}: identify concepts
14 C{CONCEPT_HIERARCHIES}: identify heirarchical relationships amongst concepts
15 C{RELATIONS}: identify other relationships amongst concepts
21 CONCEPT_HIERARCHIES
="concept_hierarchies"
24 def __init__(self
, name
, cachedir
, initial_state
={}, only_do
=None, ignore_cache
=None):
26 @type initial_state: C{dict}
27 @param initial_state: additional state available when processing with a framework instance
29 @param name: the name of this framework instance (used debuggin)
30 @type cachedir: C{str}
31 @param cachedir: the base directory in which to cache the ontology generation process
33 Cacheable
.__init
__(self
, os
.path
.join(cachedir
, name
), ignore_cache
)
34 self
.__state
= initial_state
36 self
.__only
_do
= only_do
40 get the current state.
48 get the name of the framework instance.
54 def _do_step(self
, step
):
56 returns C{True} if the framework instance is designed to perform step C{step}
60 if self
.__only
_do
and step
not in self
.__only
_do
:
64 def __get_initial_state(self
, additional_state
):
66 construct a state collection given the defaults for object,
67 and anything additional supplied for the specific run.
69 state
= self
.state().copy()
70 state
.update(additional_state
)
73 def process(self
, **additional_state
):
75 iterate through predefined to construct an ontology.
77 @param only_do: if specified, only perform steps in this collection
78 @param ignore_cache: if specified, ignore any cached results from
79 steps specified in this collection. note that any new results
80 will still be saved to cache, possibly overwriting existing results.
82 @return the resulting state
85 state
= self
.__get
_initial
_state
(additional_state
)
87 for step
in (OntologyBuilderFramework
.TERMS
,
88 OntologyBuilderFramework
.SYNONYMS
,
89 OntologyBuilderFramework
.CONCEPTS
,
90 OntologyBuilderFramework
.CONCEPT_HIERARCHIES
,
91 OntologyBuilderFramework
.RELATIONS
):
93 if self
._do
_step
(step
):
94 result
= self
.__getattribute
__('_get_%s' % step
)(**state
)
97 raise Exception("no result (%s) at step %s" % (result
, step
))
100 print "found %s %s" % (len(result
), step
)
104 class OntologyBuilderTerms(object):
105 """interface for building terms for an ontology"""
106 def _get_terms(self
, **state
):
109 class OntologyBuilderSynonyms(object):
110 """interface for building synonyms (usually of terms) for an ontology"""
111 def _get_synonyms(self
, **state
):
114 class OntologyBuilderConcepts(object):
115 """interface for constructing concepts for an ontology"""
116 def _get_concepts(self
, **state
):
119 class OntologyBuilderConceptHierarchies(object):
120 """interafce for constructing hierarchies of concepts for an ontology"""
121 def _get_concept_hierarchies(self
, **state
):
124 class OntologyBuilderRelations(object):
125 """interface for building relations between concepts in an ontology"""
126 def _get_relations(self
, **state
):