Skip to content

Commit

Permalink
*** empty log message ***
Browse files Browse the repository at this point in the history
  • Loading branch information
chimrod committed Sep 8, 2006
0 parents commit aed8e9a
Show file tree
Hide file tree
Showing 8 changed files with 2,168 additions and 0 deletions.
117 changes: 117 additions & 0 deletions cfgfile.py
@@ -0,0 +1,117 @@
#
# PyBorg: The python AI bot.
#
# Copyright (c) 2000, 2006 Tom Morton, Sebastien Dailly
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
import string

def load_config(filename):
"""
Load a config file returning dictionary of variables.
"""
try:
f = open(filename, "r")
except IOError, e:
return None

stuff = {}
line = 0

while 1:
line = line + 1
s = f.readline()
if s=="":
break
if s[0]=="#":
continue

#read if the string is above multiple lines
while s.rfind("\\") > -1:
s = s[:s.rfind("\\")] + f.readline()
line = line + 1

s = string.split(s, "=")
if len(s) != 2:
print "Malformed line in %s line %d" % (filename, line)
print s
continue
stuff[string.strip(s[0])] = eval(string.strip(string.join(s[1:], "=")))
return stuff

def save_config(filename, fields):
"""
fields should be a dictionary. Keys as names of
variables containing tuple (string comment, value).
"""
f = open(filename, "w")

# write the values with comments. this is a silly comment
for key in fields.keys():
f.write("# "+fields[key][0]+"\n")
s = repr(fields[key][1])
f.write(key+"\t= ")
if len(s) > 80:
cut_string = ""
while len(s) > 80:
position = s.find(",",75)+1
cut_string = cut_string + s[:position] + "\\\n\t\t"
s = s[position:]
s = cut_string + s
f.write(s+"\n")

f.close()


class cfgset:
def load(self, filename, defaults):
"""
Defaults should be key=variable name, value=
tuple of (comment, default value)
"""
self._defaults = defaults
self._filename = filename

for i in defaults.keys():
self.__dict__[i] = defaults[i][1]

# try to laad saved ones
vars = load_config(filename)
if vars == None:
# none found. this is new
self.save()
return
for i in vars.keys():
self.__dict__[i] = vars[i]

def save(self):
"""
Save borg settings
"""
keys = {}
for i in self.__dict__.keys():
# reserved
if i == "_defaults" or i == "_filename":
continue
if self._defaults.has_key(i):
comment = self._defaults[i][0]
else:
comment = ""
keys[i] = (comment, self.__dict__[i])
# save to config file
save_config(self._filename, keys)

83 changes: 83 additions & 0 deletions convert.py
@@ -0,0 +1,83 @@
#!/usr/bin/env python
#
# Use to convert pyborg 0.9.10 and 0.9.11 dictionaries to the
# version 1.0.0+ format.
#
# Copyright (c) 2000, 2006 Tom Morton, Sebastien Dailly
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
import sys
import marshal
import struct
import string

# Read the dictionary
print "Reading dictionary stage 1..."

try:
f = open("lines.dat", "r")
s = f.read()
f.close()
lines = marshal.loads(s)
del s
except (EOFError, IOError), e:
print "Error reading dictionary."
sys.exit()

print "working..."
for x in lines.keys():
# clean up whitespace mess
line = lines[x]
words = string.split(line)
lines[x] = string.join(words, " ")

print "Saving Dictionary..."

f = open("lines.dat", "w")
s = marshal.dumps(lines)
f.write(s)
f.close()

# Read the dictionary
print "Reading dictionary stage 2..."

try:
f = open("words.dat", "r")
s = f.read()
f.close()
words = marshal.loads(s)
del s
except (EOFError, IOError), e:
print "Error reading dictionary."
sys.exit()

print "working..."
for key in words.keys():
# marshallise it:
y = []
for i in words[key]:
y.append(struct.pack("iH", i[0], i[1]))
words[key] = y

print "Saving Dictionary..."

f = open("words.dat", "w")
s = marshal.dumps(words)
f.write(s)
f.close()
print "Done."

85 changes: 85 additions & 0 deletions convert2.py
@@ -0,0 +1,85 @@
#!/usr/bin/env python
#
# PyBorg: The python AI bot.
#
#
# Use to convert pyborg 1.0.6 dictionaries to the
# version 1.1.0+ format.
#
# Copyright (c) 2006 Sebastien Dailly
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
import sys
import marshal
import struct
import string

print "Reading dictionary..."
try:
f = open("words.dat", "rb")
s = f.read()
f.close()
self.words = marshal.loads(s)
del s
f = open("lines.dat", "rb")
s = f.read()
f.close()
self.lines = marshal.loads(s)
del s
except (EOFError, IOError), e:
pass


#change the contexts here !
compteur = 0
for x in self.lines.keys():
self.lines[x]=[self.lines[x],1]
compteur = 1
if compteur != 0:
print "Contexts update done"

print "Writing dictionary..."

zfile = zipfile.ZipFile('archive.zip','r')
for filename in zfile.namelist():
data = zfile.read(filename)
file = open(filename, 'w+b')
file.write(data)
file.close()

f = open("words.dat", "wb")
s = marshal.dumps(self.words)
f.write(s)
f.close()
f = open("lines.dat", "wb")
s = marshal.dumps(self.lines)
f.write(s)
f.close()

#zip the files
f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED)
f.write('words.dat')
f.write('lines.dat')
f.close()

try:
os.remove('words.dat')
os.remove('lines.dat')
except (OSError, IOError), e:
print "could not remove the files"

f.close()
71 changes: 71 additions & 0 deletions pyborg-filein.py
@@ -0,0 +1,71 @@
#!/usr/bin/env python
#
# PyBorg ascii file input module
#
# Copyright (c) 2000, 2001 Tom Morton
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
import string
import sys

import pyborg

class ModFileIn:
"""
Module for file input. Learning from ASCII text files.
"""

# Command list for this module
commandlist = "FileIn Module Commands:\nNone"
commanddict = {}

def __init__(self, Borg, args):

f = open(args[1], "r")
buffer = f.read()
f.close()

print "I knew "+`Borg.settings.num_words`+" words ("+`len(Borg.lines)`+" lines) before reading "+sys.argv[1]
buffer = pyborg.filter_message(buffer)
# Learn from input
try:
Borg.learn(buffer)
except KeyboardInterrupt, e:
# Close database cleanly
print "Premature termination :-("
print "I know "+`Borg.settings.num_words`+" words ("+`len(Borg.lines)`+" lines) now."
del Borg

def shutdown(self):
pass

def start(self):
sys.exit()

def output(self, message, args):
pass

if __name__ == "__main__":
if len(sys.argv) < 2:
print "Specify a filename."
sys.exit()
# start the pyborg
my_pyborg = pyborg.pyborg()
ModFileIn(my_pyborg, sys.argv)
my_pyborg.save_all()
del my_pyborg

0 comments on commit aed8e9a

Please sign in to comment.