基于KWIC 的keyword匹配算法(管道+过滤器模式下实现)
以下是基于KWIC 的keyword匹配算法(管道+过滤器模式下实现)
关键部分的管道+过滤器 软件体系下的实现, 在非常多的keyword搜索平台都使用了这一 循环移位+排序输出的 keyword匹配算法:
详细需求例如以下:
1、使用管道-过滤器风格:
每一个过滤器处理数据,然后将结果送至下一个过滤器,。
要有数据传入,过滤器即開始工作。
过滤器之间的数据共享被严格限制在管道传输
四个过滤器:
输入(Input filter):
从数据源读取输入文件,解析格式,将行写入输出管道
移位(CircularShifter filter):循环移位
排序(Alphabetizer filter):
输出(Output filter)
管道:
in_cs pipe
cs_al pipe
al_ou pile
比如:
代码例如以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; namespace KWIC
{
/// <summary>
/// 管道类
/// </summary>
public class Pipe
{
List<string> word;
public List<string> read()
{
return word;
}
public void write(List<string> word)
{ this.word = word; }
} /// <summary>
/// 管道之间的过滤器接口
/// </summary>
public abstract class Filter
{ public virtual void Transform()
{ }
} /// <summary>
/// 继承并实现实现管道接口
/// </summary>
public class InputFilter : Filter
{ public Pipe outPipe;
public List<string> word;
public InputFilter(List<string> word, Pipe outPipe)
{
this.word = word;
this.outPipe = outPipe;
}
public void Transform()
{
outPipe.write(word);
}
} /// <summary>
/// 继承并实现过滤器接口
/// </summary>
public class CircleShiftFilter : Filter
{
public Pipe inputPipe;
public Pipe outPipe;
public CircleShiftFilter(Pipe inputPipe, Pipe outPipe)
{
this.inputPipe = inputPipe;
this.outPipe = outPipe;
}
/// <summary>
/// 关键的循环移位函数
/// </summary>
public virtual void Transform()
{
List<string> word = inputPipe.read(); /////////////////////////////////////////////// 补充代码,将WORD数组中字符串循环移位//////////////////////////////////////////////////////// List<string> turned_words = new List<string>(); // 获得每一行字符串数据
foreach (string line in word)
{
// 拆分一句话
string[] words = line.Split(' '); // 获取单词数
ulong word_number = (ulong)words.LongLength; // 暂时存储中间排序好的串
List<string> tmp_words = new List<string>(); tmp_words.Clear(); tmp_words.Add(line); string tmp_line = ""; for (ulong i = 0; i < word_number - 1; i++)
{
// 获取上一行串
tmp_line = tmp_words[tmp_words.Count - 1]; // 获取上一行串的最后一个单词
string last_word = tmp_line.Split(' ')[word_number -1]; // 获取上一行串的除了最后一个单词之外的全部单词
string left_words = tmp_line.Substring(0, (tmp_line.Length -last_word.Length-1 )); tmp_words.Add(last_word +" "+ left_words );
} // 移除原有的串
tmp_words.RemoveAt(0); // 将一句移位的串加到暂时的list集合
turned_words.AddRange(tmp_words); } // 将全部移位的串加到原来list集合
word.AddRange(turned_words); /////////////////////////////////////
outPipe.write(word); }
} /// <summary>
/// 实现的排序过滤器类
/// </summary>
public class AlphaFilter : Filter
{
public Pipe inputPipe;
public Pipe outPipe;
public AlphaFilter(Pipe inputPipe, Pipe outPipe)
{
this.inputPipe = inputPipe;
this.outPipe = outPipe;
} /// <summary>
/// 排序输出函数
/// </summary>
public void Transform()
{
List<string> word = inputPipe.read(); ////////////////////////////////////// 补充代码,将word数组中单词排序输出/////////////////////////////////////////////////
word.Sort(); outPipe.write(word); }
} /// <summary>
/// 实现输出过滤器接口类
/// </summary>
public class OutputFilter : Filter
{
public Pipe inputPipe;
public Pipe outPipe;
public OutputFilter(Pipe inputPipe, Pipe outPipe)
{
this.inputPipe = inputPipe; this.outPipe = outPipe; }
public void Transform()
{
List<string> word = inputPipe.read();
outPipe.write(word);
}
} /// <summary>
/// 程序的总体执行框架
/// </summary>
public class KWIC_System
{ Pipe in_cs; // create three objects of Pipe
Pipe cs_al; // and one object of type
Pipe al_ou; // FileInputStream
Pipe ou_ui; // FileInputStream
InputFilter inputFilter;
CircleShiftFilter shifter;
AlphaFilter alpha;
OutputFilter output; // output to screen
public KWIC_System()
{
in_cs = new Pipe(); // create three objects of Pipe
cs_al = new Pipe(); // and one object of type
al_ou = new Pipe(); // FileInputStream
ou_ui = new Pipe(); // FileInputStream List<string> word = new List<string>();
word.Add(Regex.Replace("I love you".Trim(), @"\s+", " ")); //正则会获取到全部类型的空格(比方制表符。新行等等),然后将其替换为一个空格
word.Add(Regex.Replace("me too".Trim(), @"\s+", " "));
word.Add(Regex.Replace("do you know".Trim(), @"\s+", " ")); inputFilter = new InputFilter(word, in_cs);
shifter = new CircleShiftFilter(in_cs, cs_al);
alpha = new AlphaFilter(cs_al, al_ou);
output = new OutputFilter(al_ou,ou_ui); // output to screen
}
public List<string > GetResult()
{
inputFilter.Transform();
shifter.Transform();
alpha.Transform();
output.Transform(); return ou_ui.read();
} } }
(备注:假设想换行这里想换行输出,须要在结尾输出的每一行结尾加‘\r\n’)
在广泛的搜索技术中。事实上这个keyword匹配算法应用范围非常广,比方我们常见的Baidu和Google的搜索keyword 提示功能。
基于KWIC 的keyword匹配算法(管道+过滤器模式下实现)的更多相关文章
- docker4dotnet #5 使用VSTS/TFS搭建基于容器的持续交付管道
在过去的几篇d4d系列中,我给大家介绍了如何使用docker来支持asp.net core的应用开发,打包的场景.Asp.net core的跨平台开发能力为.net开发人员提供了使用容器进行应用开发的 ...
- 【配置】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。
× 检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 5 ...
- 基于Java的Http服务器几种模式演进
首先抛出问题: 程序1---错误版本 import java.io.IOException; import java.io.InputStream; import java.io.PrintWrite ...
- Core 1.0中的管道-中间件模式
ASP.NET Core 1.0中的管道-中间件模式 SP.NET Core 1.0借鉴了Katana项目的管道设计(Pipeline).日志记录.用户认证.MVC等模块都以中间件(Middlewar ...
- 设计模式之过滤器模式——Java语言描述
过滤器模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来 实现 创建一个Person对象.Criteria 接口和实现了该接口的实体类,来过滤 Person 对象的列 ...
- 设计模式系列之过滤器模式(Chriteria Pattern)
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来.这种类 ...
- 设计模式のFilterPattern(过滤器模式)----结构模式
一.产生背景 我们有一堆“人”的对象,我们应该怎么选择出其中的男性.女性或者其他类型的呢?这时候我们可以用过滤器模式 二.通常做法 我们将创建一个 Person 对象.Criteria 接口和实现了该 ...
- 【转】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。
检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 500.23 ...
- Java设计模-过滤器模式
过滤器模式 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接 ...
随机推荐
- css美化、优化、合并工具推荐
其实很多时候,我们写完css规则之后,我们思考的无非就是3件事情: 验证 美化 压缩 当然无论是我们自己做这样的工具还是寻找一些比较好的成熟的工具,都有几个期望: 是否支持一些ie下的hack方式: ...
- Socket.IO介绍:支持WebSocket、用于WEB端的即时通讯的框架
一.基本介绍 WebSocket是HTML5的一种新通信协议,它实现了浏览器与服务器之间的双向通讯.而Socket.IO是一个完全由JavaScript实现.基于Node.js.支持WebSocket ...
- Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)
解题报告 意思就是说有n行柜子,放奖杯和奖牌.要求每行柜子要么全是奖杯要么全是奖牌,并且奖杯每行最多5个,奖牌最多10个. 直接把奖杯奖牌各自累加,分别出5和10,向上取整和N比較 #include ...
- 【UI设计】扁平化设计之流行色值
收集了一些颜色值 顏色表示方法: 以命名方式定义经常使用的顏色,如color="green".可是自由度较低,何况单词量...... 以RGB值表示.如#FF0000表示red(红 ...
- ArcGIS中的查询
最近身体不适,静下心来看了一下以前收集的电子书.下面是<ArcGIS地理信息系统教程_第5版>(李玉龙)第5章“查询”的读书笔记. 1.查询的常见应用: 选择感兴趣的要素:查找哪些要素满足 ...
- 几种流行Webservice框架性能对比(转载)
1摘要 开发webservice应用程序中离不开框架的支持,当open-open网站列举的就有很多种,这对于开发者如何选择带来一定的疑惑.性能Webservice的关键要素,不同的框架性能上存在较大差 ...
- web中的安全编码
个人记录 一.Web安全验证 输入验证 防范跨站脚本XSS攻击 防止SQL注入 图片验证码 二.输入验证 经典的安全法则:永远不要相信用户提交的数据 验证内容: 用户名,密码等格式 验证长度防止数据库 ...
- C++AMP介绍(一)
C++AMP介绍(一) 最后更新日期:2014-05-02 阅读前提: 环境:Windows 8.1 64bit英文版,Visual Studio 2013 Professional Update1英 ...
- Canvas简述
HTML Canvas API有两方面优势可以弥补:首先,不需要将所绘制图像中的每个图元当做对象存储,因此执行性能非常好:其次,在其他编程语言现有的优秀二维绘图API的基础上实现Canvas API相 ...
- 关于SimHash去重原理的理解(能力工场小马哥)
阅读目录 1. SimHash与传统hash函数的区别 2. SimHash算法思想 3. SimHash流程实现 4. SimHash签名距离计算 5. SimHash存储和索引 6. SimHas ...