How to use the nlp.countermap.CounterMap function in nlp

To help you get started, we’ve selected a few nlp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nuance / python-nlp / nlp / countermap.py View on Github external
def test():
	one_all_spam = CounterMap()
	one_all_spam['xxx']['spam'] = 2

	assert(one_all_spam['xxx'].total_count() == 2)
	assert(one_all_spam['xxx'].arg_max() == 'spam')
	
	one_all_spam.normalize()

	assert(one_all_spam['xxx']['spam'] == 1.0)
	assert(one_all_spam['xxx']['ham'] == 0.0)
	assert(one_all_spam['cheese']['ham'] == 0.0)

	print "All spam: %s" % one_all_spam
	
	del(one_all_spam)

	half_spam = CounterMap()
github nuance / python-nlp / nlp / countermap.py View on Github external
	@classmethod
	def from_matrix(cls, keys, nparray):
		cnter_map = CounterMap()

		for i, key in enumerate(keys):
			for j, sub_key in enumerate(keys):
				cnter_map[key][subkey] = nparray[i][j]

		return cnter_map
github nuance / python-nlp / nlp / countermap.py View on Github external
def __mul__(self, other):
		if isinstance(other, (int, long, float)):
			return self.scale(other)

		ret = CounterMap()

		for key, counter in self.iteritems():
			if key not in other: continue
			ret[key] = counter * other[key]

		return ret
github nuance / python-nlp / nlp / countermap.py View on Github external
def __add__(self, other):
		if isinstance(other, (int, long, float)):
			ret = CounterMap()
			for key, value in self.iteritems():
				ret[key] = value + other
				
			return ret

		ret = CounterMap()

		for (key, counter) in self.iteritems():
			if key in other:
				ret[key] = counter + other[key]
			else:
				ret[key] = copy(counter)

		for key in (set(other.iterkeys()) - set(self.iterkeys())):
			ret[key] = copy(other[key])
github nuance / python-nlp / nlp / countermap.py View on Github external
one_all_spam['xxx']['spam'] = 2

	assert(one_all_spam['xxx'].total_count() == 2)
	assert(one_all_spam['xxx'].arg_max() == 'spam')
	
	one_all_spam.normalize()

	assert(one_all_spam['xxx']['spam'] == 1.0)
	assert(one_all_spam['xxx']['ham'] == 0.0)
	assert(one_all_spam['cheese']['ham'] == 0.0)

	print "All spam: %s" % one_all_spam
	
	del(one_all_spam)

	half_spam = CounterMap()
	half_spam['xxx']['spam'] += 1
	half_spam['xxx']['ham'] += 1
	half_spam['cheese']['spam'] += 1
	half_spam['cheese']['ham'] += 1

	half_spam.normalize()

	assert(half_spam['xxx']['spam'] == 0.5)
	assert(half_spam['cheese']['spam'] == 0.5)

	print "Half spam: %s" % half_spam

	del(half_spam)
github nuance / python-nlp / nlp / countermap.py View on Github external
def scale(self, other):
		ret = CounterMap()

		for key, counter in self.iteritems():
			ret[key] = counter * other

		return ret
github nuance / python-nlp / nlp / countermap.py View on Github external
def __sub__(self, other):
		if isinstance(other, (int, long, float)):
			return self + (0-other)

		ret = CounterMap()

		for (key, counter) in self.iteritems():
			if key in other:
				ret[key] = counter - other[key]
			else:
				ret[key] = copy(counter)

		for key in (set(other.iterkeys()) - set(self.iterkeys())):
			ret[key] = type(other[key])() - other[key]

		return ret
github nuance / python-nlp / nlp / countermap.py View on Github external
def outer_product(a, b):
	# sort keys from both and return a countermap of the resulting
	# matrix
	outer = CounterMap(a.default * b.default)

	for a_key, a_value in a.iteritems():
		for b_key, b_value in b.iteritems():
			outer[a_key][b_key] = a_value * b_value

	return outer