golang实现快速自动关键词提取算法插件库RAKE.go的使用

Golang实现快速自动关键词提取算法插件库RAKE.go的使用

RAKE.go是一个Golang实现的快速自动关键词提取(RAKE)算法库,基于Rose等人2010年提出的算法。该算法能够从单个文档中自动提取关键词。

算法背景

RAKE算法描述于以下论文: Rose, S., Engel, D., Cramer, N., & Cowley, W. (2010). Automatic Keyword Extraction from Individual Documents. In M. W. Berry & J. Kogan (Eds.), Text Mining: Theory and Applications: John Wiley & Sons.

原始Python实现可在GitHub上找到。

源代码采用MIT许可证发布。

使用示例

下面是一个完整的RAKE.go使用示例:

package main

import (
	"github.com/afjoseph/RAKE.go"
	"fmt"
)

func main() {
	// 定义要提取关键词的文本
	text := `The growing doubt of human autonomy and reason has created a state of moral confusion where man is left without the guidance of either revelation or reason. The result is the acceptance of a relativistic position which proposes that value judgements and ethical norms are exclusively matters of arbitrary preference and that no objectively valid statement can be made in this realm... But since man cannot live without values and norms, this relativism makes him an easy prey for irrational value systems.`

	// 运行RAKE算法提取关键词
	candidates := rake.RunRake(text)

	// 打印提取的关键词及其分数
	for _, candidate := range candidates {
		fmt.Printf("%s --> %f\n", candidate.Key, candidate.Value)
	}

	// 打印提取到的关键词总数
	fmt.Printf("\nsize: %d\n", len(candidates))
}

示例输出

运行上述代码将产生类似以下输出:

objectively valid statement --> 9.000000
exclusively matters --> 4.000000
arbitrary preference --> 4.000000
easy prey --> 4.000000
relativistic position --> 4.000000
human autonomy --> 4.000000
relativism makes --> 4.000000
growing doubt --> 4.000000
moral confusion --> 4.000000
ethical norms --> 3.500000
norms --> 1.500000
made --> 1.000000
guidance --> 1.000000
man --> 1.000000
result --> 1.000000
systems --> 1.000000
values --> 1.000000
realm --> 1.000000
live --> 1.000000
judgements --> 1.000000
reason --> 1.000000
left --> 1.000000
proposes --> 1.000000
irrational --> 1.000000
created --> 1.000000
acceptance --> 1.000000
revelation --> 1.000000
state --> 1.000000

size: 28

使用说明

  1. 首先导入RAKE.go库:github.com/afjoseph/RAKE.go
  2. 准备要提取关键词的文本
  3. 调用rake.RunRake(text)函数,传入文本内容
  4. 函数返回一个关键词候选列表,每个候选包含关键词文本和对应的分数
  5. 可以根据分数对关键词进行排序或筛选

RAKE算法特别适合从单个文档中提取关键词,无需预先训练模型或语料库。提取的关键词通常是多词短语,并根据其在文本中的统计特性进行评分。


更多关于golang实现快速自动关键词提取算法插件库RAKE.go的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang实现快速自动关键词提取算法插件库RAKE.go的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


RAKE.go - Golang快速自动关键词提取库

RAKE (Rapid Automatic Keyword Extraction) 是一种简单高效的自动关键词提取算法,特别适合从单文档中提取关键词。下面我将介绍如何在Go中使用RAKE.go库实现关键词提取。

安装RAKE.go

首先使用go get安装RAKE.go库:

go get github.com/Obaied/RAKE.go

基本使用方法

package main

import (
	"fmt"
	"github.com/Obaied/RAKE.go"
)

func main() {
	// 示例文本
	text := `关键词提取是自然语言处理中的重要任务。RAKE算法是一种无监督的、独立于语言的关键词提取算法,
	它通过分析词频和共现关系来识别文本中的关键短语。本示例展示如何在Go中使用RAKE.go库。`

	// 1. 初始化RAKE实例
	r := rake.NewRake()
	
	// 2. 运行关键词提取
	keywords := r.Run(text)
	
	// 3. 输出结果
	fmt.Println("提取的关键词及得分:")
	for _, kw := range keywords {
		fmt.Printf("%s (%.2f)\n", kw.Key, kw.Value)
	}
	
	// 4. 获取前N个关键词
	topN := r.GetKeywords(text, 3)
	fmt.Println("\nTop 3关键词:")
	for _, kw := range topN {
		fmt.Println(kw.Key)
	}
}

高级配置选项

RAKE.go提供了一些配置选项来优化提取结果:

func main() {
	text := "自定义停用词列表可以提高关键词提取的准确性。停用词通常包括常见但无实际意义的词汇。"

	// 自定义停用词列表
	stopWords := []string{"的", "是", "在", "了", "和", "有", "可以", "通常", "包括"}
	
	// 自定义分隔符(默认包含空格、标点等)
	wordSeparators := []rune{' ', '.', ',', '!', '?', ';', ':', '\t', '\n'}
	
	// 初始化RAKE实例并配置
	r := rake.NewRake(
		rake.WithStopWords(stopWords),          // 自定义停用词
		rake.WithWordSeparators(wordSeparators), // 自定义分隔符
		rake.WithMinCharLength(2),              // 关键词最小长度
		rake.WithMaxWordsLength(3),             // 关键词最大单词数
	)
	
	keywords := r.Run(text)
	
	fmt.Println("自定义配置后的关键词提取结果:")
	for _, kw := range keywords {
		fmt.Printf("%s (%.2f)\n", kw.Key, kw.Value)
	}
}

处理中文文本

对于中文文本,建议先进行分词处理:

import (
	"github.com/Obaied/RAKE.go"
	"github.com/yanyiwu/gojieba" // 中文分词库
)

func main() {
	text := "自然语言处理是人工智能领域的一个重要方向。中文分词是中文自然语言处理的基础步骤。"
	
	// 使用结巴分词进行预处理
	x := gojieba.NewJieba()
	defer x.Free()
	
	words := x.Cut(text, true)
	processedText := strings.Join(words, " ") // 用空格连接分词结果
	
	// 使用RAKE提取关键词
	r := rake.NewRake()
	keywords := r.Run(processedText)
	
	fmt.Println("中文文本关键词提取结果:")
	for _, kw := range keywords {
		fmt.Printf("%s (%.2f)\n", kw.Key, kw.Value)
	}
}

性能优化建议

  1. 预处理文本:去除HTML标签、特殊字符等
  2. 缓存停用词:如果处理大量文本,可以预先加载停用词
  3. 并行处理:对于多文档处理可以使用goroutine
func processDocuments(docs []string) []rake.Keyword {
	r := rake.NewRake()
	var results []rake.Keyword
	
	// 使用带缓冲的channel处理结果
	resultChan := make(chan []rake.Keyword, len(docs))
	
	// 并行处理每个文档
	for _, doc := range docs {
		go func(d string) {
			resultChan <- r.Run(d)
		}(doc)
	}
	
	// 收集结果
	for range docs {
		results = append(results, <-resultChan...)
	}
	
	return results
}

评估关键词提取结果

RAKE.go不包含内置的评估方法,但你可以通过以下方式评估:

func evaluateExtraction(keywords []rake.Keyword, groundTruth []string) float64 {
	extracted := make(map[string]bool)
	for _, kw := range keywords {
		extracted[kw.Key] = true
	}
	
	var matchCount int
	for _, gt := range groundTruth {
		if extracted[gt] {
			matchCount++
		}
	}
	
	precision := float64(matchCount) / float64(len(keywords))
	recall := float64(matchCount) / float64(len(groundTruth))
	
	// F1分数
	if precision+recall == 0 {
		return 0
	}
	return 2 * precision * recall / (precision + recall)
}

RAKE.go是一个简单但有效的关键词提取工具,特别适合快速原型开发和小规模文本处理。对于更复杂的需求,可能需要考虑结合其他NLP技术或使用更先进的算法。

回到顶部