summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <elena.valhalla@gmail.com>2011-06-10 00:08:24 +0200
committerElena ``of Valhalla'' Grandi <elena.valhalla@gmail.com>2011-06-10 00:08:24 +0200
commita5e97e3f0cf2466086f31046d47deb32be1685b9 (patch)
tree7b47a6d4a3008eed2b77519084cdaea9965fe9c6
parente7d05b331b7dbd2a7041ee6e4f17c226d125660d (diff)
namegen.py: use json instead of yaml to save stats.
yaml is slow and json is faster and available in the standard library.
-rwxr-xr-xnamegen.py26
1 files changed, 9 insertions, 17 deletions
diff --git a/namegen.py b/namegen.py
index 194208f..baed954 100755
--- a/namegen.py
+++ b/namegen.py
@@ -43,16 +43,10 @@ LICENSE
"""
+import json
import sys
import random
-try:
- import yaml
- skip_yaml = False
-except ImportError:
- sys.stderr.write("Could not find module YAML: will not be able to save / load statistics\n")
- skip_yaml = True
-
class Stats:
def __init__(self,context):
@@ -73,6 +67,7 @@ class Stats:
"""Calculate stats from a list of names in a file-like object."""
for name in fp.readlines():
+ name = name.decode('utf-8')
for i in xrange(min(self.context,len(name))):
self._increment_stats(name[0:i],name[i])
for i in xrange(self.context,len(name)):
@@ -80,17 +75,11 @@ class Stats:
def load_stats(self,fp):
"""Load stats from a yaml in a string or file-like object"""
- if skip_yaml:
- return False
- self.stats = yaml.safe_load(fp)
+ self.stats = json.load(fp)
def save_stats(self,fp):
"""Save stats in yaml to a file-like object"""
- if skip_yaml:
- return False
- yaml.safe_dump(self.stats,fp)
- return True
-
+ json.dump(self.stats,fp)
def generate_name(self):
"""Generate a name following the stats"""
@@ -110,8 +99,11 @@ class Stats:
def main():
mystats = Stats(3)
- fp = open('data/nomif2.txt')
- mystats.read_names(fp)
+ #fp = open('data/nomif2.txt')
+ #mystats.read_names(fp)
+ #fp.close()
+ fp = open('it_female.stats')
+ mystats.load_stats(fp)
fp.close()
print mystats.generate_name(),