Skip to content

Commit

Permalink
Merge pull request #1 from cryptography-research-lab/dev
Browse files Browse the repository at this point in the history
add CalculateScoreAndDescSort & add doc
  • Loading branch information
CC11001100 authored Feb 4, 2023
2 parents 60a759c + 259ce57 commit 2b5589a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 3 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 句子打分

# 一、概述

用来区分一个句子中含有的有效的单词的数量,其实就是一个简单的分词,用于在一些暴力枚举破解字典的场景下把尽可能真实的解找出来。

# 二、安装

```bash
go get -u github.com/cryptography-research-lab/go-sentence-score
```

# 三、示例代码

```go
package main

import (
"fmt"
sentence_score "github.com/cryptography-research-lab/go-sentence-score"
)

func main() {

// 计算单个句子
s := "1123thisisverybkjkjbsecurityfghfkeyassdfdfadasdasdasdasdasdasdasdgoodasdasdking"
words, score := sentence_score.CalculateScore(s)
fmt.Println(words)
fmt.Println(score)

// 计算多个句子并排序
slice := []string{
"1123thisisverybkjkjbsecurityfghfkeygoodasdasdking",
"1123thisisverybkjkintenetjbsecurityfghfkeygoodasdasdking",
"1123thisisverybknamejkjbsecurityfghfkeygoodasdasdking",
}
sort := sentence_score.CalculateScoreAndDescSort(slice)
fmt.Println(sort)

}
```





25 changes: 25 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
sentence_score "github.com/cryptography-research-lab/go-sentence-score"
)

func main() {

// 计算单个句子
s := "1123thisisverybkjkjbsecurityfghfkeyassdfdfadasdasdasdasdasdasdasdgoodasdasdking"
words, score := sentence_score.CalculateScore(s)
fmt.Println(words)
fmt.Println(score)

// 计算多个句子并排序
slice := []string{
"1123thisisverybkjkjbsecurityfghfkeygoodasdasdking",
"1123thisisverybkjkintenetjbsecurityfghfkeygoodasdasdking",
"1123thisisverybknamejkjbsecurityfghfkeygoodasdasdking",
}
sort := sentence_score.CalculateScoreAndDescSort(slice)
fmt.Println(sort)

}
50 changes: 48 additions & 2 deletions sentence_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package sentence_score

import (
variable_parameter "github.com/golang-infrastructure/go-variable-parameter"
"sort"
"strings"
)

// BuiltinDictionary 内建的词典
var BuiltinDictionary = LoadBuiltinDictionary()

// ------------------------------------------------ ---------------------------------------------------------------------

// CalculateScore 为字符串计算得分
func CalculateScore(sentence string, dictionary ...*Dictionary) float64 {
func CalculateScore(sentence string, dictionary ...*Dictionary) ([]string, float64) {

// 如果没有传递字典的话,则使用默认的字典
dictionary = variable_parameter.SetDefaultParam(dictionary, BuiltinDictionary)
Expand Down Expand Up @@ -62,5 +65,48 @@ func CalculateScore(sentence string, dictionary ...*Dictionary) float64 {
}

// 读取单词,采取最长匹配原则
return float64(len(words))
return words, float64(len(words))
}

// ------------------------------------------------ ---------------------------------------------------------------------

// CalculateScoreAndDescSort 对一组字符串计算得分并倒序排序
func CalculateScoreAndDescSort(sentenceSlice []string, dictionary ...*Dictionary) []*CalculateScoreAndSortResultItem {

// 计算得分
result := make([]*CalculateScoreAndSortResultItem, len(sentenceSlice))
for index, sentence := range sentenceSlice {
words, score := CalculateScore(sentence)
result[index] = NewCalculateScoreAndSortResultItem(sentence, words, score)
}

// 排序
sort.Slice(result, func(i, j int) bool {
return result[i].Score > result[j].Score
})

return result
}

// CalculateScoreAndSortResultItem 对上一项的每条计算结果
type CalculateScoreAndSortResultItem struct {

// 是哪个句子
Sentence string

// 从Sentence中解析到的单词
Words []string

// 得分是多少
Score float64
}

func NewCalculateScoreAndSortResultItem(sentence string, words []string, score float64) *CalculateScoreAndSortResultItem {
return &CalculateScoreAndSortResultItem{
Sentence: sentence,
Words: words,
Score: score,
}
}

// ------------------------------------------------ ---------------------------------------------------------------------
13 changes: 12 additions & 1 deletion sentence_score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import (

func TestCalculateScore(t *testing.T) {
s := "1123thisisverybkjkjbsecurityfghfkeyassdfdfadasdasdasdasdasdasdasdgoodasdasdking"
score := CalculateScore(s)
words, score := CalculateScore(s)
fmt.Println(words)
fmt.Println(score)
}

func TestCalculateScoreAndDescSort(t *testing.T) {
slice := []string{
"1123thisisverybkjkjbsecurityfghfkeygoodasdasdking",
"1123thisisverybkjkintenetjbsecurityfghfkeygoodasdasdking",
"1123thisisverybknamejkjbsecurityfghfkeygoodasdasdking",
}
sort := CalculateScoreAndDescSort(slice)
fmt.Println(sort)
}

0 comments on commit 2b5589a

Please sign in to comment.