コード例 #1
0
ファイル: statextractor.py プロジェクト: rforge/sigil
 def extractStatistics(self,cqpf):
     featDict = {}
     wordList = cqpf.getColumn(0)
     posList = cqpf.getColumn(1)
     lemmaList = cqpf.getColumn(2) # use lemmatized forms as tokens
     
     sentenceLengths = [end-start for (start,end,arg) in cqpf.getAnnotations("s")]
     featDict.update(utils.getStats("sent_length", sentenceLengths))
     
     wordLengths = []
     for i in range(len(wordList)):
         if posList[i] not in ["PUN", "PUL", "PUR", "PUQ"]:
             wordLengths.append(len(wordList[i]))
     featDict.update(utils.getStats("word_length", wordLengths))
     longWordLengths = [x for x in wordLengths if x > 8]
     featDict["long_words"] = len(longWordLengths) / float(len(wordLengths))
     featDict.update(utils.getStats("long_word_length", longWordLengths))
     
     
     
     
     tokens = len(lemmaList)
     types = len(frozenset(lemmaList))
     
     return featDict
コード例 #2
0
	def testPreprocessedData(self):
		x_train, y_train, x_control_train, x_control_test, x_test, y_test = ut.getData()
		#checkNormalFit(x_train, y_train, x_control_train)

		for i in range(1,11):
			try : 
				tau = i/10.0
				print("Tau : ", tau)
				y_res = self.processGivenData(tau, x_train, y_train, x_control_train, x_test, y_test, x_control_test, [], [])
				ut.getStats(y_test, y_res, x_control_test)
				print("\n")
			except Exception as e:
				logging.exception(str(tau) + " failed\n" + str(e))
コード例 #3
0
	def testSyntheticData(self):
		#A,S,F = [],[],[]
		x_train, y_train, x_control_train, x_control_test, x_test, y_test = ut.getData()
		dist_params, dist_params_train =  ut.getDistribution(x_train, y_train, x_control_train)

		mean, cov, meanT, covT = dist_params["mean"], dist_params["cov"], dist_params_train["mean"], dist_params_train["cov"]
		#print(mean)
		meanN = [0] * len(mean)
		covN = np.identity(len(mean))

		#clf = GaussianMixture(n_components=2, covariance_type='full')
		means = [mean, meanN]
		covariances = [cov, covN]
		lw = float(sys.argv[2])
		weights = [1-lw, lw]

		#for i in range(0,4):
		LR, LE = len(y_train), len(y_test)
		train, test = [],[]
		for i in range(0, LR):
			j = np.random.choice([0,1], p=weights)
			seed = np.random.randint(10)
			train.append(multivariate_normal(means[j], covariances[j], allow_singular=1).rvs(size=1, random_state=seed))
		for i in range(0, LE):
			j = np.random.choice([0,1], p=weights)
			seed = np.random.randint(10)
			test.append(multivariate_normal(means[j], covariances[j], allow_singular=1).rvs(size=1, random_state=seed))

		x_train, y_train, x_control_train = [], [], []
		for t in train:
			x_train.append(t[:-2])
			if t[len(t)-2] < 0:
				y_train.append(-1)
			else:
				y_train.append(1)
			#y_train.append(t[len(t)-2])
			if t[len(t)-1] < 0.5:
				x_control_train.append(0)
			else:
				x_control_train.append(1)

		x_control_test, x_test, y_test = [], [], []
		for t in test:
			x_test.append(t[:-2])
			if t[len(t)-2] < 0:
				y_test.append(-1)
			else:
				y_test.append(1)
			if t[len(t)-1] < 0.5:
				x_control_test.append(0)
			else:
				x_control_test.append(1)

		#print(x_train, y_train, x_control_train)
		y_res = self.processGivenData(0.9, x_train, y_train, x_control_train, x_test, y_test, x_control_test, dist_params, dist_params_train)
		acc, sr, fdr = ut.getStats(y_test, y_res, x_control_test)
		print("Acc: ", acc, " SR: ", sr, " FDR: ", fdr)
コード例 #4
0
 def process(self,file):
     feats = {}
     Extractor.process(self,file)
     ir = InputReader(file)
     ir.read()
     cqpf = CQPFormat(ir.getText())
     lengths = [end-start for (start,end,arg) in cqpf.getAnnotations("s")]
     print self.__featureNames
     feats = utils.getStats("SENT_LENGTH", lengths)
     return ir.getID(),feats
コード例 #5
0
ファイル: main.py プロジェクト: Sheyin/Lurky
async def on_message(message):
    # Check contents of message
    if users.findUserAlias(str(
            message.author)) == "teague" and "lurkey" in str(
                message.content).lower():
        response = teague.chat(message.content)
        await client.send_message(message.channel, response)
    if message.content.startswith('!quote '):
        quote.recordQuote(str(message.author), str(message.content)[7:])
        utils.updateStats("!quote", str(message.author))
        await client.send_message(message.channel, 'Quote has been recorded.')
    if message.content.startswith('!getquote'):
        quoteDict, success = quote.retrieveQuote(str(message.content),
                                                 str(message.author))
        utils.updateStats("!getquote", str(message.author))
        if quoteDict == "empty":
            await client.send_message(
                message.channel,
                'Sorry, there are no quotes recorded.  Add some now using !getquote (userID/date/quote as copy-pasted from Discord)!'
            )
        elif not success:
            await client.send_message(
                message.channel,
                'No quotes by that name found.  Here is a random quote: "' +
                quoteDict['text'] + ' -- ' + quoteDict['name'] +
                ', (quoted by ' + quoteDict['author'] + '), ' +
                quoteDict['date'])
        else:
            await client.send_message(
                message.channel, 'Quote: "' + quoteDict['text'] + ' -- ' +
                quoteDict['name'] + ', (quoted by ' + quoteDict['author'] +
                '), ' + quoteDict['date'])

    if message.content.startswith('!test') or message.content.startswith(
            'test'):
        utils.updateStats("!test", str(message.author))
        response = chatmisc.test(message.author)
        await client.send_message(message.channel, response)

    if message.content.startswith('!tryid'):
        await client.send_message(message.channel,
                                  'Testing <@' + str(message.author.id) + '>')

    if message.content.startswith('!slap'):
        response = chatmisc.slap(message.content.lower(), str(message.author))
        utils.updateStats("!slap", str(message.author))
        await client.send_message(message.channel, response)

    if message.content.startswith('!fish'):
        response = chatmisc.fish(message.content.lower(), str(message.author))
        utils.updateStats("!fish", str(message.author))
        await client.send_message(message.channel, response)

    if message.content.startswith('!stats'):
        response = utils.getStats(message.content.lower(), str(message.author))
        await client.send_message(message.channel, response)

    if message.content.startswith('!help'):
        response = chatmisc.help(message.content.lower())
        await client.send_message(message.channel, response)

    if message.content.startswith('!alert') or message.content.startswith(
            '!fortnite'):
        response = getFortniteAlerts()
        await client.send_message(message.channel, response)

    if message.content.startswith('Lurky?'):
        await client.send_message(message.channel, 'Yes?')

    # So it doesn't respond to itself
    elif message.channel.is_private or message.author.id != message.server.me.id:
        # Logging just because
        try:
            with open("log.txt", "a") as file:
                file.write(
                    "Author: " + str(message.author) + " ID:" +
                    str(message.author.id) + " Date: " +
                    datetime.datetime.today().strftime("%d/%m/%y %H:%M") +
                    "\n")
                file.write("Message: " + str(message.content) + "\n")
        except UnicodeEncodeError:
            with open("log.txt", "a") as file:
                file.write(
                    "Author: " + str(message.author) + " ID:" +
                    str(message.author.id) + " Date: " +
                    datetime.datetime.today().strftime("%d/%m/%y %H:%M") +
                    "\n")
                file.write(
                    "Message: " +
                    "Some emote was used that produces a unicode error." +
                    "\n")

        if "surprise" in message.content.lower():
            if message.author.voice_channel:
                if not client.is_voice_connected(message.server):
                    voice = await client.join_voice_channel(
                        message.author.voice_channel)
                    player = voice.create_ffmpeg_player(
                        'sound/GiveYouUpIntro.mp3')
                    player.start()
                    print("Here's a surprise for " + str(message.author)[:-5] +
                          "!")
                else:
                    voice = await client.is_voice_connected(
                        message.author.voice_channel)
                    player = voice.create_ffmpeg_player(
                        'sound/GiveYouUpIntro.mp3')
                    player.start()
                    #else:
                    print("I might be in the wrong channel.")
                    print(
                        str(message.author)[:-5] + " is in " +
                        str(message.author.voice_channel) + " and server " +
                        str(message.server) + ".")
                    #await client.move_to(message.author.voice_channel)
                #try:
                #player = voice.create_ffmpeg_player('sound/GiveYouUpIntro.mp3')
                #player.start()
                #print("Here's a surprise for " + str(message.author)[:-5] + "!")
                '''except:
					print("Something failed during voice test.")
					print("Is Opus loaded?: " + str(discord.opus.is_loaded()))
					# Need a custom error handler if you want to display the error
					pass
					'''
            else:
                await client.send_message(
                    message.channel,
                    'I only give surprises to people in voice chat!')

        # Listening for currency conversion, particularly jpy -> usd.
        elif message.content.startswith('!convert ') or any(
                char.isdigit() for char in message.content):
            amount = identifyCurrency(message.content.lower())
            if amount:
                await client.send_message(message.channel,
                                          convertCurrency(amount, 'JPY'))

        # This seems to throw errors on a regular basis.  Returns "video does not exist"
        elif message.content.startswith('!play '):
            # Parse url first and see if it is valid
            splitString = message.content.lower().split(' ')
            if len(splitString) == 1:
                await client.send_message(
                    message.channel,
                    'What do you want to play?  Enter "!play youtube-url-here"'
                )
            else:
                if splitString[1][0:4] == 'http':
                    url = splitString[1]
                    if message.author.voice_channel:
                        try:
                            # Probably want to add a case for "if already joined voice channel" or else throws error
                            voice = await client.join_voice_channel(
                                message.author.voice_channel)
                            # Test - from https://github.com/Rapptz/discord.py/issues/315
                            beforeArgs = "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"
                            #player = await voice.create_ytdl_player(url)
                            player = await voice.create_ytdl_player(
                                url,
                                ytdl_options="--ignore-errors",
                                before_options=beforeArgs)
                            player.start()
                            print("Successful?")

                        except:
                            print("Something failed during voice test")
                            print("Is Opus loaded?: " +
                                  str(discord.opus.is_loaded()))
                            print("Url tried to play: " + url)
                            pass

                    else:
                        await client.send_message(message.channel,
                                                  'No surprise for you!')
                else:
                    await client.send_message(
                        message.channel, "Error: " + str(splitString[1]) +
                        " is not a valid youtube url.")

        response = chatmisc.quickMessageResponse(message.content.lower(),
                                                 message.author)
        if response:
            await client.send_message(message.channel, response)

    else:
        # Not needed - probably only something Lurky says.
        print("Lurky says: " + message.content)
コード例 #6
0
 def __init__(self, owner):
     self.owner = owner
     [self.score, self.mpp] = utils.getStats(owner)
     self.salt = -1
     self.result = 2
コード例 #7
0
ファイル: generate.py プロジェクト: dangoldin/connect4bot
def eval_func(chromosome):
    try:
        old_best_comp = compile(pickle.load(open('best.c4', 'rb')), "<string>", "eval")
    except:
        old_best_comp = None
    code_comp = chromosome.getCompiledCode()

    win = lose = tie = 0.0
    for rnd in range(ROUNDS):
        c = c4(rows=ROWS,cols=COLS)
        if rnd % 2 == 0:
            other_player = dumb.url(1,'http://dl.no.de/ai/twostep/move')
        else:
            other_player = dumb.url(2,'http://dl.no.de/ai/twostep/move')
        winner = 0
        turns = 0
        total_turns = c.rows * c.cols
        for i in range(total_turns):
            winner = c.isWinner()
            if winner <> 0:
                break
            board = c.getBoard(is_json=False)
            player = (i % 2) + 1
            # Train if we're not the genetic player
            if other_player.getPlayer() == player:
                json_state = json.dumps( { "rows":ROWS,
                                           "cols":COLS,
                                           "board":board,
                                           "currentTurn":player,
                                           "moveNumber":i} )
                column = other_player.getMoveJSON(json_state)
            else: # Otherwise let's eval and see what we get
                col_scores = []
                for col in utils.getFreeCols(board):
                    const, aps, apo, nlts, nlto, nr = utils.getStats(board,player,col)
                    score = eval(code_comp)
                    col_scores.append((score, col))
                random.shuffle(col_scores)
                sc = sorted(col_scores, key=itemgetter(0), reverse=True)
                #print 'Col Scores', str(sc)
                column = sc[0][1]
            c.move(player, column)            
            # This is to compete against self, to do later
            #if False: #old_best_comp is not None:
            #    col_scores = []
            #    for col in utils.getFreeCols(board):
            #        aps, apo, nlts, nlto, nr = utils.getStats(board,player,col)
            #        score = eval(old_best_comp)
            #        col_scores.append((score, col))
            #    random.shuffle(col_scores)
            #    sc = sorted(col_scores, key=itemgetter(0), reverse=True)
            #    #print 'Col Scores', str(sc)
            #    column = sc[0][1]
            turns += 1

        if winner == 0:
            tie += 1.0
        elif winner == other_player.getPlayer():
            lose += 1.0
        else:
            win += 1.0
    
    return (win + tie * 0.5)/ROUNDS
コード例 #8
0
ファイル: test_sample.py プロジェクト: n-kb/weathercontext
def test_getStats():
    media_id = utils.getStats("Berlin")
    assert (type(int(media_id)) == int)