文章关键词人工提取检查

 场景:博客文章关键词人工提取检查

描述:

a.   初期关键词内容已由人工提取完成。关键词不含特殊字符,除英文使用的单引号、英文句号之外,不存在任何标点符号

b.   依照语言分为中文文章、英文文章。(分别处理)

c.   所有语言关键词,词间分隔符为空格

d.   中文关键词,以汉字为结尾,词的开始和中间允许存在非汉字。也允许存在个别关键词只有英文的情况

e.   英文关键词,以英文+汉字的关键词为一个英文关键词的结尾,不存在不包含汉字的英文关键词

编写要求:

a.   提供的关键词样本为一个字符串

b.   根据描述,对关键词样本进行词语排重操作,即重复出现的关键词只保留一个样本,其余的删除

c.   结果可展示可演示

d.   要求面向对象编程思想

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace test2

{

/// <summary>

/// 博客文章关键词提取工具类

/// </summary>

/// 

class MyKeyWord

{

 

 

//关键字排重

public static string[] deleteTheSame(string keywordStr)

{

string[] temp = getKeyWords(keywordStr);

List<string> newKeyWords = new List<string>();

foreach (string str in temp)

if (newKeyWords.Contains<string>(str))

continue;

else

newKeyWords.Add(str);

return newKeyWords.ToArray<string>();

}

 

//获取关键字

public static string[] getKeyWords(string keywordStr)

{

if (isEnglistWord(keywordStr))

return getEnglishKeyWords(keywordStr);

else

return getChinesKeyWords(keywordStr);

}

//判断是否为英文关键字

public static Boolean isEnglistWord(string keywordStr)

{

string[] temp = getWords(keywordStr);

foreach (string str in temp)

{

 

for (int i = 0; i < str.Length; i++)

{

byte[] byte_len = Encoding.Default.GetBytes(str.Substring(i, 1));

if (i == 0 && byte_len.Length == 2)

return false;

if (byte_len.Length == 2)

return true;

}

}

 

return false;

}

//解析单词

public static string[] getWords(string keywordStr)

{

string[] temp = keywordStr.Split(' ');

List<string> words = new List<string>();

foreach (string tempstr in temp)

{

string str = tempstr.Trim();

if (str != null && str != "")

words.Add(str);

}

 

return words.ToArray<string>();

}

//获取英文关键字

public static string[] getEnglishKeyWords(string keywordStr)

{

Console.WriteLine("该关键字串是英文关键字:");

List<string> keywords = new List<string>();

StringBuilder tempstr = new StringBuilder();

string[] temp = getWords(keywordStr);

int flag = 0;

for (int i = 0; i < temp.Length; i++)

{

//Console.WriteLine("danci:"+temp[i]);

byte[] byte_len = Encoding.Default.GetBytes(temp[i].Substring(temp[i].Length - 1, 1));

if (byte_len.Length == 2)

{

for (int j = flag; j <= i; j++)

if (tempstr.Length == 0)

tempstr.Append(temp[j]);

else

tempstr.Append(" " + temp[j]);

flag = i + 1;

keywords.Add(tempstr.ToString());

tempstr.Remove(0, tempstr.Length);

}

 

 

}

 

return keywords.ToArray<string>();

}

//获取中文关键字

public static string[] getChinesKeyWords(string keywordStr)

{

Console.WriteLine("该关键字串是中文关键字:");

return getWords(keywordStr);

}

 

 

 

 

}

class Program

{

//测试程序

static void Main(string[] args)

{

string mytest = "China中国 China中国 THE UNITED STATES美国";

 

Console.WriteLine("英文关键字测试:");

Console.WriteLine("原关键字串:" + mytest);

string[] keywords = MyKeyWord.deleteTheSame(mytest);

Console.WriteLine("解析后关键字串:");

foreach (string temp in keywords)

Console.WriteLine(temp);

 

 

 

mytest = "China 中国 中国 THE UNITED STATES 美国";

 

Console.WriteLine("中文关键字测试:");

Console.WriteLine("原关键字串:" + mytest);

keywords = MyKeyWord.deleteTheSame(mytest);

Console.WriteLine("解析后关键字串:");

foreach (string temp in keywords)

Console.WriteLine(temp);

 

 

 

 

Console.ReadKey();

 

 

}

}

}

 

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