Skip to content

Commit dc3bf9a

Browse files
author
willzhen
committed
Update readme
1 parent 89337c3 commit dc3bf9a

File tree

6 files changed

+118
-13
lines changed

6 files changed

+118
-13
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- [任务调度模块](#任务调度模块)
77

88
# go common library
9-
本仓库整理了 golang 中最常用的代码、函数、和模块。
9+
本库整理了 golang 中最常用的代码、函数、和模块,避免新项目总是重复造基础代码的轮子
1010

1111
## httpcall 模块
1212
常用的简单 http 调用的封装。
@@ -25,10 +25,11 @@
2525
详见[stl_extension 模块说明](https://github.com/memory-overflow/go-common-library/blob/main/stl_extension/readme.md)
2626

2727
## text 模块
28-
常用文本处理相关方法
28+
常用文本处理算法
2929
- AcTrie:ac 自动机,多模式串快速匹配。在一个文本中快速找出来出现过哪些字符串子串以及其定位。可以理解对同一文本 s 多次调用 strings.Contains(s, xxx) 的加速。
3030
- Levenshtein:计算文本编辑距离。
3131
- TextSim:计算两个文本的相似度。
32+
- SliceSame:判断两个字符串数组是否相同。
3233

3334

3435
详见[text 模块说明](https://github.com/memory-overflow/go-common-library/blob/main/text/readme.md)

misc/readme.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
- TimeTick —— 计时器,通过 TimeTick.Tick() 打点计时间,返回距离上一次 Tick 过去的时间,单位 ms。
1414
- Timed —— 函数调用计时。
1515

16-
example:
16+
example: [TestTimeRelated](https://github.com/memory-overflow/go-common-library/blob/main/misc/time_related/time_test.go#L15)
1717
```go
1818
import (
1919
"testing"
@@ -51,7 +51,7 @@ func TestTimeRelated(t *testing.T) {
5151
- DownloadFile 下载网络文件
5252
- DownloadFileWithLimit 下载网络文件(带文件大小限制)
5353

54-
example:
54+
example: [TestDownload](https://github.com/memory-overflow/go-common-library/blob/main/misc/misc_test.go#L13)
5555
```go
5656
import (
5757
"context"
@@ -88,8 +88,7 @@ func TestDownload(t *testing.T) {
8888
- GoroutineHelp.Recoverd:协程 recover 处理函数。
8989
- GoroutineHelp.SafeGo:安全的启用携程。
9090

91-
example:
92-
91+
example: [TestGoroutineHelp](https://github.com/memory-overflow/go-common-library/blob/main/misc/misc_test.go#L35)
9392
```go
9493
import (
9594
"context"
@@ -137,7 +136,7 @@ IDGenerator 对象封装了很多生成 id 的函数。
137136
## log
138137
- FoldLog: 折叠日志中数据过长的字段,输入仅支持 json 字符串。
139138

140-
example:
139+
example: [TestFlodLog](https://github.com/memory-overflow/go-common-library/blob/main/misc/misc_test.go#L52)
141140
```go
142141
import (
143142
"context"

stl_extension/readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,18 @@ group.Wait()
3838
```
3939

4040
## OrderedMap
41-
golang stl 原生的 map 是基于 hash 的无需 map,OrderedMap 是对 hash map 的补充。支持按照 key 顺序遍历。
41+
golang stl 原生的 map 是基于 hash 的无序 map,OrderedMap 是对 hash map 的补充。支持按照 key 顺序遍历。
42+
4243
OrderedMap 使用 avl 树实现,是线程安全的。
44+
4345
和 c++ map 对比测试,1000 万的随机的增删查操作:
4446
OrderedMap: 21806 ms, c++ map: 11592ms,效率比 c++ map 慢两倍。
4547
next 遍历整个 map, OrderedMap: 676ms, c++ map: 171ms;
4648
prev 遍历整个 map, OrderedMap: 663ms, c++ map: 198ms;
4749
遍历的效率大概比 c++ map 的慢三倍。
4850

51+
c++ map 用红黑树实现,在随机数据上表现会比 avl 树要好。avl 树深度会比红黑树低一点,查询效率比较高。但是插入的效率没有红黑树高。
52+
4953
用法:
5054
- 构建
5155
```go

text/readme.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
1+
- [text 模块](#text-模块)
2+
- [SliceSame](#slicesame)
3+
- [Aho-Corasick automaton](#aho-corasick-automaton)
4+
- [计算文本编辑距离](#计算文本编辑距离)
5+
- [计算文本相似度](#计算文本相似度)
6+
17
# text 模块
8+
golang 里面的 strings 库已经有了很多丰富的字符串处理功能,但是都是偏向于基础处理。
9+
10+
text模块提供了一些字符串处理相关的算法能力。
11+
12+
## SliceSame
13+
- SliceSame——判断两个字符串数字是否相同。
14+
15+
example: [TestTextSim](https://github.com/memory-overflow/go-common-library/blob/main/text/text_test.go#L29)
16+
```go
17+
import (
18+
"testing"
19+
20+
"github.com/memory-overflow/go-common-library/text"
21+
)
22+
23+
func TestTextSim(t *testing.T) {
24+
sim := text.TextSim("编辑距离测试", "测试一下距离")
25+
t.Logf("sim: %f", sim)
26+
}
27+
```
28+
29+
## Aho-Corasick automaton
30+
ac 自动机是一种多模式串的匹配算法。
31+
32+
一个常见的例子就是给出 n 个单词,再给出一段包含 m 个字符的文章,让你找出有多少个单词在文章里出现过。
33+
34+
比较容易想到的做法是,调用 n 次 `strings.Contains(s, xxx)`。假设 n 个单词平局长度为 k, 这样处理的算法时间复杂度为 O(n * k * m)。而使用 ac 自动机可以加速上述过程,整体算法时间复杂度只需要 O(n*k + m)。
35+
36+
example: [TestActrie](https://github.com/memory-overflow/go-common-library/blob/main/text/text_test.go#L9)
37+
```go
38+
import (
39+
"testing"
40+
41+
"github.com/memory-overflow/go-common-library/text"
42+
)
43+
44+
func TestActrie(t *testing.T) {
45+
// 在字符串 "哈哈哈哈23434dfgdd" 中找出所有 "哈哈哈", "234","dfg" 出现的位置。
46+
// 使用模式串构建一个 ac 自动机
47+
ac := text.BuildAcTrie([]string{"哈哈哈", "234", "dfg"})
48+
// 匹配母串
49+
list, index := ac.Search("哈哈哈哈23434dfgdd")
50+
for i, l := range list {
51+
t.Log(l, index[i])
52+
}
53+
}
54+
```
55+
56+
## 计算文本编辑距离
57+
编辑距离(Edit Distance):是一个度量两个字符序列之间差异的字符串度量标准,两个单词之间的编辑距离是将一个单词转换为另一个单词所需的单字符编辑(插入、删除或替换)的最小数量。一般来说,编辑距离越小,两个串的相似度越大。
58+
59+
example: [TestActrie](https://github.com/memory-overflow/go-common-library/blob/main/text/text_test.go#L24)
60+
```go
61+
import (
62+
"testing"
63+
64+
"github.com/memory-overflow/go-common-library/text"
65+
)
66+
67+
func TestLevenshtein(t *testing.T) {
68+
dist := text.Levenshtein([]rune("编辑距离测试"), []rune("测试一下距离"))
69+
t.Logf("dist: %d", dist)
70+
}
71+
```
72+
73+
## 计算文本相似度
74+
通过编辑距离,计算两个文本的相似度。
75+
76+
example: [TestTextSim](https://github.com/memory-overflow/go-common-library/blob/main/text/text_test.go#L17)
77+
```go
78+
import (
79+
"testing"
80+
81+
"github.com/memory-overflow/go-common-library/text"
82+
)
283

84+
func TestTextSim(t *testing.T) {
85+
sim := text.TextSim("编辑距离测试", "测试一下距离")
86+
t.Logf("sim: %f", sim)
87+
}
88+
```

text/slice_same.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ func SliceSame(a, b []string) bool {
77
if len(a) != len(b) {
88
return false
99
}
10-
sort.Strings(a)
11-
sort.Strings(b)
12-
for i := 0; i < len(a); i++ {
13-
if a[i] != b[i] {
10+
tmpa, tempb := []string{}, []string{}
11+
copy(tmpa, a)
12+
copy(tempb, b)
13+
sort.Strings(tmpa)
14+
sort.Strings(tempb)
15+
for i := 0; i < len(tmpa); i++ {
16+
if tmpa[i] != tempb[i] {
1417
return false
1518
}
1619
}

text/text_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@ func TestActrie(t *testing.T) {
1515
}
1616

1717
func TestTextSim(t *testing.T) {
18-
sim := text.TextSim("坚持干部一线值守常态化防控不放松让市民群众度过一个健康放心的假期", "这个口现在要开始封死")
18+
sim := text.TextSim("编辑距离测试", "测试一下距离")
1919
if sim != 0 {
2020
t.Error("Failed")
2121
}
2222
}
23+
24+
func TestLevenshtein(t *testing.T) {
25+
dist := text.Levenshtein([]rune("编辑距离测试"), []rune("测试一下距离"))
26+
t.Logf("dist: %d", dist)
27+
}
28+
29+
func TestSliceSmae(t *testing.T) {
30+
a := []string{"3", "2", "1"}
31+
same := text.SliceSame(a, a)
32+
t.Logf("is same: %v", same)
33+
t.Log(a)
34+
}

0 commit comments

Comments
 (0)