https://github.com/couchbaselabs/vellum

Building an FST

To build an FST, create a new builder using the New() method. This method takes an io.Writer as an argument. As the FST is being built, data will be streamed to the writer as soon as possible. With this builder you MUST insert keys in lexicographic order. Inserting keys out of order will result in an error. After inserting the last key into the builder, you MUST call Close() on the builder. This will flush all remaining data to the underlying writer.

In memory:

  var buf bytes.Buffer
builder, err := vellum.New(&buf, nil)
if err != nil {
log.Fatal(err)
}

To disk:

  f, err := os.Create("/tmp/vellum.fst")
if err != nil {
log.Fatal(err)
}
builder, err := vellum.New(f, nil)
if err != nil {
log.Fatal(err)
}

MUST insert keys in lexicographic order:

err = builder.Insert([]byte("cat"), 1)
if err != nil {
log.Fatal(err)
} err = builder.Insert([]byte("dog"), 2)
if err != nil {
log.Fatal(err)
} err = builder.Insert([]byte("fish"), 3)
if err != nil {
log.Fatal(err)
} err = builder.Close()
if err != nil {
log.Fatal(err)
}

Using an FST

After closing the builder, the data can be used to instantiate an FST. If the data was written to disk, you can use the Open()method to mmap the file. If the data is already in memory, or you wish to load/mmap the data yourself, you can instantiate the FST with the Load() method.

Load in memory:

  fst, err := vellum.Load(buf.Bytes())
if err != nil {
log.Fatal(err)
}

Open from disk:

  fst, err := vellum.Open("/tmp/vellum.fst")
if err != nil {
log.Fatal(err)
}

Get key/value:

  val, exists, err = fst.Get([]byte("dog"))
if err != nil {
log.Fatal(err)
}
if exists {
fmt.Printf("contains dog with val: %d\n", val)
} else {
fmt.Printf("does not contain dog")
}

Iterate key/values:

  itr, err := fst.Iterator(startKeyInclusive, endKeyExclusive)
for err == nil {
key, val := itr.Current()
fmt.Printf("contains key: %s val: %d", key, val)
err = itr.Next()
}
if err != nil {
log.Fatal(err)
}

How does the FST get built?

A full example of the implementation is beyond the scope of this README, but let's consider a small example where we want to insert 3 key/value pairs.

First we insert "are" with the value 4.

Next, we insert "ate" with the value 2.

Notice how the values associated with the transitions were adjusted so that by summing them while traversing we still get the expected value.

At this point, we see that state 5 looks like state 3, and state 4 looks like state 2. But, we cannot yet combine them because future inserts could change this.

Now, we insert "see" with value 3. Once it has been added, we now know that states 5 and 4 can longer change. Since they are identical to 3 and 2, we replace them.

Again, we see that states 7 and 8 appear to be identical to 2 and 3.

Having inserted our last key, we call Close() on the builder.

Now, states 7 and 8 can safely be replaced with 2 and 3.

For additional information, see the references at the bottom of this document.

A Go library implementing an FST (finite state transducer)——mark下的更多相关文章

  1. Finite State Transducers

    一, 简介 Finite State Transducers 简称 FST, 中文名:有穷状态转换器.在自然语言处理等领域有很大应用,其功能类似于字典的功能(STL 中的map,C# 中的Dictio ...

  2. Finite State Machine 是什么?

    状态机(Finite State Machine):状态机由状态寄存器和组合逻辑电路构成,能够根据控制信号按照预先设定的状态进行状态转移,是协调相关信号动       作.完成特定操作的控制中心. 类 ...

  3. Finite State Machine

    Contents [hide]  1 Description 2 Components 3 C# - FSMSystem.cs 4 Example Description This is a Dete ...

  4. 证明与计算(7): 有限状态机(Finite State Machine)

    什么是有限状态机(Finite State Machine)? 什么是确定性有限状态机(deterministic finite automaton, DFA )? 什么是非确定性有限状态机(nond ...

  5. paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 standard verilog FSM conding styles(二段式)

    1.Two always block style with combinational outputs(Good Style) 对应的代码如下: 2段式总结: (1)the combinational ...

  6. paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 FSM Coding Goals

    1.the fsm coding style should be easily modifiable to change state encoding and FSM styles. FSM 的的 状 ...

  7. FPGA学习笔记(七)——FSM(Finite State Machine,有限状态机)设计

    FPGA设计中,最重要的设计思想就是状态机的设计思想!状态机的本质就是对具有逻辑顺序和时序规律的事件的一种描述方法,它有三个要素:状态.输入.输出:状态也叫做状态变量(比如可以用电机的不同转速作为状态 ...

  8. paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 standard verilog FSM conding styles(三段式)

    Three always block style with registered outputs(Good style)

  9. TCP Operational Overview and the TCP Finite State Machine (FSM) http://tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF.htm

    http://tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF.htm   http://tcpipgu ...

随机推荐

  1. [GXOI/GZOI2019]与或和(单调栈)

    想了想决定把这几题也随便水个解题报告... bzoj  luogu 思路: 首先肯定得拆成二进制30位啊 此后每一位的就是个01矩阵 Q1就是全是1的矩阵个数 Q2就是总矩阵个数减去全是0的矩阵个数 ...

  2. 笔试算法题(21):将stack内外颠倒 & 判断扑克牌顺子

    出题:要求用递归将一个栈结构的元素内外颠倒: 分析: 本题再次说明系统栈是程序员最好的帮手,但递归度较高所以时间复杂度较大,可以使用空间换时间的方法(额外数组保存栈元素,然后逆向压入): 第一层递归( ...

  3. ORM之创建数据库

    ORM之创建数据库 样板:创建表名为UserInfo的表,表的主键可自行写,Django的ORM也可自行创建. from django.db import models class UserInfo( ...

  4. 用python爬取一张仓鼠图片

    一. 找到一张仓鼠图片并复制一下它的url url='http://img.go007.com/2017/08/16/c407f5b732f4e748_2.jpg' 二. 调用urllib库 impo ...

  5. 节点回来shard仍然delayed原因

    1:es2 fetch shard data时,存在节点刚加入集群,还没有收到cluster metadata的情况.此时,节点因为没有该索引,返回的sharddata为empty,主节点缓存了该sh ...

  6. 缩小Oracle目录下UNDOTBS01.DBF文件的大小

    缩小Oracle目录下UNDOTBS01.DBF文件的大小 分类: Oracle 使用sys用户登录Oracle 方法一:重置表空间大小 执行ALTER DATABASE DATAFILE 'D:OR ...

  7. hihoCoder#1051 补提交卡

    原题地址 简单贪心 首先,补提交卡应该连续使用,其次,补提交卡应该全部用掉(如果补提交卡多于未提交天数则额外处理) 所以,依次遍历未提交日期,计算:从当前位置开始,用M张补提交卡覆盖后面连续M个数字, ...

  8. node.js里的buffer常见操作,copy,concat等实例讲解

    //通过长度构建的buffer内容是随机的 var buffer=new Buffer(100); console.log(buffer); //手动清空buffer,一般创建buffer不会清空 b ...

  9. noi.openjudge——8465 马走日

    http://noi.openjudge.cn/ch0205/8465/ 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  1024kB 描述 马在中国象棋以日字形规则移动. 请编写 ...

  10. P1996||T1282 约瑟夫问题 洛谷||codevs

    https://www.luogu.org/problem/show?pid=1996||http://codevs.cn/problem/1282/ 题目背景 约瑟夫是一个无聊的人!!! 题目描述 ...