-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
991b008
commit 97fc7b7
Showing
4 changed files
with
42 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ranking | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/CocaineCong/tangseng/app/search_engine/types" | ||
"github.com/CocaineCong/tangseng/pkg/util/relevant" | ||
) | ||
|
||
// CalculateScoreBm25 计算相关性 | ||
func CalculateScoreBm25(token string, searchItem []*types.SearchItem) (resp []*types.SearchItem) { | ||
recallToken := make([]string, 0) | ||
for i := range searchItem { | ||
recallToken = append(recallToken, searchItem[i].Content) | ||
} | ||
corpus, _ := relevant.MakeCorpus(recallToken) | ||
docs := relevant.MakeDocuments(recallToken, corpus) | ||
tf := relevant.New() | ||
for _, doc := range docs { | ||
tf.Add(doc) | ||
} | ||
tf.CalculateIDF() | ||
tokenRecall := relevant.Doc{corpus[token]} | ||
bm25Scores := relevant.BM25(tf, tokenRecall, docs, 1.5, 0.75) | ||
sort.Sort(sort.Reverse(bm25Scores)) | ||
|
||
for i := range bm25Scores { | ||
searchItem[bm25Scores[i].ID].Score = bm25Scores[i].Score | ||
} | ||
sort.Slice(searchItem, func(i, j int) bool { | ||
return searchItem[i].Score > searchItem[j].Score | ||
}) | ||
resp = make([]*types.SearchItem, 0) | ||
resp = searchItem | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package ranking |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package ranking |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters