Tuesday, August 14, 2007

Create a serch engine with linq

I always amazed by how search engine works and it seem so wonderful that google has created the most amazing search engine to put infomation on our finger tips. Would you like to create a simple search engine? Here you can do it pretty easy with Linq.

The techniques needed in constructing search engine are regular expression, Linq, Generic, C#, sort and ranking algorithm.

Here is the code:


using System;
using System.Collections.Generic;
using
System.Text;
using System.Text.RegularExpressions;
using
System.Query;
namespace search
{
public class Search
{
private
List _SearchRelevance = new
List();
public List
SearchRelevence
{
get
{
return
_SearchRelevance;
}
}
private List _Results =
new List();
public List
Results
{
get
{
return
_Results;
}
}


public Search(string input, string[]
text)
{
for (int i = 0; i < text.Length; i++)
{
SearchRelevance
sr = new SearchRelevance(input,
text[i]);
sr.Calculate();
_SearchRelevance.Add(sr);
}
Sort();
}
private
void Sort()
{
var relevences =
from w in _SearchRelevance
orderby
w.RelevancePoint descending
select w;
foreach (SearchRelevance sri in
relevences)
{
_Results.Add(sri);
}
}

}
public class
SearchRelevance
{
public SearchRelevance(string input, string
text)
{
_OriginalText = text;
_UserInput = input;
}
public
override string ToString()
{
return _RelevancePoint.ToString() +" "+
_OriginalText ;
}
internal void Calculate()
{
string[] splitedinput
= UserInput.Split(new char[] { ' ' });
Dictionary
relevency = new Dictionary();
//initialize the relevency
dic.
for (int i = 0; i < splitedinput.Length;
i++)
{
relevency.Add(splitedinput[i], 0);
}
for (int i = 0; i <
splitedinput.Length; i++)
{
Regex reg = new Regex(splitedinput[i],
RegexOptions.IgnoreCase);
if (reg.IsMatch(OriginalText))
{
int
point;
relevency.TryGetValue(splitedinput[i], out
point);
relevency.Remove(splitedinput[i]);
relevency.Add(splitedinput[i],
++point);
}
}
_Relevancy = relevency;
//sum the relevency
int
rpoint = 0;
foreach (KeyValuePair kv in
relevency)
{
rpoint += kv.Value;
}
_RelevancePoint =
rpoint;
}
private string _UserInput;
public string
UserInput
{
get
{
return _UserInput;
}
}
private string
_OriginalText;
public string OriginalText
{
get
{
return
_OriginalText;
}
}
private int _RelevancePoint;
public int
RelevancePoint
{
get
{
return _RelevancePoint;
}
}
private
Dictionary _Relevancy = new Dictionaryint>();
public Dictionary
Relevancy
{
get
{
return _Relevancy;
}
}
}
public class
Program
{
static void Main(string[] args)
{
string[] sb = new
string[4];
sb[0] = "Relevance cassida shell theory may be seen as an
attempt";
sb[1] = "registered by NSF (Class H1) cassida for use
where";
sb[2] = "Shell Cassida Silicone Fluid is";
sb[3] = "to work out in
detail one of Grice’s central claims";
string input = "nsf shell central h1
sillicone fluid";
Search sr = new Search(input, sb);
SearchRelevance[] res
= sr.Results.ToArray();
}
}
}

Thursday, August 9, 2007

XML as data storage, query with Linq

Most of the time data is stored as database but how about data stored as a file format? If data is stored as file format meaning that we have much more power to decide the storage however file as storage is not always the ideal situation if data is too complex to store and difficute to manage. Today we see relational database is so successfull because of the database management design. We use SQL to manipulate data and we are happy with the power and the flexibility that SQL could give.

However, there is a problem that storing data in database always seem to be bulky for small application. Imagine that, you are creating a subsystem as a service of a larger system, which has is own database implementation. Logically, it is not advisable that the sub system use it own database or share the data store with the larger system mainly because having a it own database is too bulky and has environment limitation. And, if data store is shared meaning that integration will be too costly and complicated.

The solution is to use XML as isolated data storage. We have to build services around the data and expose it as interface for larger system integration. Having using xml as data, the integration is much simpler and less impact on the overall larger system.

Most people i would say do not like to query XML data because of the complicated query and it is not SQL liked. However, now the situation has changed, you can query data having SQL liked by using linq to XML. Developer can use linq to query XML data. In the next discussion, I will show you how you can play around with this Linq to XML technique.

Frackyly, i do not like XML as data store until i found that Linq has so much to promise.

for more information on the linq technology, please visit
http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx

Read XML data and bind to datagridview .NET

if you want to bind xml data to your datagrid, you need do as below:

dataSet1.ReadXml("kwyProducts.xml");
dataGridView1.DataSource =
dataSet1;
dataGridView1.DataMember = "KWYProducts";

Notes: please specify your DataMember correctly else will throw you this error:

Child list for field KWYProduscts cannot be created.