本文整理汇总了Python中space.Space类的典型用法代码示例。如果您正苦于以下问题:Python Space类的具体用法?Python Space怎么用?Python Space使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Space类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_src_wrapper
def build_src_wrapper(self, source_file, test_wpairs):
"""
In the _source_ space, we only need to load vectors for the words in
test. Semantic spaces may contain additional words.
All words in the _target_ space are used as the search space
"""
source_words = set(test_wpairs.iterkeys())
if self.additional:
#read all the words in the space
lexicon = set(np.loadtxt(source_file, skiprows=1, dtype=str,
comments=None, usecols=(0,)).flatten())
#the max number of additional+test elements is bounded by the size
#of the lexicon
self.additional = min(self.additional, len(lexicon) - len(source_words))
random.seed(100)
logging.info("Sampling {} additional elements".format(self.additional))
# additional lexicon:
lexicon = random.sample(list(lexicon.difference(source_words)),
self.additional)
#load the source space
source_sp = Space.build(source_file,
lexicon=source_words.union(set(lexicon)),
max_rows=1000)
else:
source_sp = Space.build(source_file, lexicon=source_words,
max_rows=1000)
source_sp.normalize()
return source_sp
示例2: post_space_handler
def post_space_handler(environ, start_response):
"""
entry point for posting a new space name to TiddlyWeb
"""
space_name = environ['tiddlyweb.query']['name'][0]
space = Space(environ)
try:
space.create_space(space_name, environ['tiddlyweb.config']['space'])
except (RecipeExistsError, BagExistsError):
raise HTTP409('Space already Exists: %s' % space_name)
host = environ['tiddlyweb.config']['server_host']
if 'port' in host:
port = ':%s' % host['port']
else:
port = ''
recipe = Recipe('%s_public' % space_name)
new_space_uri = '%s/tiddlers.wiki' % recipe_url(environ, recipe)
start_response('201 Created', [
('Location', new_space_uri),
('Content-type', 'text/plain')])
return new_space_uri
示例3: testExecute
def testExecute(self):
from space import Space
from player import Player
from items.weapon import Weapon
from commands.drop_command import DropCommand
space = Space("Shire", "Home of the Hobbits.")
player = Player("Frodo", space)
dropCmd = DropCommand("drop", "Drops an object from inventory to space", player)
weapon = Weapon("Dagger", "A trusty blade", 2, 2, 2)
player.addToInventory(weapon)
player.equip(weapon)
#Asserts item in player inventory but not in space
self.assertFalse(space.containsItem(weapon), "Space should not have item but does.")
inventory = player.getInventory()
self.assertTrue(inventory.containsItem(weapon), "Inventory should have item but does not.")
#Assert item in space but not in player inventory and not in equipment
rawInputMock = MagicMock(return_value="Dagger")
with patch('commands.drop_command.raw_input', create=True, new=rawInputMock):
dropCmd.execute()
self.assertTrue(space.containsItemString("Dagger"), "Space should have item but does not.")
inventory = player.getInventory()
self.assertFalse(inventory.containsItem(weapon), "Inventory should not have item but does.")
equipped = player.getEquipped()
self.assertFalse(equipped.containsItem(weapon), "Equipment should not have item but does.")
示例4: __init__
def __init__(self):
self.name = 'World'
Space.__init__(self)
if farm_config.DEBUG_WIN:
debug_console.get()._print("World created.")
self.house = House(1, 1, HOUSE_GRAPHIC, self)
self.ship_box = Shipbox(4, 12, SHIP_BOX_GRAPHIC, self)
self.pond = Pond(GAME_WIN_SIZE_Y - 5,
GAME_WIN_SIZE_X - 16,
POND_GRAPHIC,
self
)
self.seed(Cave_Entrance, CAVE_GRAPHICS_DIR + 'entrance_external', 1)
self.seed(Tree, 'GRAPHICS/tree', NUMBER_TREES)
self.seed(Rock, 'GRAPHICS/rock', NUMBER_ROCKS)
self.seed(Bush, 'GRAPHICS/bush', NUMBER_BUSHES)
if farm_config.DEBUG_WIN:
debug_console.get()._print("World populated.")
self.sort_contents()
if farm_config.DEBUG_WIN:
debug_console.get()._print("World contents sorted.")
示例5: train_wrapper
def train_wrapper(seed_fn, source_fn, target_fn, reverse=False, mx_path=None,
train_size=5000):
logging.info("Training...")
seed_trans = read_dict(seed_fn, reverse=reverse)
#we only need to load the vectors for the words in the training data
#semantic spaces contain additional words
source_words = set(seed_trans.iterkeys())
target_words = set().union(*seed_trans.itervalues())
source_sp = Space.build(source_fn, lexicon=source_words)
source_sp.normalize()
target_sp = Space.build(target_fn, lexicon=target_words)
target_sp.normalize()
logging.info("Learning the translation matrix")
tm, used_for_train = train_tm(source_sp, target_sp, seed_trans, train_size)
mx_path = default_output_fn(mx_path, seed_fn, source_fn, target_fn,)
logging.info("Saving the translation matrix to {}".format(mx_path))
np.save('{}.npy'.format(mx_path), tm)
pickle.dump(used_for_train, open('{}.train_wds'.format(mx_path),
mode='w'))
return tm, used_for_train
示例6: test_on_off
def test_on_off(self):
space = Space('hello world')
test_fun = lambda: 'test'
space.on('test', test_fun)
self.assertIn('test', space._Space__events)
space.off('test', test_fun)
self.assertEqual(space._Space__events['test'], [])
示例7: train_translation_matrix
def train_translation_matrix(source_file, target_file, dict_file, out_file):
"""Trains a transltion matrix between the source and target languages, using the words in dict_file as anchor
points and writing the translation matrix to out_file
Note that the source language file and target language file must be in the word2vec C ASCII format
:param source_file: The name of the source language file
:param target_file: The name of the target language file
:param dict_file: The name of the file with the bilingual dictionary
:param out_file: The name of the file to write the translation matrix to
"""
log.info("Reading the training data")
train_data = read_dict(dict_file)
#we only need to load the vectors for the words in the training data
#semantic spaces contain additional words
source_words, target_words = zip(*train_data)
log.info("Reading: %s" % source_file)
source_sp = Space.build(source_file, set(source_words))
source_sp.normalize()
log.info("Reading: %s" % target_file)
target_sp = Space.build(target_file, set(target_words))
target_sp.normalize()
log.debug('Words in the source space: %s' % source_sp.row2id)
log.debug('Words in the target space: %s' % target_sp.row2id)
log.info("Learning the translation matrix")
log.info("Training data: %s" % str(train_data))
tm = train_tm(source_sp, target_sp, train_data)
log.info("Printing the translation matrix")
np.savetxt(out_file, tm)
示例8: create_room_elements
def create_room_elements(room_name):
room_space = Space({'tiddlyweb.store': store})
this_room = ROOM.replace('ROOMNAME', room_name)
this_room = json.loads(this_room)
room_space.create_space(this_room)
示例9: test_append
def test_append(self):
space = Space('hello world')
space.append('foo', 'bar')
space.set('foo2', 'bar')
space.append('foo', 'two')
self.assertEqual(space.get('foo'), 'bar')
self.assertEqual(space.length(), 4)
示例10: addproject
def addproject(args):
"""make a project space. <project_name>"""
if len(args) != 1:
print >> sys.stderr, ('usage: twanager addproject <project_name>')
#replace PROJECT_NAME with the actual name of the project
this_project = PROJECT.replace('PROJECT_NAME', args[0])
this_project = json.loads(this_project)
#create the space
project_space = Space({'tiddlyweb.store': get_store(config)})
project_space.create_space(this_project)
示例11: test_trigger
def test_trigger(self):
space = Space('hello world')
class Count():
def __init__(self):
self.count = 0
def incr(self):
self.count += 1
c = Count()
space.on('change', c.incr)
space.trigger('change')
self.assertEqual(c.count, 1)
示例12: eval_js_vm
def eval_js_vm(js):
a = ByteCodeGenerator(Code())
s = Space()
a.exe.space = s
s.exe = a.exe
d = pyjsparser.parse(js)
a.emit(d)
fill_space.fill_space(s, a)
# print a.exe.tape
a.exe.compile()
return a.exe.run(a.exe.space.GlobalObj)
示例13: build
def build(cls, core_space, **kwargs):
"""
Reads in data files and extracts the data to construct a semantic space.
If the data is read in dense format and no columns are provided,
the column indexing structures are set to empty.
Args:
data: file containing the counts
format: format on the input data file: one of sm/dm
rows: file containing the row elements. Optional, if not provided,
extracted from the data file.
cols: file containing the column elements
Returns:
A semantic space build from the input data files.
Raises:
ValueError: if one of data/format arguments is missing.
if cols is missing and format is "sm"
if the input columns provided are not consistent with
the shape of the matrix (for "dm" format)
"""
sp = Space.build(**kwargs)
mat = sp._cooccurrence_matrix
id2row = sp.id2row
row2id = sp.row2id
return PeripheralSpace(core_space, mat, id2row, row2id)
示例14: test_wrapper
def test_wrapper(self):
self.load_tr_mx()
logging.info('The denominator of precision {} OOV words'.format(
'includes' if self.args.coverage
else "doesn't include"))
test_wpairs = read_dict(self.args.seed_fn, reverse=self.args.reverse,
needed=1000 if self.args.coverage else -1,
exclude=self.exclude_from_test)
source_sp = self.build_src_wrapper(self.args.source_fn, test_wpairs)
target_sp = Space.build(self.args.target_fn)
target_sp.normalize()
test_wpairs, _ = get_invocab_trans(source_sp, target_sp,
test_wpairs, needed=1000)
"""
#turn test data into a dictionary (a word can have mutiple translation)
gold = collections.defaultdict(set, test_wpairs)
for sr, tg in test_wpairs:
gold[sr].add(tg)
"""
logging.info(
"Mapping all the elements loaded in the source space")
mapped_source_sp = apply_tm(source_sp, self.tr_mx)
if hasattr(self.args, 'mapped_vecs') and self.args.mapped_vecs:
logging.info("Printing mapped vectors: %s" % self.args.mapped_vecs)
np.savetxt("%s.vecs.txt" % self.args.mapped_vecs, mapped_source_sp.mat)
np.savetxt("%s.wds.txt" %
self.args.mapped_vecs, mapped_source_sp.id2word, fmt="%s")
return score(mapped_source_sp, target_sp, test_wpairs, self.additional)
示例15: main
def main():
from space import Space
import fill_space
from pyjsparser import parse
import json
a = ByteCodeGenerator(Code())
s = Space()
fill_space.fill_space(s, a)
a.exe.space = s
s.exe = a.exe
con = get_file_contents('internals/esprima.js')
d = parse(con+(''';JSON.stringify(exports.parse(%s), 4, 4)''' % json.dumps(con)))
# d = parse('''
# function x(n) {
# log(n)
# return x(n+1)
# }
# x(0)
# ''')
# var v = 333333;
# while (v) {
# v--
#
# }
a.emit(d)
print a.declared_vars
print a.exe.tape
print len(a.exe.tape)
a.exe.compile()
def log(this, args):
print args[0]
return 999
print a.exe.run(a.exe.space.GlobalObj)