[Python.NET] Using a .Net DLL

Sean McKay mckay_sean at yahoo.com
Fri Mar 12 14:03:11 EST 2004


Hi,

I'm trying to come up with a port of a python project into C# (to speed it
up, and get experience with C#), and I'm having problems using the C# DLL
I've created.  I've added it to the GAC...  Here is the Python code, and
also the C# code..  Any help is appreciated!

Thanks,
Sean

>>> import CLR
>>> import CLR.nltk
>>> from CLR.nltk import Set
>>> dir(Set)
['__doc__', '__name__']
>>> from CLR.nltk.Set import Set
>>> dir(Set)
['Equals', 'Finalize', 'GetHashCode', 'GetType', 'MemberwiseClone',
'ReferenceEquals', 'ToString', '__and__', '__call__', '__class__',
'__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__',
'__eq__', '__getattribute__', '__getitem__', '__hash__', '__init__',
'__iter__', '__len__', '__module__', '__new__', '__or__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__str__',
'__sub__', 'contains', 'copy', 'count', 'dict', 'difference', 'elements',
'f_measure', 'intersection', 'precision', 'recall', 'union']
>>> a = Set([5,3,2,1])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'MethodObject' object is not callable

C# code:

namespace nltk
{
	namespace Set
	{
		public class Set
		{
			protected Dict dict ;

			public Set(Dict idict)
			{
				dict = idict;
			}

			public Set(params object[] list)
			{
				__init__(list);
			}

			private void __init__(params object[] list)
			{

				dict = new Dict();
				foreach (object name in list)
					dict[name.ToString()] = 1;
			}

			public string[] elements()
			{
				return dict.GetKeys;
			}

			public Set union(Set other)
			{
				Dict newDict = new Dict();
				foreach (string key in dict.Keys)
					newDict[key] = 1;

				foreach (string key in other.elements())
					newDict[key] = 1;

				return new Set(newDict);
			}

			public bool contains(object element)
			{
				return dict.Contains(element.ToString());
			}

			public bool contains(string element)
			{
				return dict.Contains(element);
			}

			public Set intersection(Set other)
			{
				Dict newDict = new Dict();
				foreach (string key in dict.Keys)
					if (other.contains(key))
						newDict[key] = 1;
				return new Set( newDict);
			}

			public Set difference(Set other)
			{
				Dict newDict = new Dict();
				foreach (string key in dict.Keys)
					if (!other.contains(key))
						newDict[key] = 1;
				return new Set(newDict);
			}

			public Set __and__(Set other)
			{
				return intersection(other);
			}

			public Set __or__(Set other)
			{
				return union(other);
			}

			public Set __sub__(Set other)
			{
				return difference(other);
			}

			public bool __contains__(string element)
			{
				return dict.Contains(element);
			}

			public Set copy()
			{
				Dict newDict = new Dict();
				foreach (string key in dict.Keys)
					newDict[key] = 1;
				return new Set(newDict);
			}

			public float precision(Set other)
			{
				int guessed = other.elements().Length;
				if (guessed==0) return 0;
				int found = intersection(other).elements().Length;
				return (float) found / guessed;

			}

			public float recall(Set other)
			{
				int toFind = elements().Length;
				if (toFind == 0) return 0;
				int found = intersection(other).elements().Length;
				return (float) found / toFind;
			}

			public float f_measure(Set other, float alpha)
			{
				float p = precision(other);
				float r = recall(other);
				if (p==0 || r==0) return 0;
				return 1/(alpha/p + (1-alpha)/r);
			}

			public float f_measure(Set other)
			{
				return f_measure(other, (float)0.5);
			}

			public string __repr__()
			{
				return string.Format("[{0}]",string.Join(",
",elements())).Replace("[","{").Replace("]","}");
			}

			public int __len__()
			{
				return dict.GetKeys.Length;
			}

			public int count()
			{
				return __len__();
			}

			public int __cmp__(Set other)
			{
				if (other.count() > count())
					return -1;
				else if (other.count() == count())
					return 0;
				else return 1;
			}
			
			public bool __eq__(Set other)
			{
				return (other == this);
			}

			public virtual int __hash__()
			{
				return dict.GetHashCode();
			}
		}




__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com



More information about the PythonDotNet mailing list