Re.Mark

My Life As A Blog

Text Processing in WPF

with one comment

Having seen that I could run my Python code to calculate word frequency in IronPython, my thoughts turned to displaying the results.  Having previously built a simple WPF application in IronPython, I decided to start there.  First step was to create a simple XAML file:


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Text Processor App" Width="640" Height="480">
  <StackPanel>
    <Label>Text Processor</Label>
    <ListBox x:Name="listbox1">
	<ListBox.ItemTemplate>
	    <DataTemplate>
		<StackPanel Orientation="Horizontal" >
		    <TextBlock Text="{Binding Path=[0]}" Margin="0,0,10,0" />
		    <TextBlock Text="{Binding Path=[1]}" />
		</StackPanel>
	    </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
  </StackPanel>
</Window>

I called this file main.xaml.  The results from the Reader class in Python are a list of tuples – each tuple will have the word and the frequency with which it appears.  The CLR type is IronPython.Runtime.PythonTuple – which allows us to access the values via an indexer.  The next step was to create a file called main.py (in the same folder as main.xaml):


import clr
clr.AddReferenceByPartialName("PresentationFramework")

from System.IO import File
from System.Windows.Markup import XamlReader
from System.Windows import Application
from TextProcessor import Reader

file = File.OpenRead('main.xaml')
window = XamlReader.Load(file)
reader = Reader()
list = ["This is a multiline string.\nWith many lines.", "This isn't.", "And nor is this"]
reader.read(list)
window.Content.Children[1].ItemsSource = reader.get_sorted_results()

Application().Run(window)

The next step is to fire up a command prompt and type:

ipy main.py

And it works. Here’s the output on my machine.

image

Advertisements

Written by remark

January 11, 2010 at 7:49 pm

Posted in .NET, Development, Python

One Response

Subscribe to comments with RSS.

  1. thank you… it’s help me..

    deden

    June 10, 2010 at 5:30 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: