Showing posts with label garden. Show all posts
Showing posts with label garden. Show all posts

Wednesday, 23 September 2009

Chars, words and sorted lists


Pictures today are autumn things in the garden. First is some pink Sedum, looking much like pink cauliflower (you can get pink varieties). The second is bright orange Pyracanthus berries before the blackbirds scoff them all.

I've recently been working on a little task which is to run on the console. Not a website at all, much simpler. I found myself starting up my Visual Studio and setting up a project for the first time. Even, rather later, gathering my nice new classes into one place and making them into a library. Hot stuff! I should certainly create libraries more often.

The task was to create an instance of a character reader and assign it a chunk of text as a string. This can then be accessed, one char at a time. In a friendly way it lets you know you have got to the end by throwing an EndOfStreamException. The output wanted was a  list of words in the string sorted by number of occurrences and then alphabetically. After that it got more complicated but that will do for the next time.

I got my text from the first 3 paras of a classic short story "The Star" by Arthur C Clarke. Thank you Wikipedia.

I decided to create a class wrapper for a list of words called WordList. Also a WordListBuilder to process each character as it came and wrap things up at the end. This character reader class is called RealTimeCharReader, there will be another type later.
So:

    ICharReader myCharReader = new RealTimeCharReader();
    assignReaderContent(myCharReader);
    WordListBuilder myWordListBuilder = new WordListBuilder(myCharReader, 
         new WordList());

This means I want an instance of ICharReader, which implements IDispose, so has to have a Dispose() method, to make sure it cleans up. It also has to have a method fetchNextChar() which delivers a character, moves the pointer along and throws an exception when it is finished.

assignReaderContent is a static function allowing me to pass in whatever string I feel like in the Main() function.

WordListBuilder is going to have to take each char and either add it to a new word, or finish the word, eat any nonletter characters and start a new word. Each word is then added to the WordList. A Word is another new class containing a string and a counter. If a word turns up more than once, you can just increment the counter. Both the char reader and the wordlist are passed to the WordListBuilder to do the real work. Here is the call:
myWordListBuilder.buildWordList();
myWordListBuilder.BuilderWordList.makeReport();
buildWordList will process the list, makeReport will sort it and print the results to the console.

buildWordList consists of a try/catch structure containing a forever while loop which takes each returned char and decides what to do with it. The catch intercepts the EndOfStreamException and exits the while loop. Any other runtime exception in the reader gets caught by a more general catch. Either way the finally clause means Dispose is always invoked.

    public void buildWordList()
    {
        char nextChar;
        string nextWord = string.Empty;
                
        try{
            while (true){            
                nextChar = BuilderReader.fetchNextChar();
                if(char.IsLetter(nextChar)){
                    nextWord += nextChar;
                }
                else{
                   // treat all puntuation, digits and whitespace the same
                    if(nextWord.Length > 0){
                        completeWord(nextWord);
                        nextWord = string.Empty;                            
                    }
                }
            }
         }
             
         catch(EndOfStreamException ex){
             // add last word - even if it is partial - discuss[EOS]ion point?
            completeWord(nextWord);
            Console.WriteLine(ex.Message);
         }
         catch(Exception ex){
             Console.WriteLine(ex.Message);
         }
         finally{
             BuilderReader.Dispose();
         }
    }

    private void completeWord(string s){
        if(s.Length > 0){
            Word w = new Word(s);
            BuilderWordList.addNewWord(w);
         }
     }

The local variable nextWord is just a string, set to empty string. Since you have to add all words, including the last one - even if it has only partially arrived before transmission ceased, I added a short method completeWord, to be called whenever a non-letter is read or an EndOfStreamException is hit. This calls the WordList method addNewWord if there is anything in the string.


    public void addNewWord(Word w)
    {  
        // check word exists
        bool foundMatch = false;
        foreach (Word listWord in MyList){
             // if yes, then just increment count
             if (w.Content == listWord.Content){
                  foundMatch = true;
                  listWord.incrementCount();
             }
        }
        if (!foundMatch){
             MyList.Add(w);
        }
    }

Again, a simple method (as I prefer it!). It simply iterated through the list of Word objects in the member variable myList. If it finds a match it sets the boolean foundMatch to true and adds one to the count (or I could have used Count++). If it doesn't, then a new word is added to the list.

So now we have a list which contains a max of one entry for each word but it is not in order. I needed to create a class derived from IComparer and pass it to the sort function for the list of words. An IComparer clas needs to implement the function Compare. In this case we have to compare counts and then alphabetical order. It can be done in one pass, which I think makes life a lot easier!

    public class WordComparer : IComparer<word>
    {
        public int Compare(Word x, Word y)
        {
            if (x.Count > y.Count){
                return -1;
            }
            else if (x.Count < y.Count){
                return 1;
            }
            else{
            // they're the same word count, so sort alphabetically
                return string.Compare(x.Content, y.Content);
            }
        }
    }
The only unexpected part of this was that I assigned 1 as the result if x.Count was the greater. This gave me a reverse sorted list! So I swapped the values round. Notice for the alphabetical order I just use string.Compare, no further specification needed.

Nearly there! The sort function in WordList is very short:
private void sortList()
        {
            WordComparer wc = new WordComparer();
            MyList.Sort(wc);
        }
Passing an instance of IComparer enables the list to sort itself.

The last function simply iterates through the list and writes each word to the console, together with its count.

What's this all good for? Well, you might be monitoring someone's Tweets for rude words or buzzwords to display a Tag_cloud. The next time we'll consider monitoring several streams (Tweeters?) at once with possible delays in transmission.

By the way, I even managed to put all my classes in a library and make the small project refer to it. I'm quite proud.

I don't have any idea how to place a zip file here. I doubt if it would be that interesting at this stage but I ought to find out.

Next Episode

Thursday, 10 September 2009

Damson Time!

You may remember a few posts back I put up a picture of some beautiful damsons growing in our tree. This is called a Shropshire Prune, and I bought it from Keepers Nursery in Kent three years ago.
At that time, I had to go away for the weekend to take the children to a competition and David had to plant the tree in a really pretty large square (not round!) hole. Since he was suffering from vertigo, he had to get a neighbour to make sure he didn't put it in at 45 degrees!
This year it is tall and fairly bushy. Thanks to great weather and huge amounts of water, it has produced a crop for the first time. I'd say about 8 kilos. To my surprise, the small, silver-bloomed dark fruit are very good to eat raw but that is not why I wanted them. When I do get round to eating jam, my favourite is certainly dark purple damson. I have a soft spot for sour cherry as well, perhaps I should find room for a cherry tree?
Anyway, over the last two weekends, my indefatigable husband has produced several jars of damson jam (front, one went to helpful neighbour), one large jar of damsons in rum(back) and reasonable amount of chinese plum sauce(right), which should work just great as a marinade for pork and other things. The rum fruit is for Christmas, if I can wait that long.
Recipes should follow, when I can prise them out of David. I know he made the rum one up. I am also waiting for my daughter to turn the remaining damsons in the freezer into dark red crumble. Me cook? Actually, I make curries and fab wholemeal bread. More another time ...

Thursday, 27 August 2009

MS SQL is too expensive


Hi,

Today's picture was taken by my daughter at the RHS Rosemoor in Devon last week. I love those big scary ferny things. They have large brush like flowers under all the leaves as well.

Spent much of yesterday researching how to become a "reseller" of hosting space. In other words, if I am going to write websites for people, I want somewhere to host their websites so I can keep an eye on them. I like ASP.Net and C#, which implies a Microsoft server and Ms SQL. There are lots of these around, some are very cheap. But ALL of the cheap ones have lots of truly scary reviews. Problem is, it looks like you have to pay a certain amount to Microsoft per MS database and there is no getting around that.

So why do I want it? MySQL is free and as far as I can see does the same job (is relational and all that). I can't use DotNetNuke but then I haven't yet managed to get that to run on my PC anyway. Looks like I can get a deal with a reputable host (for example 1&1 but it could be someone else) and use ASP.Net and MySQL (it runs on a Windows PC, it can run on a Windows server, surely?).

Ok, next step is how to connect to MySQL, the easiest way possible via Visual Studio 2008. I recommend this tutorial by Dr. Jay Krishnaswamy. This looks really lovely until you get to the part where you change the database type. MySQL just does not appear. The magic (and it took me ages to find this out) is on the MySQL website itself. Look through the downloads page (there aren't very many) and choose the latest stable connector for MySQL and .Net. This is an msi file. Scary though it is, I just downloaded it and let it do its stuff.

Next time I opened up VisStudio and had another go at configuring the data source, MySQL appeared in the list! After that it is all a matter of making sure your WAMP server is on and choosing the database and table you want. You also have to follow the above tutorial, to avoid errors (i.e, VisStudio does not like square brackets in SQL queries).

I now have a page which shows a gridview of a MySQL table on localhost. Next stop the external server!

Thursday, 6 August 2009

Let's Get Going!


Well, I've been keeping a diary of my rather technical doings for some time now, do it would be useful to summarize now and again. To give some relief from webstuff boredom, I think I'll add a picture of something beautiful every day. This will get me out and taking pictures even if only in the garden! Otherwise I'd spend all my time in Starbucks.

Yesterday and today I have been grappling with the open source-ish AJAX Control Toolkit, specifically with the Calendar extender. Very nice little widget that pops up under a textbox so you can choose a date. Trouble is, it has preset css. So if, for example, you have set your font size a bit big, the calendar won't fit into its predefined space. In my case I lost Friday and Saturday. To make any changes to the CSS, I could find no other solution than to replace most of it (it ignores little bits). I copied suggestions from a site called Top54. Written last month, so up-to-date. I now have a large mass of extra class definitions in my CSS. Haven't they heard of the cascade? I also have a largeish date chooser with colours from my website and a rather wonky pair of substitute next/prev arrows.

Wierd how much time such seemingly little problems can eat up, eh?

The image today is two of our first crop of damsons. We've had the tree 3-4 years now and finally we see them. Gorgeous colour and bloom! With any luck at least one pot of dark crimson jam to come.