Пример #1
0
 def test_this_and_that(self):
     self.assert_ (2 == len (self.fake_dict.keys ()))
     cat_hits = self.fake_dict[bag ("cat")]
     self.assert_ (2 == len (cat_hits))
     self.assert_ (cat_hits[0] == "cat")
     self.assert_ (cat_hits[1] == "tac")
     self.assert_ (1 == len (self.fake_dict[bag ("fred")]))
     self.assert_ (self.fake_dict[bag ("fred")][0] == "fred")
def jaccard_two_lists(from_list, to_list):
    # bag it
    from_bag = bag.bag(from_list)
    to_bag = bag.bag(to_list)

    # In the intersection (respectively union) of two multisets A and
    # B, the count of x is the min (respectively sum) of its counts in
    # A and B.)

    union_count = 0
    intersection_count = 0
    keys = Set()
    keys.update(from_bag.iterunique())
    keys.update(to_bag.iterunique())
    for thing in keys:
        union_count += max(to_bag[thing], from_bag[thing])
        intersection_count += min(to_bag[thing], from_bag[thing])

    # Jaccard's coefficient: size of intersection over size of union.
    if union_count == 0:
        return float(0)
    if intersection_count == 0:
        return float(0)
    return float(intersection_count) / float(union_count)
Пример #3
0
 def __init__(self, d, conn):
     self.name = d.name
     self.__conn = conn
     self.__state = constants.PLAYER_STATE_LOGIN
     conn.ply = self 
     self.bag = bag.bag(self)
     self.bag.add(constants.ItemBullet, 10000)
     self.dead = False
     self.pos = data.data(x = 1000, z = 1000)
     
     # data
     self.hero = "hero"
     self.weapon = constants.WeaponNormal
     self.run_distance = 0
     self.point = 0
Пример #4
0
    def __init__(self, cSpeed=0, cHeight=0, cMusic='a', ):
        self.speed = self.startSpeed = cSpeed # 0-9
        self.startHeight = cHeight # 0-5
        self.score = 0
        self.bag = bag.bag(7)
        self.lines = 0
        self.timer = stopwatch.Stopwatch()
        self.pause = False
        
        # generate height garbage

        self.field = field.Field()

        # populate blocks
        self.currPiece = piece.Piece(self.bag.grab())
        self.nextPiece = piece.Piece(self.bag.grab())
Пример #5
0
def snarf_dictionary_from_IO (I):
    print >> sys.stderr, "Snarfing", I
    hash_table = {}
    for w in re.findall (r'.+', I.read ()):
        w = string.lower (w)

        if not word_acceptable(w):
            continue

        key = bag(w)
        if hash_table.has_key (key):
            if (0 == hash_table[key].count (w)): # avoid duplicates
                hash_table[key].append (w)
        else:
            hash_table[key] = [w]

    print >> sys.stderr, "done"
    return hash_table
Пример #6
0
 def parse_reaction_formula_side(self, s):
     """ parse the side formula, e.g. '2 C00001 + C00002 + 3 C00003'
         return the set of CIDs, ignore stoichiometry
     """
     compound_bag = bag.bag()
     for member in s.split("+"):
         member = member.strip()
         if (member.find(' ') > -1):
             (amount, cid) = member.split()
         else:
             amount = 1
             cid = member
         
         try:
             compound_bag.add(cid, int(amount))
         except ValueError:
             raise KeggParseException("Non-specific reaction: " + s)
     
     return compound_bag
Пример #7
0
def snarf_dictionary_from_IO(I):
    print "Snarfing", I
    hash_table = {}
    for w in re.findall(r".+", I.read()):
        w = string.lower(w)
        if non_letter_re.search(w):
            continue
        if not long_enough_re.match(w):
            continue
        if not has_a_vowel_re.search(w):
            continue

        key = bag(w)
        if hash_table.has_key(key):
            if 0 == hash_table[key].count(w):  # avoid duplicates
                hash_table[key].append(w)
        else:
            hash_table[key] = [w]

    print "done"
    return hash_table
Пример #8
0
def snarf_dictionary_from_IO (I):
    print "Snarfing", I
    hash_table = {}
    for w in re.findall (r'.+', I.read ()):
        w = string.lower (w)
        if non_letter_re.search (w):
            continue
        if (not long_enough_re.match (w)):
            continue
        if (not has_a_vowel_re.search (w)):
            continue

        key = bag(w)
        if hash_table.has_key (key):
            if (0 == hash_table[key].count (w)): # avoid duplicates
                hash_table[key].append (w)
        else:
            hash_table[key] = [w]

    print "done"
    return hash_table
Пример #9
0
                      action="store",
                      type="string",
                      dest="dict_fn",
                      default=dict.default_dict_name,
                      metavar="FILE",
                      help="location of word list")

    (options, args) = parser.parse_args()

    if (0 == len(args)):
        parser.print_help ()
        sys.exit (0)

    dict_hash_table = dict.snarf_dictionary (options.dict_fn)

    the_phrase = bag (args[0])
    print >> sys.stderr, "Pruning dictionary.  Before:", len (dict_hash_table.keys ()), "bags ...",

    # Now convert the hash table to a list, longest entries first.  (This
    # isn't necessary, but it makes the more interesting anagrams appear
    # first.)

    # While we're at it, prune the list, too.  That _is_ necessary for the
    # program to finish before you grow old and die.


    the_dict_list = [[k, dict_hash_table[k]]
                     for k in dict_hash_table.keys ()
                     if (subtract_bags (the_phrase, k))]

    # Note that sorting entries "alphabetically" only makes partial sense,
Пример #10
0

def snarf_dictionary(fn):
    try:
        fh = open(hash_cache, "rb")
        rv = cPickle.load(fh)
        print "Reading cache", hash_cache, "instead of dictionary", fn
    except:
        fh = open(fn, "r")
        rv = snarf_dictionary_from_IO(fh)
        fh.close()
        fh = open(hash_cache, "wb")
        cPickle.dump(rv, fh, 2)

    fh.close()
    return rv


fake_input = "cat\ntac\nfred\n"
fake_dict = snarf_dictionary_from_IO(StringIO.StringIO(fake_input))

assert 2 == len(fake_dict.keys())
cat_hits = fake_dict[bag("cat")]
assert 2 == len(cat_hits)
assert cat_hits[0] == "cat"
assert cat_hits[1] == "tac"
assert 1 == len(fake_dict[bag("fred")])
assert fake_dict[bag("fred")][0] == "fred"

print __name__, "tests passed."
Пример #11
0
                      action="store",
                      type="string",
                      dest="dict_fn",
                      default="../words.utf8",
                      metavar="FILE",
                      help="location of word list")

    (options, args) = parser.parse_args()

    if (0 == len(args)):
        parser.print_help()
        sys.exit(0)

    dict_hash_table = snarf_dictionary(options.dict_fn)

    the_phrase = bag(args[0])
    print "Pruning dictionary.  Before:", len(
        dict_hash_table.keys()), "bags ...",

    # Now convert the hash table to a list, longest entries first.  (This
    # isn't necessary, but it makes the more interesting anagrams appear
    # first.)

    # While we're at it, prune the list, too.  That _is_ necessary for the
    # program to finish before you grow old and die.

    the_dict_list = [[k, dict_hash_table[k]] for k in dict_hash_table.keys()
                     if (subtract_bags(the_phrase, k))]

    the_dict_list.sort(biggest_first_then_alphabetically)
Пример #12
0
 def __init__(self, name):
     self.name=name
     self.startCash=2000
     self.startBagCapacity=80
     self.startDebt=0
     self.playersBag=bag.bag(self.name + "'s Bag ", self.startBagCapacity)
Пример #13
0
hash_cache = "hash.cache"

def snarf_dictionary (fn):
    try:
        fh = open (hash_cache, "rb")
        rv= cPickle.load (fh)
        print "Reading cache", hash_cache, "instead of dictionary", fn
    except:
        fh = open (fn, "r")
        rv = snarf_dictionary_from_IO (fh)
        fh.close ()
        fh = open (hash_cache, "wb")
        cPickle.dump (rv, fh, 2)

    fh.close ()
    return rv


fake_input = "cat\ntac\nfred\n"
fake_dict = snarf_dictionary_from_IO (StringIO.StringIO (fake_input))

assert (2 == len (fake_dict.keys ()))
cat_hits = fake_dict[bag ("cat")]
assert (2 == len (cat_hits))
assert (cat_hits[0] == "cat")
assert (cat_hits[1] == "tac")
assert (1 == len (fake_dict[bag ("fred")]))
assert (fake_dict[bag ("fred")][0] == "fred")

print __name__, "tests passed."