<commit_before># coding: utf-8

import json
import train
import random
import os
from generatebatchsettings import *
import numpy as np
import copy
import hashlib
import glob
from python import utils
import sys

def main(start=None,verbose=True,logfile=None):
    """
    Contains the main loop for this script.
    Pseudo-MHMCMC to find optimal AUC scoring
    combinations of HDF5s.
    start - location of json file settings to begin at
    """
    if logfile != None:
        sys.stdout = open(logfile,"w")
    # pseudo-code for the MCMC iteration
    # want it to start with the probably good features
    if start == None:
        with open("startchain_gavin.json") as f:
            start = json.load(f)
        startauc = 0.86575473285
    mcmcdir = "hdf5mcmc"

    # hardcode AUC results to the hdf5mcmc directory
    start['AUC_SCORE_PATH'] = mcmcdir

    # have to load a list of possible features to replace with
    featlist = get_featlist()
    # and possible preceding modifiers
    modlist  = get_modlist()

    # define sampled json
    prevsample = copy.deepcopy(start)

    # initialise auc
    prevauc = startauc

    converged = False
    # will decide what constitutes converged later
    while not converged:

        sample = copy.deepcopy(prevsample)
        # Sample a new hdf5 and replace existing at random
        #   Or, just push it in, or just drop a hdf5 at random
        utils.print_verbose("===== Sampling new proposal "
                "settings ======",flag=verbose)
        u = np.random.rand()
        if u < 0.25:
            # drop an element at random
            features = sample['FEATURES'][:]
            random.shuffle(features)
            dropped = features.pop()
            sample['FEATURES'] = features
            utils.print_verbose("Dropped feature {0}".format(dropped),flag=verbose)
        elif u > 0.25 and u < 0.5:
            # push a new feature, but don't remove an old one
            newfeature = random.sample(featlist,1)[0]
            newmod = random.sample(modlist,1)[0]
            added = '{0}_{1}_'.format(newmod, newfeature)
            sample['FEATURES'].append(added)
            utils.print_verbose("Added feature {0}".format(added),flag=verbose)
        elif u > 0.5:
            # push a new feature and remove an old one
            features = sample['FEATURES'][:]
            random.shuffle(features)
            dropped = features.pop()
            newfeature = random.sample(featlist,1)[0]
            newmod = random.sample(modlist,1)[0]
            added = '{0}_{1}_'.format(newmod, newfeature)
            features.append(added)
            sample['FEATURES'] = features
            utils.print_verbose("Switched feature {0} for "
                    "{1}".format(dropped,added),flag=verbose)
        utils.print_verbose("============================"
                "===============",flag=verbose)
        # ensure that ordering of the features is the same between jsons
        sample['FEATURES'].sort()

        # Then save this new json with a descriptive name
        # unless it's already been generated
        md5name = hashlib.md5("".join(sample['FEATURES']).encode('UTF-8')).hexdigest()
        # get a list of the files in the mcmcdir
        existingjsons = glob.glob(mcmcdir+"/*.json")
        # check if the md5 exists
        if md5name + ".json" in existingjsons:
            # then load the results of that run
            with open(os.path.join(mcmcdir,"AUC_scores.csv"),"r") as fh:
                c = csv.reader(fh, delimiter="\t")
                utils.print_verbose("Already ran {0},"
                        "reading from results.".format(md5name),flag=verbose)
                for line in c:
                    # look for that md5sum
                    if md5name in line[0]:
                        auc_score = line[-1]
        else:
            # save a json with this name and run train.py on it
            samplefname = os.path.join(mcmcdir,md5name+".json")
            utils.print_verbose("Creating new settings"
                    " file for {0}".format(samplefname))
            with open(samplefname, "w") as fh:
                json.dump(sample, fh)
            # call train.py
            try:
                auc_score_dict = train.main(samplefname,verbose=verbose)
                auc_score = auc_score_dict['all']
            except IndexError:
                print("Warning: accidentally added invalid feature.")
                os.remove(samplefname)
                # set auc to zero so these settings are not accepted
                auc_score = 0

        utils.print_verbose("==== Acceptance calculation ====",flag=verbose)
        # compute acceptance probability from AUC:
        #     r = min(1,AUC/(previous AUC))
        acceptance = np.max([np.min([1,(auc_score-0.5)/(prevauc-0.5)]), 0])

        u = np.random.rand()
        # accept new point with probability r
        if u < acceptance:
            prevsample = sample
            # save current auc
            prevauc = auc_score
            utils.print_verbose("accepting new settings with probability "
                "{0}".format(acceptance),flag=verbose)
        else:
            utils.print_verbose("rejecting new settings with probability "
                "{0}".format(1.0-acceptance),flag=verbose)
        utils.print_verbose("================================",flag=verbose)
        #otherwise it will not overwrite prevsample, so continue from where it was

def get_parser():
    '''
    Generate parser for cmdline options.
    '''
    parser = optparse.OptionParser()

    parser.add_option("-v", "--verbose",
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="Print verbose output")

    parser.add_option("-s", "--start",
                      action="store",
                      dest="start",
                      default="SETTINGS.json",
                      help="Settings file to start at in JSON format"
                      "(default=SETTINGS.json)")

    parser.add_option("-l", "--log",
                      action="store",
                      dest="log",
                      default="stoch_opt.log",
                      help="Log file for verbose output of script"
                      "(default=stoch_opt.log)")
    return parser

if __name__ == "__main__":
    parser = utils.get_parser()
    (opts, args) = parser.parse_args()
    main(start=opts.start,verbose=opts.verbose,logfile=opts.log)
<commit_msg>local changes<commit_after># coding: utf-8

import json
import train
import random
import os
from generatebatchsettings import *
import numpy as np
import copy
import hashlib
import glob
from python import utils
import sys
import optparse

def main(start=None,start_auc=None,verbose=True,logfile=None):
    """
    Contains the main loop for this script.
    Pseudo-MHMCMC to find optimal AUC scoring
    combinations of HDF5s.
    start - location of json file settings to begin at
    """
    if logfile != None:
        sys.stdout = open(logfile,"w")
    # pseudo-code for the MCMC iteration
    # want it to start with the probably good features
    with open(start) as f:
        start = json.load(f)
    if start_auc == None:
        startauc = 0.86575473285
    mcmcdir = "hdf5mcmc"

    # hardcode AUC results to the hdf5mcmc directory
    start['AUC_SCORE_PATH'] = mcmcdir

    # have to load a list of possible features to replace with
    featlist = get_featlist()
    # and possible preceding modifiers
    modlist  = get_modlist()

    # define sampled json
    prevsample = copy.deepcopy(start)

    # initialise auc
    prevauc = startauc

    counter = 0
    converged = False
    # will decide what constitutes converged later
    while not converged:

        sample = copy.deepcopy(prevsample)
        # Sample a new hdf5 and replace existing at random
        #   Or, just push it in, or just drop a hdf5 at random
        utils.print_verbose("===== Sampling new proposal "
                "settings ======",flag=verbose)
        u = np.random.rand()
        if u < 0.25:
            # drop an element at random
            features = sample['FEATURES'][:]
            random.shuffle(features)
            dropped = features.pop()
            sample['FEATURES'] = features
            utils.print_verbose("Dropped feature {0}".format(dropped),flag=verbose)
        elif u > 0.25 and u < 0.5:
            # push a new feature, but don't remove an old one
            newfeature = random.sample(featlist,1)[0]
            newmod = random.sample(modlist,1)[0]
            added = '{0}_{1}_'.format(newmod, newfeature)
            sample['FEATURES'].append(added)
            utils.print_verbose("Added feature {0}".format(added),flag=verbose)
        elif u > 0.5:
            # push a new feature and remove an old one
            features = sample['FEATURES'][:]
            random.shuffle(features)
            dropped = features.pop()
            newfeature = random.sample(featlist,1)[0]
            newmod = random.sample(modlist,1)[0]
            added = '{0}_{1}_'.format(newmod, newfeature)
            features.append(added)
            sample['FEATURES'] = features
            utils.print_verbose("Switched feature {0} for "
                    "{1}".format(dropped,added),flag=verbose)
        utils.print_verbose("============================"
                "===============",flag=verbose)
        # ensure that ordering of the features is the same between jsons
        sample['FEATURES'].sort()

        # Then save this new json with a descriptive name
        # unless it's already been generated
        md5name = hashlib.md5("".join(sample['FEATURES']).encode('UTF-8')).hexdigest()
        # get a list of the files in the mcmcdir
        existingjsons = glob.glob(mcmcdir+"/*.json")
        # check if the md5 exists
        if md5name + ".json" in existingjsons:
            # then load the results of that run
            with open(os.path.join(mcmcdir,"AUC_scores.csv"),"r") as fh:
                c = csv.reader(fh, delimiter="\t")
                utils.print_verbose("Already ran {0},"
                        "reading from results.".format(md5name),flag=verbose)
                for line in c:
                    # look for that md5sum
                    if md5name in line[0]:
                        auc_score = line[-1]
        else:
            # save a json with this name and run train.py on it
            samplefname = os.path.join(mcmcdir,md5name+".json")
            utils.print_verbose("Creating new settings"
                    " file for {0}".format(samplefname))
            with open(samplefname, "w") as fh:
                json.dump(sample, fh)
            # call train.py
            try:
                auc_score_dict = train.main(samplefname,verbose=verbose)
                auc_score = auc_score_dict['all']
            except IndexError:
                print("Warning: accidentally added invalid feature.")
                os.remove(samplefname)
                # set auc to zero so these settings are not accepted
                auc_score = 0

        utils.print_verbose("==== Acceptance calculation ====",flag=verbose)
        # compute acceptance probability from AUC:
        #     r = min(1,AUC/(previous AUC))
        acceptance = np.max([np.min([1,(auc_score-0.5)/(prevauc-0.5)]), 0])

        u = np.random.rand()
        # accept new point with probability r
        if u < acceptance:
            prevsample = sample
            # save current auc
            prevauc = auc_score
            utils.print_verbose("accepting new settings with probability "
                "{0}".format(acceptance),flag=verbose)
        else:
            utils.print_verbose("rejecting new settings with probability "
                "{0}".format(1.0-acceptance),flag=verbose)
        utils.print_verbose("================================",flag=verbose)
        #otherwise it will not overwrite prevsample, so continue from where it was

        # as it may be bad manners to run infinite loops
        counter += 1
        if counter > 100:
            converged = True

def get_parser():
    '''
    Generate parser for cmdline options.
    '''
    parser = optparse.OptionParser()

    parser.add_option("-v", "--verbose",
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="Print verbose output")

    parser.add_option("-s", "--start",
                      action="store",
                      dest="start",
                      default="startchain_gavin.json",
                      help="Settings file to start at in JSON format"
                      "(default=startchain_gavin.json)")

    parser.add_option("-a", "--start_auc",
                      action="store",
                      dest="start_auc",
                      default=None,
                      help="AUC score of settings file starting the chain at"
                      "(default=None)")

    parser.add_option("-l", "--log",
                      action="store",
                      dest="log",
                      default=None,
                      help="Log file for verbose output of script"
                      "(default=None)")
    return parser

if __name__ == "__main__":
    parser = get_parser()
    (opts, args) = parser.parse_args()
    main(start=opts.start,start_auc=opts.start_auc,verbose=opts.verbose,logfile=opts.log)
<|endoftext|>


<commit_before>import os
import sys
import logging
import argparse
import ConfigParser
import struct
import collections
import pprint
import numpy as np
from modulos.Collection import Collection
from modulos.Indexer import Indexer
from custom_exceptions import NoIndexFilesException, IniException

POSTINGS_FILENAME = "postings.bin"
MAXFREQS_FILENAME = "max_freq_in_docs.bin"
POSTINGS_POINTERS_FILENAME = "postings_pointers.bin"
METADATA_FILENAME = "metadata.bin"

class IRManager(object):

    def __init__(self):
        self.indexPath = loadIni()["index_dir"]
        self.corpusPath = False
        self.docs = False
        self.terms = False
        self.postings = False
        self.idf = False
        self.docsNorm = False
        self.indexConfig = loadIndexConfig(loadIni())

    def index(self, corpusPath):
    	try:
    		collection = Collection(corpusPath)
    	except OSError, e:
    		logging.error(e)
    		raise

    	indexer = Indexer(collection)
    	indexer.index(self.indexConfig)

        self.corpusPath = corpusPath
        indexer.postings.sortByKey()
        self.postings = indexer.postings.content
        self.terms = len(indexer.vocabulary.content)
        self.docs = len(indexer.documents.content)
        return {
            "vocabulary": indexer.vocabulary.content,
            "documents": indexer.documents.content,
            "postings": self.postings,
            "max_freq": indexer.maxFreqInDocs,
            "terms": self.terms,
            "docs": self.docs
        }

    def evaluate(self, query):
        docScores = {}
        for docId in range(0, self.docs):
            docScores[docId] = 0.0
        qMaxFreq = max(query.values()) + 0.0
        qNorm = 0.0
        for qtId in query:
            qTfIdf = (query[qtId] / qMaxFreq) * self.idf[qtId]
            query[qtId] = qTfIdf
            qNorm += qTfIdf ** 2.0
        qNorm = qNorm ** 0.5
        for qtId in query:
            qtPosting = self.postings[qtId]
            for dId in qtPosting:
                docScores[dId] += query[qtId] * qtPosting[dId]
        for docId in range(0, self.docs):
            divider = qNorm * self.docsNorm[docId]
            if divider == 0.0:
                docScores[docId] = 0.0
            else:
                docScores[docId] /= divider
        return docScores

    def loadStoredIndex(self):
        logging.info("Loading stored index")

        if not os.path.isdir(self.indexPath):
            logging.info("Index files directory does not exist. Creating...")
            os.makedirs(self.indexPath)
        metadataDir = os.path.join(self.indexPath, METADATA_FILENAME)
        maxFreqsDir = os.path.join(self.indexPath, MAXFREQS_FILENAME)
        pointersDir = os.path.join(self.indexPath, POSTINGS_POINTERS_FILENAME)
        postingsDir = os.path.join(self.indexPath, POSTINGS_FILENAME)
        if not (os.path.exists(metadataDir) and os.path.exists(maxFreqsDir) and os.path.exists(pointersDir) and os.path.exists(postingsDir)):
            raise NoIndexFilesException("index files have not been generated.")

        metadata = loadMetadata(metadataDir)
        self.docs = metadata["docs"]
        self.terms = metadata["terms"]
        maxFreqs = loadMaxfreqs(maxFreqsDir, self.docs)
        df = loadDf(pointersDir, self.terms)
        self.postings = loadPostings(postingsDir, df)

        logging.info("Generating retrieval data structures")
        self.generateRetrievalData(maxFreqs)

    def generateRetrievalData(self, maxFreqs):
        self.docsNorm = {}
        for dId in range(0, self.docs):
            self.docsNorm[dId] = 0.0

        self.idf = {}
        for tId in self.postings:
            df = len(self.postings[tId]) + 0.0
            currentIdf = np.log10(self.docs / df)
            self.idf[tId] = currentIdf
            for dId in self.postings[tId]:
                currentTf = (self.postings[tId][dId] + 0.0) / maxFreqs[dId]
                currentTfIdf = currentTf * currentIdf
                self.postings[tId][dId] = currentTfIdf
                self.docsNorm[dId] += currentTfIdf ** 2.0

        for dId in self.docsNorm:
            self.docsNorm[dId] = self.docsNorm[dId] ** 0.5

def loadIni():
	INI_PATH = os.path.dirname(os.path.realpath(__file__)) + "/config.ini"
	Config = ConfigParser.ConfigParser()
	Config.read(INI_PATH)
	iniData = {}
	sections = Config.sections()
	for option in Config.options(sections[0]):
		opValue = Config.get(sections[0], option)
		iniData[option] = opValue if opValue != -1 else False;
	return iniData

def loadIndexConfig(iniData):
    indexConfig = {}
    if "stopwords" in iniData and iniData["stopwords"]:
        if not (os.path.exists(iniData["stopwords"])):
            raise IniException("in 'stopwords' property: not a valid file path")
        indexConfig["stopwords"] = iniData['stopwords']
	if "stem" in iniData and iniData["stem"]:
		indexConfig["stem"] = iniData['stem']
	if "term_min_size" in iniData and iniData["term_min_size"]:
		indexConfig["term_min_size"] = int(iniData["term_min_size"])
	if "term_max_size" in iniData and iniData["term_max_size"]:
		indexConfig["term_max_size"] = int(iniData["term_max_size"])
	return indexConfig

def loadMetadata(metadataDir):
    metadata = {}
    with open(metadataDir, "rb") as f:
        binMeta = f.read(8)
        meta = struct.unpack('<2I', binMeta)
        metadata["docs"] = meta[0]
        metadata["terms"] = meta[1]
    return metadata

def loadMaxfreqs(metadataDir, docs):
    maxfreqs = {}
    with open(metadataDir, "rb") as f:
        mfRead = f.read(docs * 4)
        mfRead = struct.unpack('<%iI' % docs, mfRead)
        for docId in range(0, docs):
            maxfreqs[docId] = int(mfRead[docId])
    return maxfreqs

def loadDf(dfDir, terms):
    df = {}
    with open(dfDir, "rb") as f:
        read = f.read(terms * 4)
        read = struct.unpack('<%iI' % terms, read)
        for tId in range(0, terms):
            df[tId] = int(read[tId])
    return df

def loadPostings(postingsDir, df):
    postings = collections.OrderedDict()
    with open(postingsDir, "rb") as f:
        for tId in range(0, len(df)):
            postings[tId] = collections.OrderedDict()
            docIdsRead = struct.unpack('<%iI' % df[tId], f.read(df[tId] * 4))
            freqsRead = struct.unpack('<%iI' % df[tId], f.read(df[tId] * 4))
            for i in range(0, df[tId]):
                postings[tId][int(docIdsRead[i])] = int(freqsRead[i])
    return postings
<commit_msg>python process: more logs<commit_after>import os
import sys
import logging
import argparse
import ConfigParser
import struct
import collections
import pprint
import numpy as np
from modulos.Collection import Collection
from modulos.Indexer import Indexer
from custom_exceptions import NoIndexFilesException, IniException

POSTINGS_FILENAME = "postings.bin"
MAXFREQS_FILENAME = "max_freq_in_docs.bin"
POSTINGS_POINTERS_FILENAME = "postings_pointers.bin"
METADATA_FILENAME = "metadata.bin"

class IRManager(object):

    def __init__(self):
        self.indexPath = loadIni()["index_dir"]
        self.corpusPath = False
        self.docs = False
        self.terms = False
        self.postings = False
        self.idf = False
        self.docsNorm = False
        self.indexConfig = loadIndexConfig(loadIni())

    def index(self, corpusPath):
    	try:
    		collection = Collection(corpusPath)
    	except OSError, e:
    		logging.error(e)
    		raise

    	indexer = Indexer(collection)
    	indexer.index(self.indexConfig)

        self.corpusPath = corpusPath
        indexer.postings.sortByKey()
        self.postings = indexer.postings.content
        self.terms = len(indexer.vocabulary.content)
        self.docs = len(indexer.documents.content)
        return {
            "vocabulary": indexer.vocabulary.content,
            "documents": indexer.documents.content,
            "postings": self.postings,
            "max_freq": indexer.maxFreqInDocs,
            "terms": self.terms,
            "docs": self.docs
        }

    def evaluate(self, query):
        docScores = {}
        for docId in range(0, self.docs):
            docScores[docId] = 0.0
        qMaxFreq = max(query.values()) + 0.0
        qNorm = 0.0
        for qtId in query:
            qTfIdf = (query[qtId] / qMaxFreq) * self.idf[qtId]
            query[qtId] = qTfIdf
            qNorm += qTfIdf ** 2.0
        qNorm = qNorm ** 0.5
        for qtId in query:
            qtPosting = self.postings[qtId]
            for dId in qtPosting:
                docScores[dId] += query[qtId] * qtPosting[dId]
        for docId in range(0, self.docs):
            divider = qNorm * self.docsNorm[docId]
            if divider == 0.0:
                docScores[docId] = 0.0
            else:
                docScores[docId] /= divider
        return docScores

    def loadStoredIndex(self):
        logging.info("Loading stored index")

        if not os.path.isdir(self.indexPath):
            logging.info("Index files directory does not exist. Creating...")
            os.makedirs(self.indexPath)

        metadataDir = os.path.join(self.indexPath, METADATA_FILENAME)
        maxFreqsDir = os.path.join(self.indexPath, MAXFREQS_FILENAME)
        pointersDir = os.path.join(self.indexPath, POSTINGS_POINTERS_FILENAME)
        postingsDir = os.path.join(self.indexPath, POSTINGS_FILENAME)
        if not (os.path.exists(metadataDir) and os.path.exists(maxFreqsDir) and os.path.exists(pointersDir) and os.path.exists(postingsDir)):
            raise NoIndexFilesException("index files have not been generated.")

        logging.info("Loading metadata")
        metadata = loadMetadata(metadataDir)
        self.docs = metadata["docs"]
        self.terms = metadata["terms"]
        logging.info("%d docs and %d terms" % (self.docs, self.terms))

        logging.info("Loading maxfreqs")
        maxFreqs = loadMaxfreqs(maxFreqsDir, self.docs)

        logging.info("Loading pointers to postings")
        df = loadDf(pointersDir, self.terms)

        logging.info("Loading postings")
        self.postings = loadPostings(postingsDir, df)

        logging.info("Generating retrieval data structures")
        self.generateRetrievalData(maxFreqs)

    def generateRetrievalData(self, maxFreqs):
        self.docsNorm = {}
        for dId in range(0, self.docs):
            self.docsNorm[dId] = 0.0

        self.idf = {}
        for tId in self.postings:
            df = len(self.postings[tId]) + 0.0
            currentIdf = np.log10(self.docs / df)
            self.idf[tId] = currentIdf
            for dId in self.postings[tId]:
                currentTf = (self.postings[tId][dId] + 0.0) / maxFreqs[dId]
                currentTfIdf = currentTf * currentIdf
                self.postings[tId][dId] = currentTfIdf
                self.docsNorm[dId] += currentTfIdf ** 2.0

        for dId in self.docsNorm:
            self.docsNorm[dId] = self.docsNorm[dId] ** 0.5

def loadIni():
	INI_PATH = os.path.dirname(os.path.realpath(__file__)) + "/config.ini"
	Config = ConfigParser.ConfigParser()
	Config.read(INI_PATH)
	iniData = {}
	sections = Config.sections()
	for option in Config.options(sections[0]):
		opValue = Config.get(sections[0], option)
		iniData[option] = opValue if opValue != -1 else False;
	return iniData

def loadIndexConfig(iniData):
    indexConfig = {}
    if "stopwords" in iniData and iniData["stopwords"]:
        if not (os.path.exists(iniData["stopwords"])):
            raise IniException("in 'stopwords' property: not a valid file path")
        indexConfig["stopwords"] = iniData['stopwords']
	if "stem" in iniData and iniData["stem"]:
		indexConfig["stem"] = iniData['stem']
	if "term_min_size" in iniData and iniData["term_min_size"]:
		indexConfig["term_min_size"] = int(iniData["term_min_size"])
	if "term_max_size" in iniData and iniData["term_max_size"]:
		indexConfig["term_max_size"] = int(iniData["term_max_size"])
	return indexConfig

def loadMetadata(metadataDir):
    metadata = {}
    with open(metadataDir, "rb") as f:
        binMeta = f.read(8)
        meta = struct.unpack('<2I', binMeta)
        metadata["docs"] = meta[0]
        metadata["terms"] = meta[1]
    return metadata

def loadMaxfreqs(metadataDir, docs):
    maxfreqs = {}
    with open(metadataDir, "rb") as f:
        mfRead = f.read(docs * 4)
        mfRead = struct.unpack('<%iI' % docs, mfRead)
        for docId in range(0, docs):
            maxfreqs[docId] = int(mfRead[docId])
    return maxfreqs

def loadDf(dfDir, terms):
    df = {}
    with open(dfDir, "rb") as f:
        read = f.read(terms * 4)
        read = struct.unpack('<%iI' % terms, read)
        for tId in range(0, terms):
            df[tId] = int(read[tId])
    return df

def loadPostings(postingsDir, df):
    postings = collections.OrderedDict()
    lenPostingLists = len(df)
    with open(postingsDir, "rb") as f:
        for tId in range(0, lenPostingLists):
            logging.info("%d of %d" % (tId, lenPostingLists))
            postings[tId] = collections.OrderedDict()
            lenPosting = df[tId]
            docIdsRead = struct.unpack('<%iI' % lenPosting, f.read(lenPosting * 4))
            freqsRead = struct.unpack('<%iI' % lenPosting, f.read(lenPosting * 4))
            for i in range(0, lenPosting):
                postings[tId][int(docIdsRead[i])] = int(freqsRead[i])
    return postings
<|endoftext|>


<commit_before>import datetime
import random

import pytest
import mock

import irc.client

def test_version():
	assert 'VERSION' in vars(irc.client)
	assert isinstance(irc.client.VERSION, tuple)
	assert irc.client.VERSION, "No VERSION detected."

def test_delayed_command_order():
	"""
	delayed commands should be sorted by delay time
	"""
	null = lambda: None
	delays = [random.randint(0, 99) for x in xrange(5)]
	cmds = sorted([
		irc.client.DelayedCommand(delay, null, tuple())
		for delay in delays
	])
	assert [c.delay.seconds for c in cmds] == sorted(delays)

def test_periodic_command_fixed_delay():
	"""
	Test that we can construct a periodic command with a fixed initial
	delay.
	"""
	fd = irc.client.PeriodicCommandFixedDelay.at_time(
		at = datetime.datetime.now(),
		delay = datetime.timedelta(seconds=2),
		function = lambda: None,
		arguments = [],
		)
	assert fd.due() == True
	assert fd.next().due() == False

@mock.patch('irc.connection.socket')
def test_privmsg_sends_msg(socket_mod):
	server = irc.client.IRC().server()
	server.connect('foo', 6667, 'bestnick')
	# make sure the mock object doesn't have a write method or it will treat
	#  it as an SSL connection and never call .send.
	del server.socket.write
	server.privmsg('#best-channel', 'You are great')
	server.socket.send.assert_called_with(
		b'PRIVMSG #best-channel :You are great\r\n')

@mock.patch('irc.connection.socket')
def test_privmsg_fails_on_embedded_carriage_returns(socket_mod):
	server = irc.client.IRC().server()
	server.connect('foo', 6667, 'bestnick')
	with pytest.raises(ValueError):
		server.privmsg('#best-channel', 'You are great\nSo are you')
<commit_msg>Adding test to capture #6<commit_after>import datetime
import random

import pytest
import mock

import irc.client

def test_version():
	assert 'VERSION' in vars(irc.client)
	assert isinstance(irc.client.VERSION, tuple)
	assert irc.client.VERSION, "No VERSION detected."

def test_delayed_command_order():
	"""
	delayed commands should be sorted by delay time
	"""
	null = lambda: None
	delays = [random.randint(0, 99) for x in xrange(5)]
	cmds = sorted([
		irc.client.DelayedCommand(delay, null, tuple())
		for delay in delays
	])
	assert [c.delay.seconds for c in cmds] == sorted(delays)

def test_periodic_command_fixed_delay():
	"""
	Test that we can construct a periodic command with a fixed initial
	delay.
	"""
	fd = irc.client.PeriodicCommandFixedDelay.at_time(
		at = datetime.datetime.now(),
		delay = datetime.timedelta(seconds=2),
		function = lambda: None,
		arguments = [],
		)
	assert fd.due() == True
	assert fd.next().due() == False

@mock.patch('irc.connection.socket')
def test_privmsg_sends_msg(socket_mod):
	server = irc.client.IRC().server()
	server.connect('foo', 6667, 'bestnick')
	# make sure the mock object doesn't have a write method or it will treat
	#  it as an SSL connection and never call .send.
	del server.socket.write
	server.privmsg('#best-channel', 'You are great')
	server.socket.send.assert_called_with(
		b'PRIVMSG #best-channel :You are great\r\n')

@mock.patch('irc.connection.socket')
def test_privmsg_fails_on_embedded_carriage_returns(socket_mod):
	server = irc.client.IRC().server()
	server.connect('foo', 6667, 'bestnick')
	with pytest.raises(ValueError):
		server.privmsg('#best-channel', 'You are great\nSo are you')

@mock.patch('irc.connection.socket')
def test_connect_with_legacy_params(socket_mod):
	server = irc.client.IRC().server()
	server.connect('foo', 6667, 'bestnick', ssl=True)
	import pytest; pytest.set_trace()
<|endoftext|>


<commit_before>import discord
from discord.ext import commands
from discord.permissions import Permissions
from .utils import checks
import asyncio
from .utils.dataIO import dataIO
import os

client = discord.Client()

class EventBets:
    """Create an event which people can bet on"""

    def __init__(self, bot):
        self.bot = bot
        self.file_path = "data/irlbetting/irlbetting.json"
        self.json_data = dataIO.load_json(self.file_path)

    @commands.command(pass_context=True, aliases=["ce"])
    @checks.admin_or_permissions(manage_server=True)
    @client.event
    async def createevent(self, ctx):
        """Create event"""

        #Your code will go here
        user = ctx.message.author
        await self.bot.say("What is the name of the event?")
        eventname = await self.bot.wait_for_message(timeout=30, author=user)
        if eventname is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        eventname = str(eventname.content)
        await self.bot.say("Event name: \n" + eventname)
        await self.bot.say("What are the possible outcomes of the event? You must have atleast 2. To stop inputting outcomes simply type `.`")
        a = 0
        outcome = []
        message = ""
        while not message == ".":
            await self.bot.say(str(a+1) + ": ")
            outcome.append(await self.bot.wait_for_message(timeout=30, author=user))
            if outcome[a] is None:
                if a == 3:
                    message = "."
                else:
                    await self.bot.say("You didn't enter anything. Event cancelled")
                    return
            else:
                outcome[a] = str(outcome[a].content)
                message = outcome[a]
            a += 1
        if len(outcome) < 2:
            await self.bot.say("You need atleast 2 outcomes. Event cancelled")
            return
        await self.bot.say("Outcomes: ")
        i = 0
        while i < a-1:
            await self.bot.say(outcome[i])
            i += 1
        await self.bot.say("What should the payout multiplier be?")
        payoutM = await self.bot.wait_for_message(timeout=30, author=user)
        if payoutM is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            payoutM = int(payoutM.content)
        except ValueError:
            await self.bot.say("You must enter a number. Event cancelled")
            return
        await self.bot.say("The multiplier has been set to: " + str(payoutM))
        await self.bot.say("In what month does the event happen? (Give as a number)")
        month = await self.bot.wait_for_message(timeout = 30, author = user)
        if month is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            month = int(month.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("What day of the month does the event happen? (Must be a number)")
        day = await self.bot.wait_for_message(timeout=30, author = user)
        if day is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            day = int(day.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("What hour of the day does the event happen? (Must be in 24 hour format IE: 9PM is 21)")
        hour = await self.bot.wait_for_message(timeout=30, author = user)
        if hour is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            hour = int(hour.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("The event happens at " + str(hour) + ":00 on " + str(month) + "/" + str(day))
        numberofcurrentevents = self.json_data["Events"]["CurrentEvents"]
        self.json_data["Events"][str(numberofcurrentevents+1)] = {}
        self.json_data["Events"][str(numberofcurrentevents+1)]["Name"] = eventname
        self.json_data["Events"][str(numberofcurrentevents+1)]["Multiplier"] = payoutM
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"] = {}
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Month"] = month
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Day"] = day
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Hour"] = hour
        self.json_data["Events"][str(numberofcurrentevents+1)]["Users"] = {}
        c = 0
        while c < len(outcome):
            self.json_data["Events"][str(numberofcurrentevents+1)]["Outcomes"] = {}
            self.json_data["Events"][str(numberofcurrentevents+1)]["Outcomes"][str(c+1)] = outcome[c]
            c += 1
        self.json_data["Events"]["CurrentEvents"] = self.json_data["Events"]["CurrentEvents"]+1
        dataIO.save_json(self.file_path, self.json_data)

def check_folders():
    if not os.path.exists("data/irlbetting"):
        print("Creating data/irlbetting floder...")
        os.makedirs("data/irlbetting")

def check_files():
    system = {"Events": {"1": {"Name": "",
                               "Multiplier": 1,
                               "Users": { "1": {"Bet": 0,
                                                "Choice": ""},
                                        },
                               "Date": {"Hour": 0,
                                        "Day": 0,
                                        "Month": 0},
                               "Outcomes": {"1": ""}},
                        "CurrentEvents": 0}}

    f = "data/irlbetting/irlbetting.json"
    if not dataIO.is_valid_json(f):
        print("Creating defualt irlbetting.json...")
        dataIO.save_json(f, system)

def setup(bot):
    check_folders()
    check_files()
    n = EventBets(bot)
    bot.add_cog(n)
<commit_msg>aliases<commit_after>import discord
from discord.ext import commands
from discord.permissions import Permissions
from .utils import checks
import asyncio
from .utils.dataIO import dataIO
import os

client = discord.Client()

class EventBets:
    """Create an event which people can bet on"""

    def __init__(self, bot):
        self.bot = bot
        self.file_path = "data/irlbetting/irlbetting.json"
        self.json_data = dataIO.load_json(self.file_path)

    @commands.command(pass_context=True, aliases=["ce", "event create"])
    @checks.admin_or_permissions(manage_server=True)
    @client.event
    async def createevent(self, ctx):
        """Create event"""

        #Your code will go here
        user = ctx.message.author
        await self.bot.say("What is the name of the event?")
        eventname = await self.bot.wait_for_message(timeout=30, author=user)
        if eventname is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        eventname = str(eventname.content)
        await self.bot.say("Event name: \n" + eventname)
        await self.bot.say("What are the possible outcomes of the event? You must have atleast 2. To stop inputting outcomes simply type `.`")
        a = 0
        outcome = []
        message = ""
        while not message == ".":
            await self.bot.say(str(a+1) + ": ")
            outcome.append(await self.bot.wait_for_message(timeout=30, author=user))
            if outcome[a] is None:
                if a == 3:
                    message = "."
                else:
                    await self.bot.say("You didn't enter anything. Event cancelled")
                    return
            else:
                outcome[a] = str(outcome[a].content)
                message = outcome[a]
            a += 1
        if len(outcome) < 2:
            await self.bot.say("You need atleast 2 outcomes. Event cancelled")
            return
        await self.bot.say("Outcomes: ")
        i = 0
        while i < a-1:
            await self.bot.say(outcome[i])
            i += 1
        await self.bot.say("What should the payout multiplier be?")
        payoutM = await self.bot.wait_for_message(timeout=30, author=user)
        if payoutM is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            payoutM = int(payoutM.content)
        except ValueError:
            await self.bot.say("You must enter a number. Event cancelled")
            return
        await self.bot.say("The multiplier has been set to: " + str(payoutM))
        await self.bot.say("In what month does the event happen? (Give as a number)")
        month = await self.bot.wait_for_message(timeout = 30, author = user)
        if month is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            month = int(month.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("What day of the month does the event happen? (Must be a number)")
        day = await self.bot.wait_for_message(timeout=30, author = user)
        if day is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            day = int(day.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("What hour of the day does the event happen? (Must be in 24 hour format IE: 9PM is 21)")
        hour = await self.bot.wait_for_message(timeout=30, author = user)
        if hour is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            hour = int(hour.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("The event happens at " + str(hour) + ":00 on " + str(month) + "/" + str(day))
        numberofcurrentevents = self.json_data["Events"]["CurrentEvents"]
        self.json_data["Events"][str(numberofcurrentevents+1)] = {}
        self.json_data["Events"][str(numberofcurrentevents+1)]["Name"] = eventname
        self.json_data["Events"][str(numberofcurrentevents+1)]["Multiplier"] = payoutM
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"] = {}
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Month"] = month
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Day"] = day
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Hour"] = hour
        self.json_data["Events"][str(numberofcurrentevents+1)]["Users"] = {}
        c = 0
        while c < len(outcome):
            self.json_data["Events"][str(numberofcurrentevents+1)]["Outcomes"] = {}
            self.json_data["Events"][str(numberofcurrentevents+1)]["Outcomes"][str(c+1)] = outcome[c]
            c += 1
        self.json_data["Events"]["CurrentEvents"] = self.json_data["Events"]["CurrentEvents"]+1
        dataIO.save_json(self.file_path, self.json_data)

    @commands.command(pass_context=True, aliases=["be", "event bet"])
    @client.event
    async def betevent(self, ctx):
        """Create event"""

def check_folders():
    if not os.path.exists("data/irlbetting"):
        print("Creating data/irlbetting floder...")
        os.makedirs("data/irlbetting")

def check_files():
    system = {"Events": {"1": {"Name": "",
                               "Multiplier": 1,
                               "Users": { "1": {"Bet": 0,
                                                "Choice": ""},
                                        },
                               "Date": {"Hour": 0,
                                        "Day": 0,
                                        "Month": 0},
                               "Outcomes": {"1": ""}},
                        "CurrentEvents": 0}}

    f = "data/irlbetting/irlbetting.json"
    if not dataIO.is_valid_json(f):
        print("Creating defualt irlbetting.json...")
        dataIO.save_json(f, system)

def setup(bot):
    check_folders()
    check_files()
    n = EventBets(bot)
    bot.add_cog(n)
<|endoftext|>


<commit_before># noqa
from dodo_commands.system_commands import DodoCommand, CommandError
import sys
import os

def _extra_dir():
    import dodo_commands
    return os.path.join(
        os.path.dirname(dodo_commands.__file__), "extra"
    )

class Command(DodoCommand):  # noqa
    help = (
        "Install default command directories supplied by the " +
        "paths argument."
    )
    decorators = []

    def add_arguments_imp(self, parser):  # noqa
        parser.add_argument(
            "paths",
            nargs='*',
            help='Create a symlink to these command directories'
        )
        parser.add_argument(
            "--pip",
            nargs='*',
            help='Pip install the commands in these packages and create symlinks'
        )

    def _report_error(self, msg):
        sys.stderr.write(msg + os.linesep)

    def _install_commands(self, path):
        """Install the dir with the default commands."""
        dest_dir = os.path.join(
            os.path.expanduser("~/.dodo_commands/default_commands"),
            os.path.basename(path)
        )

        if not os.path.exists(path):
            alt_path = os.path.join(_extra_dir(), path)
            if os.path.exists(alt_path):
                path = alt_path
            else:
                self._report_error("Error: path not found: %s" % path)
                return False

        if os.path.exists(dest_dir):
            return False

        try:
            os.symlink(os.path.abspath(path), dest_dir)
        except:
            self._report_error("Error: could not create a symlink in %s." % dest_dir)
            return False

        return True

    def _install_package(self, package):
        pip = os.path.join(os.path.dirname(sys.executable), "pip")
        default_commands_dir = os.path.expanduser("~/.dodo_commands/default_commands")
        self.runcmd([
            pip, 'install', '--upgrade', '--target', default_commands_dir, package
        ])

    def handle_imp(self, paths, pip, **kwargs):  # noqa
        import dodo_commands
        dodo_commands_path = dodo_commands.__path__[0]
        if os.path.realpath(dodo_commands_path) != dodo_commands_path:
            raise CommandError("Please deactivate your dodo project first.")

        for path in paths:
            self._install_commands(path)

        for package in pip:
            self._install_package(package)
<commit_msg>Improve help text<commit_after># noqa
from dodo_commands.system_commands import DodoCommand, CommandError
import sys
import os

def _extra_dir():
    import dodo_commands
    return os.path.join(
        os.path.dirname(dodo_commands.__file__), "extra"
    )

def _packages_in_extra_dir():
    extra_dir = _extra_dir()
    packages = [
        x for x in os.listdir(extra_dir)
        if os.path.isdir(os.path.join(extra_dir, x))
        and not x.startswith('__')
    ]

    if len(packages) == 0:
        return ""

    if len(packages) == 1:
        msg = " The %s package is found automagically " % packages[0]
    else:
        packages[-1] = "and " + packages[-1]
        msg = " The %s packages are found automagically " % ", ".join(packages)

    return (
        msg + "in the dodo_commands.extra package" +
        ", e.g. the following works: dodo install-default-commands %s." % packages[0]
    )

class Command(DodoCommand):  # noqa
    help = (
        "Install default command directories supplied by the " +
        "paths argument: dodo install-default-commands " +
        "/path/to/my/commands. " + _packages_in_extra_dir()
    )
    decorators = []

    def add_arguments_imp(self, parser):  # noqa
        parser.add_argument(
            "paths",
            nargs='*',
            help='Create symlinks to these command directories'
        )
        parser.add_argument(
            "--pip",
            nargs='*',
            help='Pip install the commands in these packages'
        )

    def _report_error(self, msg):
        sys.stderr.write(msg + os.linesep)

    def _install_commands(self, path):
        """Install the dir with the default commands."""
        dest_dir = os.path.join(
            os.path.expanduser("~/.dodo_commands/default_commands"),
            os.path.basename(path)
        )

        if not os.path.exists(path):
            alt_path = os.path.join(_extra_dir(), path)
            if os.path.exists(alt_path):
                path = alt_path
            else:
                self._report_error("Error: path not found: %s" % path)
                return False

        if os.path.exists(dest_dir):
            return False

        try:
            os.symlink(os.path.abspath(path), dest_dir)
        except:
            self._report_error("Error: could not create a symlink in %s." % dest_dir)
            return False

        return True

    def _install_package(self, package):
        pip = os.path.join(os.path.dirname(sys.executable), "pip")
        default_commands_dir = os.path.expanduser("~/.dodo_commands/default_commands")
        self.runcmd([
            pip, 'install', '--upgrade', '--target', default_commands_dir, package
        ])

    def handle_imp(self, paths, pip, **kwargs):  # noqa
        import dodo_commands
        dodo_commands_path = dodo_commands.__path__[0]
        if os.path.realpath(dodo_commands_path) != dodo_commands_path:
            raise CommandError("Please deactivate your dodo project first.")

        for path in paths:
            self._install_commands(path)

        for package in pip:
            self._install_package(package)
<|endoftext|>


<commit_before>__author__ = 'Ashish'
from nltk.tokenize import sent_tokenize
import json
import uuid

class TagTogReader(object):
	def __init__(self, file_location):
		self.file_location = file_location
		self.documents = {}
		self.symbols = [',', '.', '(', ')', ':', ';', '[', ']']
		self.punctuations = ['.', ',']
		self.json_content = {}

	def parse(self):
		data_file = open(self.file_location)
		tempString = ""
		id = 1
		for line in data_file:
			if str(line).strip().startswith("###MEDLINE"):
				if tempString!="":
					self.documents[pmid.rstrip()]={}
					sentences = sent_tokenize(tempString.lstrip().rstrip())
					self.documents[pmid.rstrip()]['abstract']=sentences[0]
					self.documents[pmid.rstrip()]['text'] = ' '.join(sentences[1:])
					id = 1
					json_single={}
					json_single['annotatable']={}
					json_single['annotatable']['parts']=["s1h1", "s2h1", "s2p1"]
					json_single['anncomplete']=False
					json_single['sources']=[]
					json_single['sources'].append({"name": "MEDLINE", "id": pmid.rstrip(), "url": None})
					json_single['entities']=entities
					json_single['relations']=[]
					json_single['metas']={}
					self.json_content[pmid.rstrip()]=json_single
				entities = []
				pmid = line.split(":")[1]
				tempString = ""
				is_title = True
			elif str(line)=='\n':
				continue
			else:
				tempWord = str(line).split('\t')
				if tempWord[0] in self.symbols:
					tempString = tempString + tempWord[0]
					if (tempWord[0]=="." and is_title==True):
						is_title = False;
					if (entity and tempWord[1].startswith("I-")):
						if tempWord[0]=="(":
							word = word+" "+tempWord[0]
							word.rstrip()
						elif word.endswith("("):
							word = word+tempWord[0];
						elif tempWord[0]==")":
							word = word+tempWord[0]
					if (isEntity and tempWord[1].startswith("O")):
						entity['offsets'][0]['text'] = word
						entities.append(entity)
						isEntity = False
						entity = {}
				else:
					if tempWord[1].startswith("B-protein") or tempWord[1].startswith("B-RNA") or tempWord[1].startswith("B-DNA"):
						if tempWord[1].startswith("B-protein"):
							classId = "e_1"
						elif tempWord[1].startswith("B-DNA"):
							classId = "e_2"
						else:
							classId = "e_3"
						entity = {}
						entity['classId']=classId
						word = tempWord[0]
						entity['offsets']=[{'start': len(tempString)+1}]
						entity['confidence']={'state': "", 'who': ['user:genia4er'], 'prob': 1.0000}
						isEntity = True
						if (is_title):
							entity['part']='s1h1'
						else:
							entity['part']='s2s1h1'
					elif tempWord[1].startswith("I-protein") or tempWord[1].startswith("I-RNA") or tempWord[1].startswith("I-DNA"):
						if word.endswith("("):
							word = word+tempWord[0]
						else:
							word = word+" "+tempWord[0]
					elif (tempWord[1].startswith("O") and isEntity):
						entity['offsets'][0]['text'] = word
						entities.append(entity)
						isEntity = False
						entity = {}
					tempString = tempString + ' ' + tempWord[0]
		data_file.close()

	def createJSON(self):
		for key in self.json_content:
			cutoff = len(self.documents[key]['abstract'])
			for i in range(len(self.json_content[key]['entities'])):
				if self.json_content[key]['entities'][i]['offsets'][0]['start']>cutoff:
					self.json_content[key]['entities'][i]['part']='s2h1'
				else:
					self.json_content[key]['entities'][i]['part']='s1h1'
			f = open('/home/ashish/mthesis-ashish/resources/corpora/entity_recognition/jnlpba/anndoc/'+str(key)+'.ann.json', 'w')
			f.write(json.dumps(self.json_content[key], sort_keys=True, indent=2, separators=(',', ': ')))
			f.close()

	def writeHTML(self, key):
		title = self.documents.get(key)['abstract']
		abstr = self.documents.get(key)['text']

		doctype = "<!DOCTYPE html>\n"

		hashId = str(uuid.uuid4().hex) + ':' + key.rstrip()

		header = '\n' \
				 "\t\n" \
				 '\t\t<meta charset="UTF-8"/>\n' \
		 '\t\t<meta name="generator" content="org.rostlab.relna"/>\n' \
				 "\t\t<title>" + hashId + "</title>\n" \
				 "\t\n"

		body = "\t\n" \
			   "\t\t
\n" \
			   '\t\t\t
\n' \
			   '\t\t\t\t
' + title + "
\n" \
			   "\t\t\t
\n"

		abstract = 	'\t\t\t
\n' \
					'\t\t\t\t
' \
				   	'Abstract' \
				   	'
\n' \
					'\t\t\t\t
\n'

		content = '\t\t\t\t\t
'

		content_close = '
\n'

		abstract_close = "\t\t\t\t
\n\t\t\t
\n"

		body_close = "\t\t
\n" \
					 "\t\n"

		html_close = ""

		html_file = open('/home/ashish/mthesis-ashish/resources/corpora/entity_recognition/jnlpba/anndoc/'+str(key).rstrip()+'.html', 'w')
		html_file.write(doctype)
		html_file.write(header)
		html_file.write(body)
		html_file.write(abstract)
		html_file.write(content)
		html_file.write(abstr)
		html_file.write(content_close)
		html_file.write(abstract_close)
		html_file.write(body_close)
		html_file.write(html_close)
		return

	def convert_to_html(self):
		for pmid in self.documents.keys():
			self.writeHTML(pmid)

	def print_documents(self):
		for pmid  in self.documents.keys():
			print pmid, self.documents[pmid]

tagtog = TagTogReader('/home/ashish/mthesis-ashish/resources/corpora/entity_recognition/jnlpba/iob/train/Genia4ERtask2.iob2')
tagtog.parse()
tagtog.convert_to_html()
tagtog.createJSON()
<commit_msg>Bug fixes to jnlpba2tagtogconverter<commit_after>__author__ = 'Ashish'
from nltk.tokenize import sent_tokenize
import json
import uuid

class TagTogReader(object):
	def __init__(self, file_location):
		self.file_location = file_location
		self.documents = {}
		self.symbols = [',', '.', '(', ')', ':', ';', '[', ']']
		self.punctuations = ['.', ',']
		self.json_content = {}

	def parse(self):
		data_file = open(self.file_location)
		tempString = ""
		id = 1
		for line in data_file:
			if str(line).strip().startswith("###MEDLINE"):
				if tempString!="":
					self.documents[pmid.rstrip()]={}
					sentences = sent_tokenize(tempString.lstrip().rstrip())
					self.documents[pmid.rstrip()]['abstract']=sentences[0]
					self.documents[pmid.rstrip()]['text'] = ' '.join(sentences[1:])
					id = 1
					json_single={}
					json_single['annotatable']={}
					json_single['annotatable']['parts']=["s1h1", "s2h1", "s2p1"]
					json_single['anncomplete']=False
					json_single['sources']=[]
					json_single['sources'].append({"name": "MEDLINE", "id": pmid.rstrip(), "url": None})
					json_single['entities']=entities
					json_single['relations']=[]
					json_single['metas']={}
					self.json_content[pmid.rstrip()]=json_single
				entities = []
				pmid = line.split(":")[1]
				tempString = ""
				is_title = True
			elif str(line)=='\n':
				continue
			else:
				tempWord = str(line).split('\t')
				if tempWord[0] in self.symbols:
					if tempWord[0].startswith('('):
						tempString = tempString + ' ' + tempWord[0]
					else:
						tempString = tempString + tempWord[0]
					if (tempWord[0]=="." and is_title==True):
						is_title = False;
					if (entity and tempWord[1].startswith("I-")):
						if tempWord[0]=="(":
							word = word+" "+tempWord[0]
							word.rstrip()
						elif word.endswith("("):
							word = word+tempWord[0];
						elif tempWord[0]==")":
							word = word+tempWord[0]
					if (isEntity and tempWord[1].startswith("O")):
						entity['offsets'][0]['text'] = word
						entities.append(entity)
						isEntity = False
						entity = {}
				else:
					if tempWord[1].startswith("B-protein") or tempWord[1].startswith("B-RNA") or tempWord[1].startswith("B-DNA"):
						if tempWord[1].startswith("B-protein"):
							classId = "e_1"
						elif tempWord[1].startswith("B-DNA"):
							classId = "e_2"
						else:
							classId = "e_3"
						entity = {}
						entity['classId']=classId
						word = tempWord[0]
						entity['offsets']=[{'start': len(tempString)}]
						entity['confidence']={'state': "", 'who': ['user:genia4er'], 'prob': 1.0000}
						isEntity = True
						if (is_title):
							entity['part']='s1h1'
						else:
							entity['part']='s2p1'
					elif tempWord[1].startswith("I-protein") or tempWord[1].startswith("I-RNA") or tempWord[1].startswith("I-DNA"):
						if word.endswith("("):
							word = word+tempWord[0]
						else:
							word = word+" "+tempWord[0]
					elif (tempWord[1].startswith("O") and isEntity):
						entity['offsets'][0]['text'] = word
						entities.append(entity)
						isEntity = False
						entity = {}
					if tempString.endswith('('):
						tempString = tempString + tempWord[0]
					else:
						tempString = tempString + ' ' + tempWord[0]
		data_file.close()

	def createJSON(self):
		for key in self.json_content:
			cutoff = len(self.documents[key]['abstract'])
			for i in range(len(self.json_content[key]['entities'])):
				if self.json_content[key]['entities'][i]['offsets'][0]['start']>cutoff:
					self.json_content[key]['entities'][i]['part']='s2p1'
				else:
					self.json_content[key]['entities'][i]['part']='s1h1'
			f = open('/home/ashish/mthesis-ashish/resources/corpora/entity_recognition/jnlpba/anndoc/'+str(key)+'.ann.json', 'w')
			f.write(json.dumps(self.json_content[key], sort_keys=True, indent=2, separators=(',', ': ')))
			f.close()

	def writeHTML(self, key):
		title = self.documents.get(key)['abstract']
		abstr = self.documents.get(key)['text']

		doctype = "<!DOCTYPE html>\n"

		hashId = str(uuid.uuid4().hex) + ':' + key.rstrip()

		header = '\n' \
				 "\t\n" \
				 '\t\t<meta charset="UTF-8"/>\n' \
		 '\t\t<meta name="generator" content="org.rostlab.relna"/>\n' \
				 "\t\t<title>" + hashId + "</title>\n" \
				 "\t\n"

		body = "\t\n" \
			   "\t\t
\n" \
			   '\t\t\t
\n' \
			   '\t\t\t\t
' + title + "
\n" \
			   "\t\t\t
\n"

		abstract = 	'\t\t\t
\n' \
					'\t\t\t\t
' \
				   	'Abstract' \
				   	'
\n' \
					'\t\t\t\t
\n'

		content = '\t\t\t\t\t
'

		content_close = '
\n'

		abstract_close = "\t\t\t\t
\n\t\t\t
\n"

		body_close = "\t\t
\n" \
					 "\t\n"

		html_close = ""

		html_file = open('/home/ashish/mthesis-ashish/resources/corpora/entity_recognition/jnlpba/anndoc/'+str(key).rstrip()+'.html', 'w')
		html_file.write(doctype)
		html_file.write(header)
		html_file.write(body)
		html_file.write(abstract)
		html_file.write(content)
		html_file.write(abstr)
		html_file.write(content_close)
		html_file.write(abstract_close)
		html_file.write(body_close)
		html_file.write(html_close)
		return

	def convert_to_html(self):
		for pmid in self.documents.keys():
			self.writeHTML(pmid)

	def print_documents(self):
		for pmid  in self.documents.keys():
			print pmid, self.documents[pmid]

tagtog = TagTogReader('/home/ashish/mthesis-ashish/resources/corpora/entity_recognition/jnlpba/iob/train/Genia4ERtask2.iob2')
tagtog.parse()
tagtog.convert_to_html()
tagtog.createJSON()
<|endoftext|>


<commit_before>import discord
from discord.ext import commands
from discord.permissions import Permissions
from .utils import checks
import asyncio
from .utils.dataIO import dataIO
import os
import time

client = discord.Client()

class EventBets:
    """Create an event which people can bet on"""

    def __init__(self, bot):
        self.bot = bot
        self.file_path = "data/irlbetting/irlbetting.json"
        self.json_data = dataIO.load_json(self.file_path)

    @commands.command(pass_context=True, aliases=["ce", "eventcreate"])
    @checks.admin_or_permissions(manage_server=True)
    @client.event
    async def createevent(self, ctx):
        """Create event"""

        #Your code will go here
        user = ctx.message.author
        await self.bot.say("What is the name of the event?")
        eventname = await self.bot.wait_for_message(timeout=30, author=user)
        if eventname is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        eventname = str(eventname.content)
        await self.bot.say("Event name: \n" + eventname)
        await self.bot.say("What are the possible outcomes of the event? You must have atleast 2. To stop inputting outcomes simply type `.`")
        a = 0
        outcome = []
        message = ""
        while not message == ".":
            await self.bot.say(str(a+1) + ": ")
            outcome.append(await self.bot.wait_for_message(timeout=30, author=user))
            if outcome[a] is None:
                if a == 3:
                    message = "."
                else:
                    await self.bot.say("You didn't enter anything. Event cancelled")
                    return
            else:
                outcome[a] = str(outcome[a].content)
                message = outcome[a]
            a += 1
        if len(outcome) < 2:
            await self.bot.say("You need atleast 2 outcomes. Event cancelled")
            return
        await self.bot.say("Outcomes: ")
        i = 0
        while i < a-1:
            await self.bot.say(outcome[i])
            i += 1
        await self.bot.say("What should the payout multiplier be?")
        payoutM = await self.bot.wait_for_message(timeout=30, author=user)
        if payoutM is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            payoutM = int(payoutM.content)
        except ValueError:
            await self.bot.say("You must enter a number. Event cancelled")
            return
        await self.bot.say("The multiplier has been set to: " + str(payoutM))
        await self.bot.say("In what month does the event happen? (Give as a number)")
        month = await self.bot.wait_for_message(timeout = 30, author = user)
        if month is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            month = int(month.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("What day of the month does the event happen? (Must be a number)")
        day = await self.bot.wait_for_message(timeout=30, author = user)
        if day is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            day = int(day.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("What hour of the day does the event happen? (Must be in 24 hour format IE: 9PM is 21)")
        hour = await self.bot.wait_for_message(timeout=30, author = user)
        if hour is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            hour = int(hour.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("The event happens at " + str(hour) + ":00 on " + str(month) + "/" + str(day))
        numberofcurrentevents = self.json_data["Events"]["CurrentEvents"]
        self.json_data["Events"][str(numberofcurrentevents+1)] = {}
        self.json_data["Events"][str(numberofcurrentevents+1)]["Name"] = eventname
        self.json_data["Events"][str(numberofcurrentevents+1)]["Multiplier"] = payoutM
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"] = {}
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Month"] = month
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Day"] = day
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Hour"] = hour
        self.json_data["Events"][str(numberofcurrentevents+1)]["Users"] = {}
        self.json_data["Events"][str(numberofcurrentevents+1)]["CurrentUsers"] = 0
        self.json_data["Events"][str(numberofcurrentevents+1)]["Outcomes"] = {}
        c = 0
        while c < len(outcome)-1:
            self.json_data["Events"][str(numberofcurrentevents+1)]["Outcomes"][str(c+1)] = outcome[c]
            c += 1
        self.json_data["Events"]["CurrentEvents"] = self.json_data["Events"]["CurrentEvents"]+1
        dataIO.save_json(self.file_path, self.json_data)

    @commands.command(pass_context=True, aliases=["be", "eventbet"])
    @client.event
    async def betevent(self, ctx):
        """Bet on event"""

        user = ctx.message.author
        bank = self.bot.get_cog('Economy').bank
        if not bank.account_exists(user):
            await self.bot.say("You don't have a bank account so you can't bet on events. Do `*bank register` to make an account")
            return
        numberofcurrentevents = self.json_data["Events"]["CurrentEvents"]
        a = 1
        if numberofcurrentevents == 0:
            await self.bot.say("There are no events running right now, sorry")
            return
        await self.bot.say("Which event would you like to bet on?: ")
        while a <= numberofcurrentevents:
            await self.bot.say(str(a) + ": " + self.json_data["Events"][str(a)]["Name"])
            a += 1
        await self.bot.say("Enter the number of the event")
        event = await self.bot.wait_for_message(timeout = 30, author = user)
        if event is None:
            await self.bot.say("You didn't enter anything. Bet cancelled")
            return
        try:
            event = int(event.content)
        except ValueError:
            await self.bot.say("You need to enter a number. Bet cancelled")
            return
        if event > numberofcurrentevents:
            await self.bot.say("That is not a valid event number. Bet cancelled")
            return
        i = 1
        while i <= self.json_data["Events"][str(event)]["CurrentUsers"]:
            print(i)
            if user == discord.utils.get(ctx.message.server.members, id=self.json_data["Events"][str(event)]["Users"][str(i)]["ID"]):
                await self.bot.say("You cannot bet on an event more than once. Bet cancelled")
                return
            i += 1
        await self.bot.say("You have picked: " + self.json_data["Events"][str(event)]["Name"] + ". \nThe outcomes for this event are:")
        d = 1
        while d <= len(self.json_data["Events"][str(event)]["Outcomes"]):
            await self.bot.say(str(d) + ": " + self.json_data["Events"][str(event)]["Outcomes"][str(d)])
            d += 1
        await self.bot.say("Enter the number of the outcome you would like to bet on: ")
        outcome = await self.bot.wait_for_message(timeout = 30, author = user)
        if outcome is None:
            await self.bot.say("You didn't enter anything. Bet cancelled")
            return
        try:
            outcome = int(outcome.content)
        except ValueError:
            await self.bot.say("You need to enter a number. Bet cancelled")
            return
        await self.bot.say("You have picked: " + self.json_data["Events"][str(event)]["Outcomes"][str(outcome)] + ". You have $" + str(bank.get_balance(user)) + ". How much would you like to bet?")
        bet = await self.bot.wait_for_message(timeout = 30, author = user)
        if bet is None:
            await self.bot.say("You didn't enter anything. Bet cancelled")
            return
        try:
            bet = int(bet.content)
        except ValueError:
            await self.bot.say("You need to enter a number. Bet cancelled")
            return
        if bet > bank.get_balance(user):
            await self.bot.say("You don't have enough money. Bet cancelled")
            return
        bank.withdraw_credits(user, bet)
        numberofcurrentusers = self.json_data["Events"][str(event)]["CurrentUsers"]
        self.json_data["Events"][str(event)]["Users"][str(numberofcurrentusers+1)] = {}
        self.json_data["Events"][str(event)]["Users"][str(numberofcurrentusers+1)]["ID"] = str(user.id)
        self.json_data["Events"][str(event)]["Users"][str(numberofcurrentusers+1)]["Bet"] = bet
        self.json_data["Events"][str(event)]["Users"][str(numberofcurrentusers+1)]["Choice"] = outcome
        self.json_data["Events"][str(event)]["CurrentUsers"] = self.json_data["Events"][str(event)]["CurrentUsers"]+1
        dataIO.save_json(self.file_path, self.json_data)
        await self.bot.say("You have placed $" + str(bet) + " on " + str(self.json_data["Events"][str(event)]["Users"][str(numberofcurrentusers+1)]["Choice"]))

    @commands.command(pass_context=True, aliases=["fe", "finishresults"])
    @checks.admin_or_permissions(manage_server=True)
    @client.event
    async def finishevent(self, ctx):
        """Finish event"""

        month = time.strftime('%m')
        day = time.strftime('%d')
        hour = time.strftime('%H')
        tzoffset = time.strftime('%z')
        month = int(month)
        day = int(day)
        hour - int(hour)
        if not tzoffset == 0:
            tzoffset = list(tzoffset)
            tzoffset = str(tzoffset[0]+tzoffset[1]+tzoffset[2])
            tzoffset = int(tzoffset)
            hour += -(tzoffset+1)
        if hour > 24:
            hour += -24
            day += 1
        if day == 29 and month == 2:
            day += -28
            month += 1
        elif day == 31 and (month == 4 or month == 6 or month == 9 or month == 11):
            day += -30
            month += 1
        elif day == 32 and (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10):
            day += -31
            month += 1
        elif day == 32 and month == 12:
            day += -31
            month = 1

        await self.bot.say(str(hour))

def check_folders():
    if not os.path.exists("data/irlbetting"):
        print("Creating data/irlbetting floder...")
        os.makedirs("data/irlbetting")

def check_files():
    system = {"Events": {"1": {"Name": "",
                               "CurrentUsers": 0,
                               "Multiplier": 1,
                               "Users": { "1": {"Bet": 0,
                                                "ID": "",
                                                "Choice": ""},
                                        },
                               "Date": {"Hour": 0,
                                        "Day": 0,
                                        "Month": 0},
                               "Outcomes": {"1": ""}},
                        "CurrentEvents": 0}}

    f = "data/irlbetting/irlbetting.json"
    if not dataIO.is_valid_json(f):
        print("Creating defualt irlbetting.json...")
        dataIO.save_json(f, system)

def setup(bot):
    check_folders()
    check_files()
    n = EventBets(bot)
    bot.add_cog(n)
<commit_msg>Fix sytnax<commit_after>import discord
from discord.ext import commands
from discord.permissions import Permissions
from .utils import checks
import asyncio
from .utils.dataIO import dataIO
import os
import time

client = discord.Client()

class EventBets:
    """Create an event which people can bet on"""

    def __init__(self, bot):
        self.bot = bot
        self.file_path = "data/irlbetting/irlbetting.json"
        self.json_data = dataIO.load_json(self.file_path)

    @commands.command(pass_context=True, aliases=["ce", "eventcreate"])
    @checks.admin_or_permissions(manage_server=True)
    @client.event
    async def createevent(self, ctx):
        """Create event"""

        #Your code will go here
        user = ctx.message.author
        await self.bot.say("What is the name of the event?")
        eventname = await self.bot.wait_for_message(timeout=30, author=user)
        if eventname is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        eventname = str(eventname.content)
        await self.bot.say("Event name: \n" + eventname)
        await self.bot.say("What are the possible outcomes of the event? You must have atleast 2. To stop inputting outcomes simply type `.`")
        a = 0
        outcome = []
        message = ""
        while not message == ".":
            await self.bot.say(str(a+1) + ": ")
            outcome.append(await self.bot.wait_for_message(timeout=30, author=user))
            if outcome[a] is None:
                if a == 3:
                    message = "."
                else:
                    await self.bot.say("You didn't enter anything. Event cancelled")
                    return
            else:
                outcome[a] = str(outcome[a].content)
                message = outcome[a]
            a += 1
        if len(outcome) < 2:
            await self.bot.say("You need atleast 2 outcomes. Event cancelled")
            return
        await self.bot.say("Outcomes: ")
        i = 0
        while i < a-1:
            await self.bot.say(outcome[i])
            i += 1
        await self.bot.say("What should the payout multiplier be?")
        payoutM = await self.bot.wait_for_message(timeout=30, author=user)
        if payoutM is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            payoutM = int(payoutM.content)
        except ValueError:
            await self.bot.say("You must enter a number. Event cancelled")
            return
        await self.bot.say("The multiplier has been set to: " + str(payoutM))
        await self.bot.say("In what month does the event happen? (Give as a number)")
        month = await self.bot.wait_for_message(timeout = 30, author = user)
        if month is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            month = int(month.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("What day of the month does the event happen? (Must be a number)")
        day = await self.bot.wait_for_message(timeout=30, author = user)
        if day is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            day = int(day.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("What hour of the day does the event happen? (Must be in 24 hour format IE: 9PM is 21)")
        hour = await self.bot.wait_for_message(timeout=30, author = user)
        if hour is None:
            await self.bot.say("You didn't enter anything. Event cancelled")
            return
        try:
            hour = int(hour.content)
        except ValueError:
            await self.bot.say("You have to enter a number. Event cancelled")
            return
        await self.bot.say("The event happens at " + str(hour) + ":00 on " + str(month) + "/" + str(day))
        numberofcurrentevents = self.json_data["Events"]["CurrentEvents"]
        self.json_data["Events"][str(numberofcurrentevents+1)] = {}
        self.json_data["Events"][str(numberofcurrentevents+1)]["Name"] = eventname
        self.json_data["Events"][str(numberofcurrentevents+1)]["Multiplier"] = payoutM
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"] = {}
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Month"] = month
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Day"] = day
        self.json_data["Events"][str(numberofcurrentevents+1)]["Date"]["Hour"] = hour
        self.json_data["Events"][str(numberofcurrentevents+1)]["Users"] = {}
        self.json_data["Events"][str(numberofcurrentevents+1)]["CurrentUsers"] = 0
        self.json_data["Events"][str(numberofcurrentevents+1)]["Outcomes"] = {}
        c = 0
        while c < len(outcome)-1:
            self.json_data["Events"][str(numberofcurrentevents+1)]["Outcomes"][str(c+1)] = outcome[c]
            c += 1
        self.json_data["Events"]["CurrentEvents"] = self.json_data["Events"]["CurrentEvents"]+1
        dataIO.save_json(self.file_path, self.json_data)

    @commands.command(pass_context=True, aliases=["be", "eventbet"])
    @client.event
    async def betevent(self, ctx):
        """Bet on event"""

        user = ctx.message.author
        bank = self.bot.get_cog('Economy').bank
        if not bank.account_exists(user):
            await self.bot.say("You don't have a bank account so you can't bet on events. Do `*bank register` to make an account")
            return
        numberofcurrentevents = self.json_data["Events"]["CurrentEvents"]
        a = 1
        if numberofcurrentevents == 0:
            await self.bot.say("There are no events running right now, sorry")
            return
        await self.bot.say("Which event would you like to bet on?: ")
        while a <= numberofcurrentevents:
            await self.bot.say(str(a) + ": " + self.json_data["Events"][str(a)]["Name"])
            a += 1
        await self.bot.say("Enter the number of the event")
        event = await self.bot.wait_for_message(timeout = 30, author = user)
        if event is None:
            await self.bot.say("You didn't enter anything. Bet cancelled")
            return
        try:
            event = int(event.content)
        except ValueError:
            await self.bot.say("You need to enter a number. Bet cancelled")
            return
        if event > numberofcurrentevents:
            await self.bot.say("That is not a valid event number. Bet cancelled")
            return
        i = 1
        while i <= self.json_data["Events"][str(event)]["CurrentUsers"]:
            print(i)
            if user == discord.utils.get(ctx.message.server.members, id=self.json_data["Events"][str(event)]["Users"][str(i)]["ID"]):
                await self.bot.say("You cannot bet on an event more than once. Bet cancelled")
                return
            i += 1
        await self.bot.say("You have picked: " + self.json_data["Events"][str(event)]["Name"] + ". \nThe outcomes for this event are:")
        d = 1
        while d <= len(self.json_data["Events"][str(event)]["Outcomes"]):
            await self.bot.say(str(d) + ": " + self.json_data["Events"][str(event)]["Outcomes"][str(d)])
            d += 1
        await self.bot.say("Enter the number of the outcome you would like to bet on: ")
        outcome = await self.bot.wait_for_message(timeout = 30, author = user)
        if outcome is None:
            await self.bot.say("You didn't enter anything. Bet cancelled")
            return
        try:
            outcome = int(outcome.content)
        except ValueError:
            await self.bot.say("You need to enter a number. Bet cancelled")
            return
        await self.bot.say("You have picked: " + self.json_data["Events"][str(event)]["Outcomes"][str(outcome)] + ". You have $" + str(bank.get_balance(user)) + ". How much would you like to bet?")
        bet = await self.bot.wait_for_message(timeout = 30, author = user)
        if bet is None:
            await self.bot.say("You didn't enter anything. Bet cancelled")
            return
        try:
            bet = int(bet.content)
        except ValueError:
            await self.bot.say("You need to enter a number. Bet cancelled")
            return
        if bet > bank.get_balance(user):
            await self.bot.say("You don't have enough money. Bet cancelled")
            return
        bank.withdraw_credits(user, bet)
        numberofcurrentusers = self.json_data["Events"][str(event)]["CurrentUsers"]
        self.json_data["Events"][str(event)]["Users"][str(numberofcurrentusers+1)] = {}
        self.json_data["Events"][str(event)]["Users"][str(numberofcurrentusers+1)]["ID"] = str(user.id)
        self.json_data["Events"][str(event)]["Users"][str(numberofcurrentusers+1)]["Bet"] = bet
        self.json_data["Events"][str(event)]["Users"][str(numberofcurrentusers+1)]["Choice"] = outcome
        self.json_data["Events"][str(event)]["CurrentUsers"] = self.json_data["Events"][str(event)]["CurrentUsers"]+1
        dataIO.save_json(self.file_path, self.json_data)
        await self.bot.say("You have placed $" + str(bet) + " on " + str(self.json_data["Events"][str(event)]["Users"][str(numberofcurrentusers+1)]["Choice"]))

    @commands.command(pass_context=True, aliases=["fe", "finishresults"])
    @checks.admin_or_permissions(manage_server=True)
    @client.event
    async def finishevent(self, ctx):
        """Finish event"""

        month = time.strftime('%m')
        day = time.strftime('%d')
        hour = time.strftime('%H')
        tzoffset = time.strftime('%z')
        month = int(month)
        day = int(day)
        hour = int(hour)
        if not tzoffset == 0:
            tzoffset = list(tzoffset)
            tzoffset = str(tzoffset[0]+tzoffset[1]+tzoffset[2])
            tzoffset = int(tzoffset)
            hour += -(tzoffset+1)
        if hour > 24:
            hour += -24
            day += 1
        if day == 29 and month == 2:
            day += -28
            month += 1
        elif day == 31 and (month == 4 or month == 6 or month == 9 or month == 11):
            day += -30
            month += 1
        elif day == 32 and (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10):
            day += -31
            month += 1
        elif day == 32 and month == 12:
            day += -31
            month = 1

        await self.bot.say(str(hour))

def check_folders():
    if not os.path.exists("data/irlbetting"):
        print("Creating data/irlbetting floder...")
        os.makedirs("data/irlbetting")

def check_files():
    system = {"Events": {"1": {"Name": "",
                               "CurrentUsers": 0,
                               "Multiplier": 1,
                               "Users": { "1": {"Bet": 0,
                                                "ID": "",
                                                "Choice": ""},
                                        },
                               "Date": {"Hour": 0,
                                        "Day": 0,
                                        "Month": 0},
                               "Outcomes": {"1": ""}},
                        "CurrentEvents": 0}}

    f = "data/irlbetting/irlbetting.json"
    if not dataIO.is_valid_json(f):
        print("Creating defualt irlbetting.json...")
        dataIO.save_json(f, system)

def setup(bot):
    check_folders()
    check_files()
    n = EventBets(bot)
    bot.add_cog(n)
<|endoftext|>