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=""
Title="Text Processor App" Width="640" Height="480">
    <Label>Text Processor</Label>
    <ListBox x:Name="listbox1">
		<StackPanel Orientation="Horizontal" >
		    <TextBlock Text="{Binding Path=[0]}" Margin="0,0,10,0" />
		    <TextBlock Text="{Binding Path=[1]}" />

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 (in the same folder as main.xaml):

import clr

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"]
window.Content.Children[1].ItemsSource = reader.get_sorted_results()


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


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



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..


    June 10, 2010 at 5:30 am

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: