• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#强大灵活的脏字过虑:1万字文章过虑1万关键词用时只要1毫秒

OC/C/C++ 水墨上仙 1494次浏览

这几天刚整理完Kudy.Net项目中关键词过虑的功能。关键词过虑在网站开发中也算是比较常见的需求了,特别是在SNS社区网站。在网上找的相关文章都达不到我的要求,所以就自己根据过虑的特点专门写了个KeywordFilter,可能满足基本的过虑要求,性能也不错。它有如下特点:
一、允许你自定义匹配到关键词时返回的结果,例如匹配到“日你”,你可以在原文中显示例如:“”、“**”、“[已过虑]”…
二、允许你按关键词或者关键词的使用频率的排序的优先顺序进行过虑。
三、允许大小写是否敏感(性能上几乎不变),可设置关键词中可跳过的字符,例如设置可跳字符为“▇☆”,关键词里有“我爱你”,那么“我▇爱☆☆你”也会被成功过虑。
备注:如果设置了可跳字符,只会过虑有关键词出现的地方,例如上面“▇我▇爱☆☆你▇”过虑后只有“▇▇”。(哈哈,发现博客园的过虑并没有这功能)
来源:http://www.cnblogs.com/kudy/archive/2011/12/19/2293781.html

&nbsp

既然是简单的处理过虑,里面当然没有做分词的处理,所以有些句子可能会被误报,只要设置重要的敏感禁用词即可。

实现思路:关键字过虑实现的思路及Aho–Corasick高效字符串匹配算法应用(附算法C#实现和测试)

&nbsp

另在一文中看到的过虑效率也不错,请到这里看http://www.cnblogs.com/yeerh/archive/2011/10/20/2219035.html,测试了它的速度是本人的约1.5倍,但是它并没有不区分大小写、可跳字符、关键词排序和多样化自定义关键词替换的功能,我更关心的是功能上的实用和灵活性。

&nbsp

更多请关注:&nbspKudyStudio文章目录

功能实现的相关成员有:Keyword、KeywordOrder、KeywordFormatter、KeywordFilterResult、KeywordFilter、HighlightFormatter、Highlighter。

&nbsp

1.Keyword类,主要有两个属性,Text和Frequency,分别表示关键词文本与它的使用频率(这个属性是可选的),如果从文本中转换为关键词列表,那文本格式是这样的:

Keyword1

Keyword2

Keyword1|Frequency1

Keyword2|Frequency2

&nbsp

[Serializable]
public sealed class Keyword
{
    public Keyword(string text);
    public Keyword(string text, int frequency);
 
    public static implicit operator string(Keyword keyword);
    public static implicit operator Keyword(string keyword);
 
    public string Text { get; }
    public int Frequency { get; set; }
 
    public override bool Equals(object obj);
    public override int GetHashCode();
    public override string ToString();
}
 

2.KeywordOrder枚举,表示过虑时匹配的顺序,允许你按文本或使用频率排序,定义如下:

[Serializable]
public enum KeywordOrder
{
    None = 0,
    Ascending = 1,
    Descending = 2,
    ByFrequencyAscending = 3,
    ByFrequencyDescending = 4,
}
 

3.KeywordFormatter类,这个是抽象类,它的作用就是在匹配到关键词时怎么格式化关键词并返回,里面默认实现了常量和重复字符的Formatter,如果还需要特殊的格式化需求,只要继承KeywordFormatter并实现Format(string&nbspkeyword)方法即可,下面提到的HighlightFormatter就是其中一个例子。

public abstract class KeywordFormatter
{
    public static readonly KeywordFormatter ToEmpty;
    public static readonly KeywordFormatter ToIterantStar;
 
    public static KeywordFormatter CreateConstFormatter(char replacement);
    public static KeywordFormatter CreateConstFormatter(string replacement);
    public static KeywordFormatter CreateIterantCharFormatter(char replacement);
 
    public abstract string Format(string keyword);
}
 
 

4.KeywordFilterResult类,它表示过虑结果,包括过虑后的字符串和被过虑的关键词列表,定义如下:

public sealed class KeywordFilterResult
{
    public KeywordFilterResult(string result, IEnumerable<Keyword> keywords);
 
    public string Result { get; }
    public List<Keyword> Keywords { get; }
}
 

5.KeywordFilter类,这个类是重点,它是实现过虑的核心,其它类只是功能需求上的辅助成员。要注意的是,它的初始化是需要一定的开销的(关键词量大的时候),所以对于大量的关键词,建议不要使用它的静态方法来进行过虑,而是先初始化实例,再重复的调用实例的方法来过虑。里面还有个方法ContainsAny让你快速判断里面是否包括有关键词。

public sealed class KeywordFilter
{
    public KeywordFilter(IEnumerable<Keyword> keywords);
    public KeywordFilter(IEnumerable<string> keywords);
    public KeywordFilter(IEnumerable<Keyword> keywords, IEnumerable<char> skipChars);
    public KeywordFilter(IEnumerable<string> keywords, IEnumerable<char> skipChars);
 
    public ReadOnlyCollection<Keyword> Keywords { get; }
    public ReadOnlyCollection<char> SkipChars { get; }
 
    public bool ContainsAny(string original);
    public bool ContainsAny(string original, bool ignoreCase);
    public bool ContainsAny(string original, KeywordOrder order);
    public bool ContainsAny(string original, KeywordOrder order, bool ignoreCase);
 
    public KeywordFilterResult Filter(string original);
    public KeywordFilterResult Filter(string original, KeywordFormatter formatter);
    public KeywordFilterResult Filter(string original, KeywordFormatter formatter, bool ignoreCase);
    public KeywordFilterResult Filter(string original, KeywordFormatter formatter, KeywordOrder order);
    public KeywordFilterResult Filter(string original, KeywordFormatter formatter, KeywordOrder order, bool ignoreCase);
 
    public static KeywordFilterResult Filter(string original, IEnumerable<Keyword> keywords);
    public static KeywordFilterResult Filter(string original, IEnumerable<string> keywords);
    public static KeywordFilterResult Filter(string original, IEnumerable<Keyword> keywords, KeywordFormatter formatter);
    public static KeywordFilterResult Filter(string original, IEnumerable<string> keywords, KeywordFormatter formatter);
    public static KeywordFilterResult Filter(string original, IEnumerable<Keyword> keywords, KeywordFormatter formatter, bool ignoreCase);
    public static KeywordFilterResult Filter(string original, IEnumerable<Keyword> keywords, KeywordFormatter formatter, KeywordOrder order);
    public static KeywordFilterResult Filter(string original, IEnumerable<string> keywords, KeywordFormatter formatter, bool ignoreCase);
    public static KeywordFilterResult Filter(string original, IEnumerable<string> keywords, KeywordFormatter formatter, KeywordOrder order);
    public static KeywordFilterResult Filter(string original, IEnumerable<Keyword> keywords, KeywordFormatter formatter, KeywordOrder order, bool ignoreCase);
    public static KeywordFilterResult Filter(string original, IEnumerable<string> keywords, KeywordFormatter formatter, KeywordOrder order, bool ignoreCase);
    public static KeywordFilterResult Filter(string original, IEnumerable<Keyword> keywords, IEnumerable<char> skipChars, KeywordFormatter formatter, KeywordOrder order, bool ignoreCase);
    public static KeywordFilterResult Filter(string original, IEnumerable<string> keywords, IEnumerable<char> skipChars, KeywordFormatter formatter, KeywordOrder order, bool ignoreCase);
 
    public static List<Keyword> LoadKeywords(string filePath);
    public static List<Keyword> LoadKeywords(string filePath, Encoding encoding);
    public static List<Keyword> ParseKeywords(string keywordsText);
    public static void SaveKeywords(IEnumerable<Keyword> keywords, string filePath);
    public static void SaveKeywords(IEnumerable<string> keywords, string filePath);
    public static void SaveKeywords(IEnumerable<Keyword> keywords, string filePath, Encoding encoding);
    public static void SaveKeywords(IEnumerable<string> keywords, string filePath, Encoding encoding);
}
 

到此,过虑功能成员介绍完了,下面还有两个成员是在KeywordFilter基础上实现的高亮功能HighlightFormatter和Highlighter。

public sealed class HighlightFormatter : KeywordFormatter
{
    public static readonly HighlightFormatter Html;
 
    public HighlightFormatter(string prefix, string postfix);
 
    public string Postfix { get; }
    public string Prefix { get; }
 
    public static KeywordFormatter Create(string prefix, string postfix);
 
    public override string Format(string keyword);
}
 
public static class Highlighter
{
    public static string Highlight(string original, IEnumerable<Keyword> keywords, HighlightFormatter formatter);
    public static string Highlight(string original, IEnumerable<string> keywords, HighlightFormatter formatter);
    public static string Highlight(string original, IEnumerable<Keyword> keywords, HighlightFormatter formatter, bool ignoreCase);
    public static string Highlight(string original, IEnumerable<Keyword> keywords, HighlightFormatter formatter, KeywordOrder order);
    public static string Highlight(string original, IEnumerable<string> keywords, HighlightFormatter formatter, bool ignoreCase);
    public static string Highlight(string original, IEnumerable<string> keywords, HighlightFormatter formatter, KeywordOrder order);
    public static string Highlight(string original, IEnumerable<Keyword> keywords, HighlightFormatter formatter, KeywordOrder order, bool ignoreCase);
    public static string Highlight(string original, IEnumerable<string> keywords, HighlightFormatter formatter, KeywordOrder order, bool ignoreCase);
}
 


喜欢 (0)
加载中……