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 (KeyValuePairkv 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 Dictionary int>();
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();
}
}
}