UCD Search Engine Project summary

Split the string by BOOLEAN keywords:

Example: split string by "AND"

using System;

namespace stringtest{

class test{

public static void Main() {

        string words = "This is a list of words AND with a bit of punctuation";

        string [] split = words.Split(new Char [] {'A', 'N', 'D'});

        foreach (string s in split) {

            if (s.Trim() != "")
                Console.WriteLine(s);
        }
    }
}

}

************************************************************************************************************************************************************************************

去除一個string中的標點符號

using System.Text.RegularExpressions;
string p = "This is a test string, with lots of: punctuations; in it?!.";
		p = Regex.Replace(p, @"[^\w\s]", "");
		Console.WriteLine(p);
http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex.aspx
**************************************************************************************************************************************************************************************

去除stopwords

在搜索引擎中 有很多stopwords會被搜索引擎忽略, 這裏是一個去除stopwords的程序

首先把stopwords存儲在一個txt文檔裏面

using System.IO;
using System.Text.RegularExpressions;
string text =
        @"This is a text with lots and lots of stopwords. I am a student, It also talks a bit about load balancing. Load balancing is not a stopword.";

		
		string[] stopwords = File.ReadAllLines(@"stopwords.txt");

		 string regexCode = 
        @"(?<=(\A|\s|\.|,|!|\?))(" + 
        string.Join("|", stopwords) + 
        @")(?=(\s|\z|\.|,|!|\?))";
		
		Regex regex = new Regex(regexCode, RegexOptions.Singleline | RegexOptions.IgnoreCase);
        string cleaned = regex.Replace(text, " ");
        Console.WriteLine("\nAfter remove stopwords:");
        Console.WriteLine(cleaned);
***************************************************************************************************************************************************************************************

去除double space

接着上面的程序,由於去除了stopwords後產生了大量的double space(雙倍空白), 所以用下面的程序進行去除

//remove double spaces
		Regex removeDoubleWhiteSpace = 
        new Regex(@"\s{2,}", RegexOptions.Singleline | RegexOptions.IgnoreCase);
        cleaned = removeDoubleWhiteSpace.Replace(cleaned, " ");
        Console.WriteLine("\nAfter remove double white spaces:");
        Console.WriteLine(cleaned);

*************************************************************************************************************************************************************************************

讀取文件 的path問題 (如何讀取當前目錄下的)

string[] stopwords = File.ReadAllLines(HttpContext.Current.Server.MapPath("stopwords.txt")); 
在事件方法中可以用Server.path 但是在code中 需要用 
HttpContext.Current.Server.MapPath
*************************************************************************************************************************************************************************************

How to use LinkButton within Repeater

.aspx
------------------ 
<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="MyUpdate" CommandArgument='<%# Eval("erid") %>'>LinkButton</asp:LinkButton>
    </ItemTemplate>
</asp:Repeater>
 
 
.cs
------------------
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "MyUpdate"){
        //e.CommandArgument --> contain the erid value
        //Do something
    }
}

***********************************************************************************************************************************************************************************

How to get the Domain name from the url (handle the String)

string urls = "http://www.dotnetperls.com/groupby";
		
		string FinalUrl;
		if(urls.Contains("www"))
		{
		   FinalUrl = urls.Remove(0,urls.IndexOf(".")+1);
		   FinalUrl = FinalUrl.Substring(FinalUrl.IndexOf(".")+1, FinalUrl.IndexOf("/") - FinalUrl.IndexOf(".")-1);
		}
		else
		{
		   FinalUrl = urls;
		}
		Console.WriteLine(FinalUrl);

Output: com

*************************************************************************************************************************************************************************************

RadionButtonList usage method

http://rhondatipton.net/2006/11/27/radiobuttonlist-control-in-aspnet/

*************************************************************************************************************************************************************************************

計算程序運行時間:

using System.Diagnostics;

private Stopwatch stw = new Stopwatch();

private void Form1_Load(object sender, EventArgs e)
{
stw.Start();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("真的要退出?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
stw.Stop();
MessageBox.Show("程序共運行時間:" + stw.Elapsed.Seconds.ToString() + "." + stw.Elapsed.Milliseconds.ToString() + "秒");
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}

.

****************************************************************************************************************************************************************************************


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章