Changeset 208
- Timestamp:
- 09/16/08 22:29:06 (4 months ago)
- Files:
-
- trunk/microfacts/data/napoleon.js (modified) (6 diffs)
- trunk/microfacts/getdata/dbpedia.py (modified) (6 diffs)
- trunk/microfacts/lib/cli.py (modified) (5 diffs)
- trunk/microfacts/lib/json.py (modified) (2 diffs)
- trunk/microfacts/tests/getdata/test_dbpedia.py (modified) (3 diffs)
- trunk/microfacts/tests/test_json.py (modified) (2 diffs)
- trunk/microfacts/tests/test_modes.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/microfacts/data/napoleon.js
r207 r208 1 1 { 2 "id" : "b6f9b907-3033-4acc-accd-e7b8f7f29deb", 2 3 "title" : "Battles in the Napoleonic Wars", 3 4 "factlets" : [ 4 5 { 6 "id" : "a880106e-ce43-4463-8884-bafe67270aa8", 5 7 "title" : "Battle of Austerlitz", 6 8 "image" : "http://upload.wikimedia.org/wikipedia/commons/5/56/Austerlitz-baron-Pascal.jpg", … … 12 14 }, 13 15 { 16 "id" : "04d3d5a3-a2e5-41ab-a71b-306c424b2ccb", 14 17 "title" : "Battle of Borodino", 15 18 "description" : "The Battle of Borodino (September 7, 1812, or August 26 in the Julian calendar then used in Russia), was the largest and bloodiest single-day battle of the Napoleonic Wars, involving more than a quarter of a million soldiers. It was fought by the French ''Grande Armée'' under Napoleon I and the Imperial Russian army of General Mikhail Kutusov near the village of Borodino, west", … … 19 22 }, 20 23 { 24 "id" : "40432a1e-b08a-413e-b36a-4f281ce06f7d", 21 25 "title" : "Battle of Waterloo", 22 26 "source" : "http://en.wikipedia.org/wiki/Battle_of_Waterloo", … … 26 30 }, 27 31 { 32 "id" : "67c0272a-d469-4bc8-8c3e-5fe14c8f3b60", 28 33 "title" : "Battle of Trafalgar", 29 34 "source" : "http://en.wikipedia.org/wiki/Battle_of_Trafalgar", … … 33 38 }, 34 39 { 40 "id" : "a4728f72-405a-4804-b2c6-9b37ba2e44ed", 35 41 "title" : "Battle of Jena-Auerstadt", 36 42 "source" : "http://en.wikipedia.org/wiki/Battle_of_Jena-Auerstedt", … … 39 45 }, 40 46 { 47 "id" : "3a1d1201-ea85-4a5b-a59f-b957b42d22fc", 41 48 "title" : "Battle of Friedland", 42 49 "start" : "1807-06-14", 43 50 "description" : "The Battle of Friedland, fought on June 14, 1807 about twenty-seven miles (43 km) southeast of the modern Russian city of Kaliningrad, just north of Poland, was a major engagement in the Napoleonic Wars effectively ending the War of the Fourth Coalition. The conflict involved forces of the First French Empire against the army of the Russian Empire", 44 51 "location" : {"type": "Point", "coordinates": [21.0167 , 54.45] } 45 46 52 } 47 53 ] trunk/microfacts/getdata/dbpedia.py
r207 r208 1 ''' 1 '''Extract information dbpedia. 2 2 3 3 Research Summary … … 21 21 http://dbpedia.org/resource/New_Guinea, http://www.w3.org/2003/01/geo/wgs84_pos#lat, -5.333333492279053 22 22 ''' 23 import logging 23 24 import datetime 25 26 logger = logging.getLogger(__name__) 24 27 25 28 import dateutil.parser … … 40 43 PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 41 44 ''' 42 # TODO: remove these methods43 def category_search(category):44 c = CategorySearch()45 c.execute(category)46 return c.results47 48 def value_info(v):49 return u'%s' % v.value50 45 51 46 class CategorySearch: … … 71 66 values = results.getValues(u'subject') 72 67 self.results = values 68 69 70 class SPOQuery(object): 71 def __init__(self, verbose=False): 72 self.results = [] 73 self.verbose = verbose 74 75 def execute(self, subject=u'?subject', predicate=u'?predicate', object=u'?object'): 76 if subject.startswith('?') and object.startswith('?'): 77 raise Exception('Cannot have both subject and object not defined') 78 def correct(x): 79 if x.startswith(u'http://'): 80 return u'<%s>' % x 81 else: 82 return x 83 subject, predicate, object = [ correct(x) for x in [subject, predicate, 84 object]] 85 query = PREFIXES + ''' 86 SELECT * WHERE { 87 %s %s %s 88 } 89 ''' % (subject, predicate, object) 90 self.query = query 91 logger.debug(self.query) 92 if self.verbose: 93 print self.query 94 sparql = SPARQLWrapper2('http://dbpedia.org/sparql') 95 sparql.setQuery(query) 96 sparql.setReturnFormat(JSON) 97 results = sparql.query() 98 # TODO: support predicate 99 if subject.startswith('?'): 100 values = results.getValues(subject[1:]) 101 else: 102 values = results.getValues(object[1:]) 103 self.results = [ v.value for v in values ] 104 73 105 74 106 import pprint … … 252 284 253 285 254 def describe(uri, verbose=False):255 d = Describe(verbose)256 d.execute(uri)257 return d.to_str()258 259 260 286 # def describe2(): 261 287 # '''This is what the snorql interface produced when doing describe. … … 285 311 286 312 287 def demo():288 cat = 'Category:Battles_and_operations_of_World_War_II'289 uri1 = 'http://dbpedia.org/resource/Admiralty_Islands_campaign'290 uri2 = 'http://dbpedia.org/resource/Battle_of_Normandy'291 # print describe(uri1)292 print describe(uri2)293 print category_search(cat)294 295 296 if __name__ == '__main__':297 import sys298 cmd = sys.argv[1]299 if cmd == 'describe':300 uri = sys.argv[2]301 print describe(uri, verbose=True)302 elif cmd == 'search':303 uri = sys.argv[2]304 print category_search(uri)305 elif cmd == 'demo':306 demo()trunk/microfacts/lib/cli.py
r207 r208 106 106 data.close() 107 107 108 import pprint 108 109 class Dbpedia(MicrofactsCommand): 109 110 '''CLI interface to DBPedia. … … 111 112 describe <uri> 112 113 search <category-name> 114 # queries 115 qsubject <predicate> <object> 116 qobject <subject> <predicate> 113 117 114 118 Examples: … … 117 121 describe http://en.wikipedia.org/wiki/Napoleon_I_of_France 118 122 search Category:Battles_and_operations_of_World_War_II 123 qsubject http://dbpedia.org/property/commander http://dbpedia.org/resource/Napoleon_I_of_France 124 # using prefixes 125 subject dbpedia2:commander Napoleon_I_of_France 119 126 ''' 120 127 summary = __doc__.split('\n')[0] … … 124 131 min_args = 2 125 132 default_verbosity = 0 133 134 def _l(self, tlist): 135 # return pprint.pformat(c.results) 136 out = u'' 137 for item in tlist: 138 out += str(item) + '\n' 139 return out 126 140 127 141 def command(self): … … 142 156 elif cmd == 'search': 143 157 category = self.args[1] 144 result = dbp.category_search(category) 158 c = CategorySearch() 159 c.execute(category) 160 result = self._l(c.results) 161 elif cmd == 'qsubject': 162 predicate = self.args[1] 163 object = self.args[2] 164 q = dbp.SPOQuery(verbose=self.verbose) 165 q.execute(predicate=predicate, object=object) 166 result = self._l(q.results) 167 elif cmd == 'qobject': 168 subject = self.args[1] 169 predicate = self.args[2] 170 q = dbp.SPOQuery() 171 q.execute(predicate=predicate, subject=subject) 172 result = self._l(q.results) 145 173 else: 146 174 msg = 'Command %s not recognized' % cmd trunk/microfacts/lib/json.py
r201 r208 69 69 entity = self.domain_object.query.get(id) 70 70 if entity is None: 71 msg = 'No Entity of type %s exists with id %s' % (self.domain_object.__name__, id)72 # TODO: be more specific -- have a NotFound exception or return73 # None?74 raise LoadFromJsonException(msg)71 # 2008-09-16 no reason not to allow id on new objects 72 # msg = 'No Entity of type %s exists with id %s' % (self.domain_object.__name__, id) 73 # raise LoadFromJsonException(msg) 74 entity = self.domain_object(id=id) 75 75 else: 76 76 entity = self.domain_object() … … 136 136 val = v 137 137 if k == 'start' or k == 'end': # a datetime 138 if v is not None:138 if v: # could be '' or None 139 139 # default to 1st of January 1AD 140 140 default = datetime.datetime(1,1,1) trunk/microfacts/tests/getdata/test_dbpedia.py
r207 r208 4 4 from microfacts.getdata.dbpedia import * 5 5 except: 6 raise 6 7 dotest = False 7 8 … … 70 71 assert long == 16.8, long 71 72 assert self.fct.start == self.start 73 assert self.fct.end == None, self.fct.end 72 74 73 75 … … 88 90 assert self.kwds['source'] == self.uri 89 91 92 93 class TestCategorySearch(DbpediaBase): 94 def test_1(self): 95 cs = CategorySearch() 96 cat = 'Category:Battles_and_operations_of_World_War_II' 97 cs.execute(cat) 98 print cs.results 99 assert len(cs.results) > 0 100 101 class TestSPOQuery(DbpediaBase): 102 def test_subject_query(self): 103 subject = u'http://dbpedia.org/resource/Battle_of_Wagram' 104 pred = u'http://dbpedia.org/property/commander' 105 object = u'http://dbpedia.org/resource/Napoleon_I_of_France' 106 q = SPOQuery() 107 q.execute(predicate=pred, object=object) 108 print q.query 109 print q.results 110 assert len(q.results) == 48, len(q.results) 111 assert subject in q.results 112 113 def test_object_query(self): 114 subject = u'http://dbpedia.org/resource/Battle_of_Waterloo' 115 pred = u'http://dbpedia.org/property/commander' 116 object = u'http://dbpedia.org/resource/Napoleon_I_of_France' 117 q = SPOQuery() 118 q.execute(predicate=pred, subject=subject) 119 # this works too 120 # q.execute(predicate=pred, subject=subject, object='?obj') 121 print q.query 122 print q.results 123 # should be 4 not 5 but some weird dbpedia stuff (flagicon!) 124 assert len(q.results) == 5, len(q.results) 125 assert object in q.results 126 trunk/microfacts/tests/test_json.py
r201 r208 85 85 assert out.title == self.title 86 86 87 def test_load_factlet_with_bad_id(self): 87 def test_load_factlet_with_empty_string_date(self): 88 mydict = { 'end' : '' } 89 out = self.converter.to_domain_object(mydict) 90 assert out.end == None 91 92 # TODO: sort out what happens when id is supplied but not object exists 93 def _test_load_factlet_with_bad_id(self): 88 94 # non existent id 89 95 mydict = { 'id' : '1344387134' } … … 137 143 assert len(thread.factlets) == 6 138 144 139 def test_load_thread_with_id(self): 145 # TODO: sort out what happens when id is supplied but not object exists 146 def _test_load_thread_with_id(self): 140 147 # a non-existent id 141 148 mydict = { 'id' : '24343143' } trunk/microfacts/tests/test_modes.py
r201 r208 247 247 248 248 249 class TestEntityPutNotFound(PresentationModeCase): 249 # TODO: sort out what happends when id does not exist ... 250 class _TestEntityPutNotFound(PresentationModeCase): 250 251 251 252 mode_class = EntityPut
