Created
January 21, 2017 08:59
-
-
Save KTakahiro1729/2d0f5e8d673dec64f6bb58145a1067a7 to your computer and use it in GitHub Desktop.
haniko.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime, re,collections | |
from pprint import pprint | |
logTemplate = namedTuple('template','chunkName logREString') | |
logDict ={ | |
'ログヘッダー'logTemplate('ログヘッダー',r'■ターン\d+ - \d+/\d+ \d+:\d+'), | |
logTemplate('農場整備',r'\(\d+,\d+\)で農場整備が行われました。'), | |
logTemplate('工場建設',r'\(\d+,\d+\)で工場建設が行われました。'), | |
logTemplate('資金繰り',r'資金繰りが行われました。'), | |
logTemplate('一括地ならし',r'\(\d+,\d+\)で一括地ならしが行われました。'), | |
logTemplate('被陸破',r'.+島が.+島\(\d+,\d+\)地点に向けて陸地破壊弾発射を行いましたが、\(\d+,\d+\)の海に落ちたので被害がありませんでした。'), | |
logTemplate('被陸破領域外':r'.+島が.+島\(\d+,\d+\)地点に向けて陸地破壊弾発射を行いましたが、領域外の海に落ちた模様です。', | |
'被陸破被害あり':r'.+島が.+島\(\d+,\d\)地点に向けて陸地破壊弾発射を行い、.+。' | |
} | |
class Logs(): | |
def __init__(self,logs,year = datetime.datetime.today().year): | |
def makeLogChunks(): | |
result = [] | |
oneLogChunk = [self.logs[0]] | |
i = 1 | |
while i < len(self.logs): | |
if re.fullmatch(logDict['ログヘッダー'],self.logs[i]): | |
result.append(LogChunk(oneLogChunk,year)) | |
oneLogChunk = [self.logs[i]] | |
i += 1 | |
continue | |
oneLogChunk.append(self.logs[i]) | |
i += 1 | |
return result | |
self.logs = logs | |
self.year = year | |
self.logChunks = makeLogChunks() | |
class LogChunk(): | |
def __init__(self,logChunk,year = datetime.datetime.today().year): | |
def extractTime(): | |
date = self.header.split()[2] | |
time = self.header.split()[3] | |
result = datetime.datetime( | |
year = year, | |
month = int(date.split('/')[0]), | |
day = int(date.split('/')[1]), | |
hour = int(time.split(':')[0]), | |
minute = int(time.split(':')[1]), | |
) | |
return result | |
def judgeType(): | |
for action in logDict: | |
if re.fullmatch(logDict[action],logChunk[1]): | |
return action | |
self.logChunk = logChunk | |
self.header = logChunk[0] | |
self.content = logChunk[1:] | |
self.datetime = extractTime() | |
self.turn = int(self.header.split()[0][4:]) | |
self.type = judgeType() | |
def readLog(mode ,fileName = ''): | |
logsFull = [] | |
groupDict= { | |
'於:.+島':4, #海戦戦闘ログ | |
'対戦終了':4 #道場対戦終了 | |
} | |
if mode == 'file': | |
txt = open(fileName, 'rt',encoding = 'utf-8') | |
while True: | |
line = txt.readline() | |
if not line: | |
break | |
for template in groupDict: | |
if re.match(template,line[:-1]): | |
withinLog = ''.join([txt.readline() for _ in range(groupDict[template]-1)]) | |
line = line + withinLog | |
logsFull.append(line[:-1]) | |
txt.close() | |
if mode == 'input': | |
pass | |
return logsFull | |
logsFull = Logs(readLog(mode = 'file',fileName = 'logs.txt')) | |
pprint([chunk.type for chunk in logsFull.logChunks]) | |
#print(logsFull.logChunks[0].content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment