def run(annotation_dir): # load username from config configfile = open(os.path.join(annotation_dir, 'moocdb.conf')) config = json.load(configfile) configfile.close() username = config['username'] annotation_fnames = [os.path.join(annotation_dir , f) for f in os.listdir(annotation_dir ) if f.endswith('.ann')] annotations = [] text_ids = set() for fname in annotation_fnames: text_id = int(os.path.basename(fname).replace('.ann', '')) text_ids.add(text_id) f = open(fname) for line in f: annotations.append(BratAnnotation(line, text_id, username)) f.close() db = dbutils.get_database_connection() cursor = db.cursor() # first, remove all annotations by this (user, text_id) pair for text_id in text_ids: cursor.execute("SELECT id FROM text_annotations WHERE annotator = '%s' AND text_id = '%d'" % (username, text_id)) ids = cursor.fetchall() for a_id in ids: cursor.execute("DELETE FROM text_annotations WHERE id = '%s'" % a_id) db.commit() for annotation in annotations: annotation.insert(cursor) db.commit() shutil.rmtree(annotation_dir)
def main(args): def load_data(fname): try: f = open(fname) data = json.load(f) f.close() return data except: sys.stderr.write("Couldn't find \"" + fname + "\", did you run ./generate_fakedata?\n") sys.exit(1) sources = load_data('sources.json') users = load_data('users.json') posts = load_data('posts.json') forums = load_data('forums.json') db = None try: db = dbutils.get_database_connection(user=args.username, password=args.password) cursor = db.cursor() source_id = add_sources(cursor, sources) forum_ids = add_forums(cursor, forums, source_id) user_ids = add_users(cursor, users, source_id) post_ids = add_posts(cursor, posts, source_id, forum_ids, user_ids) db.commit() #except: #sys.stderr.write("It looks like you can't connect to the database! Run the script with the -h command to see how to fix this\n") finally: if db is not None: db.close()
def run(username, sql_query): home = os.path.expanduser('~') brat = os.path.join(home, 'local/software/brat-v1.3_Crunchy_Frog/data') basedir = brat + '/annotate-' + username + '-' + datetime.strftime(datetime.now(), "%Y%m%d_%H%M%S") db = dbutils.get_database_connection() cursor = db.cursor() cursor.execute(sql_query) texts = [Text(result) for result in cursor.fetchall()] for text in texts: text.write(username, basedir, cursor) # create a config file that encodes (for now) just the username config = {'username':username} configfile = open(os.path.join(basedir, 'moocdb.conf'), 'w') json.dump(config, configfile, indent = 2) configfile.close() return basedir
def run(username, sql_query): home = os.path.expanduser('~') brat = os.path.join(home, 'local/software/brat-v1.3_Crunchy_Frog/data') basedir = brat + '/annotate-' + username + '-' + datetime.strftime( datetime.now(), "%Y%m%d_%H%M%S") db = dbutils.get_database_connection() cursor = db.cursor() cursor.execute(sql_query) texts = [Text(result) for result in cursor.fetchall()] for text in texts: text.write(username, basedir, cursor) # create a config file that encodes (for now) just the username config = {'username': username} configfile = open(os.path.join(basedir, 'moocdb.conf'), 'w') json.dump(config, configfile, indent=2) configfile.close() return basedir