dependencies
[nltk_ontology_framework.git] / src / mjacob / ontologybuilder / framework.py
blob5b1cf67153e1f99b89f41c2afa38e62513b3e1a8
1 # This Python file uses the following encoding: utf-8
2 '''
3 Created on Apr 29, 2011
5 @author: mjacob
6 '''
7 import os
8 class OntologyBuilderFramework(object):
9 """A framework for building domain ontologies, assuming that the framework for doing such is:
10 C{TERMS}: identify terms (from a corpus or such)
11 C{SYNONYMS}: identify synonyms amongst the terms
12 C{CONCEPTS}: identify concepts
13 C{CONCEPT_HIERARCHIES}: identify heirarchical relationships amongst concepts
14 C{RELATIONS}: identify other relationships amongst concepts
15 """
17 TERMS="terms"
18 SYNONYMS="synonyms"
19 CONCEPTS="concepts"
20 CONCEPT_HIERARCHIES="concept_hierarchies"
21 RELATIONS="relations"
23 def __init__(self, name, initial_state={}, only_do=None):
24 """
25 @type initial_state: C{dict}
26 @param initial_state: additional state available when processing with a framework instance
27 @type name: C{str}
28 @param name: the name of this framework instance (used debuggin)
29 @type cachedir: C{str}
30 @param cachedir: the base directory in which to cache the ontology generation process
31 """
32 self.__state = initial_state
33 self.__name = name
34 self.__only_do = only_do
36 def state(self):
37 """
38 get the current state.
40 @rtype: C{dict}
41 """
42 return self.__state
44 def name(self):
45 """
46 get the name of the framework instance.
48 @rtype: C{str}
49 """
50 return self.__name
52 def _do_step(self, step):
53 """
54 returns C{True} if the framework instance is designed to perform step C{step}
56 @rtype: C{bool}
57 """
58 if self.__only_do and step not in self.__only_do:
59 return False
60 return True
62 def __get_initial_state(self, additional_state):
63 """
64 construct a state collection given the defaults for object,
65 and anything additional supplied for the specific run.
66 """
67 state = self.state().copy()
68 state.update(additional_state)
69 return state
71 def process(self, **additional_state):
72 """
73 iterate through predefined to construct an ontology.
75 @return the resulting state
76 """
78 state = self.__get_initial_state(additional_state)
80 for step in (OntologyBuilderFramework.TERMS,
81 OntologyBuilderFramework.SYNONYMS,
82 OntologyBuilderFramework.CONCEPTS,
83 OntologyBuilderFramework.CONCEPT_HIERARCHIES,
84 OntologyBuilderFramework.RELATIONS):
86 if self._do_step(step):
87 result = self.__getattribute__('_get_%s' % step)(**state)
89 if not result:
90 raise Exception("no result (%s) at step %s" % (result, step))
92 state[step] = result
93 print "found %s %s" % (len(result), step)
95 return state
97 class OntologyBuilderTerms(object):
98 """interface for building terms for an ontology"""
99 def _get_terms(self, **state):
100 pass
102 class OntologyBuilderSynonyms(object):
103 """interface for building synonyms (usually of terms) for an ontology"""
104 def _get_synonyms(self, **state):
105 pass
107 class OntologyBuilderConcepts(object):
108 """interface for constructing concepts for an ontology"""
109 def _get_concepts(self, **state):
110 pass
112 class OntologyBuilderConceptHierarchies(object):
113 """interafce for constructing hierarchies of concepts for an ontology"""
114 def _get_concept_hierarchies(self, **state):
115 pass
117 class OntologyBuilderRelations(object):
118 """interface for building relations between concepts in an ontology"""
119 def _get_relations(self, **state):
120 pass