Difference: UsingCSharpOnPatas (11 vs. 12)

Revision 122008-12-30 - brodbd

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Using C# on Patas

Line: 8 to 8
  C# programs operate in a fully sandboxed runtime environment which provides garbage-collection. As in Java, Python, and other high-level general-purpose languages, disposal of unused memory objects is tracked and managed by the system, relieving an enormous burden from the application programmer.
1. Sample Program
Changed:
<
<
 using System;  using System.Text;    static class MainClass  {     static void Main(String[] args)     {        Console.WriteLine("hello world");     }  }  
>
>
 using System;
 using System.Text;
 
 static class MainClass
 {
 	static void Main(String[] args)
 	{
 		Console.WriteLine("hello world");
 	}
 }
 
  To compile and run on patas:
Changed:
<
<
 gmcs hello.cs  mono hello.exe  
>
>
 gmcs hello.cs
 mono hello.exe
 
  The reason that this is a two-step process hints at the power of C# over interpreted languages; the first step "compiles" your source file into an intermediate byte-code called MSIL which is then processed by a runtime environment, called the Common Language Runtime, or CLR. This type of virtual instruction set is nothing new in computer science. But an innovation that Microsoft's CLR introduced was that this MSIL code is translated, on an as-needed basis, into actual native machine instructions for the target system. And it's retained in this optimal form as the program runs. This is called Just-in-time, or JIT compilation, and it means that your C# program runs with the performance of true native compilation.

In fact, you can even use mono to execute, on patas, an MSIL binary produced by Microsoft Visual Studio 2008! Just copy the .exe file (in binary mode) to patas and run it like so:

Changed:
<
<
 mono compiled_by_msvc.exe  
>
>
 mono compiled_by_msvc.exe
 
  To invoke a mono program from a CONDOR script, you should specify the full path to the mono executable:
Changed:
<
<
 universe   = vanilla  getenv      = true  executable   = /opt/mono/bin/mono  arguments   = "myprogram.exe -myarg1 -myarg2"  input      = mystdin.txt  output      = mystdout.txt  error      = mystderr.txt  log      = /tmp/myuwid.log  transfer_executable = false  queue  
>
>
 universe	= vanilla
 getenv		= true
 executable	= /opt/mono/bin/mono
 arguments	= "myprogram.exe -myarg1 -myarg2"
 input		= mystdin.txt
 output		= mystdout.txt
 error		= mystderr.txt
 log		= /tmp/myuwid.log
 transfer_executable = false
 queue
 
 
2. Documentation

Microsoft's detailed commercial-quality documentation on C# is available freely on the web. Of primary interest will be the extensive CLR (".NET Framework") class libraries, which provide a wide array of system services and data structures. The mono project also offers a set of documentation.

Line: 29 to 51
  The examples below highlight basic programming tasks which are relevant for computational linguists. All have been developed and tested with mono on patas.
3. String Manipulation
Changed:
<
<
 using System;  using System.Text;    static class MainClass  {     static void Main(String[] args)     {        String s = "1.\tThis is a string.";        String[] string_arr = s.Split('\t');          Char[] trim_chars = ".:;,".ToCharArray();        String ns = string_arr[0].Trim(trim_chars);        int i = Convert.ToInt32(ns);          Console.WriteLine(i);          foreach (String s2 in string_arr[1].Split())           Console.WriteLine(s2.Replace('s','z'));     }  }  
>
>
 using System;
 using System.Text;
 
 static class MainClass
 {
 	static void Main(String[] args)
 	{
 		String s = "1.\tThis is a string.";
 		String[] string_arr = s.Split('\t');
 
 		Char[] trim_chars = ".:;,".ToCharArray();
 		String ns = string_arr[0].Trim(trim_chars);
 		int i = Convert.ToInt32(ns);
 
 		Console.WriteLine(i);
 
 		foreach (String s2 in string_arr[1].Split())
 			Console.WriteLine(s2.Replace('s','z'));
 	}
 }
 
  Result:
Changed:
<
<
 1  Thiz  iz  a  ztring.  
>
>
 1
 Thiz
 iz
 a
 ztring.
 
  Internally, all strings in C# are Unicode, as is the Char data type. A rich set of functions is provided in the Class Library's 'Encoding' namespace for reading and writing the various 8-bit character sets as input and output.
Line: 40 to 87
  Strings can also be edited by converting them to an array of Char, as shown in the next example, "Reading and Writing Files." The example also shows how a String object can be created from an array of Char.
4. Reading and Writing Files
Changed:
<
<
 using System;  using System.IO;  using System.Linq;  using System.Text;    static class MainClass  {     static void Main(String[] args)     {        String my_filename = "the_file.txt";          String data = "Four score and seven years ago.";          // Write some data to the file        int i = 0;        using (FileStream fs = new FileStream(my_filename, FileMode.Create, FileAccess.Write, FileShare.None))          {           using (StreamWriter sr = new StreamWriter(fs, Encoding.GetEncoding(28591)))   // Latin-1           {              foreach (String s in data.Split())                 sr.WriteLine((++i).ToString() + ". " + new String(s.ToCharArray().Reverse  ().ToArray()));           }        }          // Read data from the file        using (FileStream fs = File.Open(my_filename, FileMode.Open, FileAccess.Read, FileShare.Read))        {           using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(28591)))           {              String s;              while (null != (s = sr.ReadLine()))                 Console.WriteLine(s);           }        }     }  }  
>
>
 using System;
 using System.IO;
 using System.Linq;
 using System.Text;
 
 static class MainClass
 {
 	static void Main(String[] args)
 	{
 		String my_filename = "the_file.txt";
 
 		String data = "Four score and seven years ago.";
 
 		// Write some data to the file
 		int i = 0;
 		using (FileStream fs = new FileStream(my_filename, FileMode.Create, FileAccess.Write, FileShare.None))  
 		{
 			using (StreamWriter sr = new StreamWriter(fs, Encoding.GetEncoding(28591)))	// Latin-1
 			{
 				foreach (String s in data.Split())
 					sr.WriteLine((++i).ToString() + ". " + new String(s.ToCharArray().Reverse  ().ToArray()));
 			}
 		}
 
 		// Read data from the file
 		using (FileStream fs = File.Open(my_filename, FileMode.Open, FileAccess.Read, FileShare.Read))
 		{
 			using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(28591)))
 			{
 				String s;
 				while (null != (s = sr.ReadLine()))
 					Console.WriteLine(s);
 			}
 		}
 	}
 }
 
  Result:
Changed:
<
<
 1. ruoF  2. erocs  3. dna  4. neves  5. sraey  6. .oga  
>
>
 1. ruoF
 2. erocs
 3. dna
 4. neves
 5. sraey
 6. .oga
 
 
5. Hash Table of User-defined Objects
Changed:
<
<
 using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;    public class MyObject  {     public Double d_x;     public Double d_y;     public Double d_z;       // constructor     public MyObject(Double x_arg, Double y_arg, Double z_arg)     {        d_x = x_arg; d_y = y_arg; d_z = z_arg;     }  };    static class MainClass  {     static void Main(String[] args)     {        Dictionary<String, MyObject> ht = new Dictionary<String, MyObject>();          ht.Add("object 1", new MyObject(3.0, 2.1, Math.PI));        ht.Add("object 2", new MyObject(Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2));        ht.Add("3rd object", new MyObject(2.1, 9.9, Double.NaN));          Console.WriteLine(ht["object 2"].d_x);     }  }  
>
>
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
 public class MyObject
 {
 	public Double d_x;
 	public Double d_y;
 	public Double d_z;
 
 	// constructor
 	public MyObject(Double x_arg, Double y_arg, Double z_arg)
 	{
 		d_x = x_arg; d_y = y_arg; d_z = z_arg;
 	}
 };
 
 static class MainClass
 {
 	static void Main(String[] args)
 	{
 		Dictionary<String, MyObject> ht = new Dictionary<String, MyObject>();
 
 		ht.Add("object 1", new MyObject(3.0, 2.1, Math.PI));
 		ht.Add("object 2", new MyObject(Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2));
 		ht.Add("3rd object", new MyObject(2.1, 9.9, Double.NaN));
 
 		Console.WriteLine(ht["object 2"].d_x);
 	}
 }
 
 
6. Generic Collections

C# has a number of powerful strongly-typed generic collection objects. For example:

Line: 60 to 179
 
SortedDictionary<TKey,TValue> Sorted table of objects of type TValue hashed by objects of type TKey

The following example shows how to use a generic Dictionary<String,int> to perform a very common Computational Linguistics operation, tallying the unique word-types in some text.

Changed:
<
<
 using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;    static class MainClass  {     static void Main(String[] args)     {        String text = "We the People of the United States, in Order to " +        "form a more perfect Union, establish Justice, ensure domestic " +        "Tranquility, provide for the common defence, promote the general " +        "Welfare, and secure the Blessings of Liberty to ourselves and our " +        "Posterity, do ordain and establish this Constitution for the United " +        "States of America.";          Char[] split_chars = new Char[] { '.', ',', ' ' };        String[] words = text.ToLower().Split(split_chars,StringSplitOptions.RemoveEmptyEntries);          // use a hash table to tally word-types:        Dictionary<String, int> hash_tab = new Dictionary<String, int>();        foreach (String w in words)        {           int v;           if (hash_tab.TryGetValue(w,out v))              hash_tab[w] = v+1;           else              hash_tab.Add(w,1);        }          // demonstrate using LINQ to display 12 of the most common word-types plus their counts        KeyValuePair<String, int>[] kvp_arr = hash_tab.OrderByDescending(e => e.Value).Take  (12).ToArray();        // this is similar to 'list comprehension' in Python        String s = kvp_arr.Aggregate(String.Empty,(av, e) => av + e.Key + "[" + e.Value + "] ");        Console.WriteLine(s);     }  }  
>
>
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
 static class MainClass
 {
 	static void Main(String[] args)
 	{
 		String text = "We the People of the United States, in Order to " +
 		"form a more perfect Union, establish Justice, ensure domestic " +
 		"Tranquility, provide for the common defence, promote the general " +
 		"Welfare, and secure the Blessings of Liberty to ourselves and our " +
 		"Posterity, do ordain and establish this Constitution for the United " +
 		"States of America.";
 
 		Char[] split_chars = new Char[] { '.', ',', ' ' };
 		String[] words = text.ToLower().Split(split_chars,StringSplitOptions.RemoveEmptyEntries);
 
 		// use a hash table to tally word-types:
 		Dictionary<String, int> hash_tab = new Dictionary<String, int>();
 		foreach (String w in words)
 		{
 			int v;
 			if (hash_tab.TryGetValue(w,out v))
 				hash_tab[w] = v+1;
 			else
 				hash_tab.Add(w,1);
 		}
 
 		// demonstrate using LINQ to display 12 of the most common word-types plus their counts
 		KeyValuePair<String, int>[] kvp_arr = hash_tab.OrderByDescending(e => e.Value).Take  (12).ToArray();
 		// this is similar to 'list comprehension' in Python
 		String s = kvp_arr.Aggregate(String.Empty,(av, e) => av + e.Key + "[" + e.Value + "] ");
 		Console.WriteLine(s);
 	}
 }
 
  Result:
 the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] 
Line: 64 to 220
  Result:
 the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] 
Deleted:
<
<
 
7. Access a Web Page
Changed:
<
<
 using System;  using System.IO;  using System.Text;  using System.Net;    static class MainClass  {     static void Main(String[] args)     {        String s;        using (WebClient wc = new WebClient())        {           using (Stream str = wc.OpenRead("http://www.compling.washington.edu/compling/"))           {              using (StreamReader sr = new StreamReader(str))                 s = sr.ReadToEnd();           }        }        Console.WriteLine(s);     }  }  
>
>
 using System;
 using System.IO;
 using System.Text;
 using System.Net;
 
 static class MainClass
 {
 	static void Main(String[] args)
 	{
 		String s;
 		using (WebClient wc = new WebClient())
 		{
 			using (Stream str = wc.OpenRead("http://www.compling.washington.edu/compling/"))
 			{
 				using (StreamReader sr = new StreamReader(str))
 					s = sr.ReadToEnd();
 			}
 		}
 		Console.WriteLine(s);
 	}
 }
 
 
8. LINQ Operations

One of the exciting things about mono is that it includes support for one of the latest developments in Microsoft's C# 3.5, namely Language-Integrated Query (LINQ), and its supporting technologies (extension methods and lambda expressions). LINQ allows sophisticated and concise retrieval and manipulation operations to be executed on data collections via native C# language expressions. Categories of operations include aggregation, quantification, conversion, concatenation, retrieval, set (union, intersection, etc.), generation, grouping, join, ordering, projection, partitioning, and restriction (filtering).

LINQ is an expansive topic in its own right. The following simple example gives a glimpse of what is possible.

Changed:
<
<
 using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;    static class MainClass  {     static void Main(string[] args)     {        String[] items = { "cat", "pear", "apple", "cat", "banana", "pear", "pear", "apple" };          KeyValuePair[] tallies = items.GroupBy(k => k, e => 1)              .Select(f => new KeyValuePair<String, int>(f.Key.ToUpper(), f.Sum()))              .OrderBy(g => g.Key)              .ToArray();          foreach (KeyValuePair<String, int> kvp in tallies)           Console.WriteLine(kvp.Key + '\t' + kvp.Value);     }  }  
>
>
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
 static class MainClass
 {
 	static void Main(string[] args)
 	{
 		String[] items = { "cat", "pear", "apple", "cat", "banana", "pear", "pear", "apple" };
 
 		KeyValuePair[] tallies = items.GroupBy(k => k, e => 1)
 				.Select(f => new KeyValuePair<String, int>(f.Key.ToUpper(), f.Sum()))
 				.OrderBy(g => g.Key)
 				.ToArray();
 
 		foreach (KeyValuePair<String, int> kvp in tallies)
 			Console.WriteLine(kvp.Key + '\t' + kvp.Value);
 	}
 }
 
  Result:
Changed:
<
<
 APPLE   2  BANANA  1  CAT     2  PEAR    3  
>
>
 APPLE   2
 BANANA  1
 CAT     2
 PEAR    3
 
 
9. Calling a Python Script as a Shell Process
Changed:
<
<
 using System;  using System.IO;  using System.Diagnostics;    class MainClass  {     public static void Main(string[] args)     {        ProcessStartInfo psi = new ProcessStartInfo();        psi.FileName = "python";        psi.Arguments = "test.py";        psi.RedirectStandardOutput = true;        psi.UseShellExecute = false;          using (Process p = new Process())        {           p.StartInfo = psi;           p.Start();             Console.WriteLine(p.StandardOutput.ReadToEnd());        }     }  }  
>
>
 using System;
 using System.IO;
 using System.Diagnostics;
 
 class MainClass
 {
 	public static void Main(string[] args)
 	{
 		ProcessStartInfo psi = new ProcessStartInfo();
 		psi.FileName = "python";
 		psi.Arguments = "test.py";
 		psi.RedirectStandardOutput = true;
 		psi.UseShellExecute = false;
 
 		using (Process p = new Process())
 		{
 			p.StartInfo = psi;
 			p.Start();
 
 			Console.WriteLine(p.StandardOutput.ReadToEnd());
 		}
 	}
 }
 
  -- Main.gslayden - 14 Nov 2008
 
This site is powered by the TWiki collaboration platformCopyright & by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Privacy Statement Terms & Conditions