golang从给定源生成标签集合的插件库Tagify的使用

Golang从给定源生成标签集合的插件库Tagify的使用

简介

Tagify是一个可以从STDIN、文件或HTTP地址作为输入,并返回按流行度排序的最常用单词列表作为输出的工具。

支持格式:

  • 纯文本
  • HTML
  • Markdown

支持语言:

  • 英语
  • 俄语
  • 中文
  • 印地语
  • 希伯来语
  • 西班牙语
  • 阿拉伯语
  • 日语
  • 德语
  • 法语
  • 韩语

安装

二进制安装

MacOS:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/zoomio/tagify/master/_bin/install.sh)" -o darwin

MacOS (arm64):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/zoomio/tagify/master/_bin/install.sh)" -o darwin arm64

Linux:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/zoomio/tagify/master/_bin/install.sh)" -o linux

Windows:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/zoomio/tagify/master/_bin/install.sh)" -o windows

Go依赖安装

go get -u github.com/zoomio/tagify/...

使用示例

命令行示例

tagify -s https://github.com/zoomio/tagify -l 5

Go代码示例

package main

import (
	"fmt"
	"github.com/zoomio/tagify"
	"github.com/zoomio/tagify/config"
	"github.com/zoomio/tagify/model"
)

func main() {
	// 创建配置
	cfg := config.New(
		// 设置输入源
		config.Source("https://github.com/zoomio/tagify"),
		// 设置标签数量限制
		config.Limit(5),
		// 设置语言为英语
		config.Lang("en"),
		// 禁用停用词过滤
		config.NoStopWords(false),
	)
	
	// 调用Tagify处理
	result, err := tagify.Tagify(cfg)
	if err != nil {
		panic(err)
	}
	
	// 打印结果
	printTags(result)
}

func printTags(result *model.Result) {
	fmt.Println("Top Tags:")
	for _, tag := range result.Tags {
		fmt.Printf("- %s (Score: %.2f)\n", tag.Value, tag.Score)
	}
}

扩展功能(Beta)

从v0.50.0版本开始,Tagify添加了对扩展的支持。您可以在extension/extension.go中查看扩展接口,以及在processor/html/extension.go中的实现示例。

注意事项

  1. 使用-no-stop标志可以禁用停用词过滤
  2. 可以通过-lconfig.Limit()设置返回的标签数量限制
  3. 支持多种输入源:URL、文件或直接文本输入

许可证

Tagify基于Apache License 2.0发布。


更多关于golang从给定源生成标签集合的插件库Tagify的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang从给定源生成标签集合的插件库Tagify的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Golang Tagify 插件库使用指南

Tagify 是一个用于从给定源生成标签集合的 Go 语言插件库,它可以帮助你从文本、URL 或其他数据源中提取关键词或标签。

安装

首先安装 Tagify 库:

go get github.com/tagify/tagify

基本使用

1. 从文本生成标签

package main

import (
	"fmt"
	"github.com/tagify/tagify"
)

func main() {
	// 创建 Tagify 实例
	t := tagify.New()

	// 从文本生成标签
	text := "Golang is a statically typed, compiled programming language designed at Google. It is similar to C, but with memory safety, garbage collection, and CSP-style concurrency."
	
	tags, err := t.Generate(text)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println("Generated Tags:")
	for _, tag := range tags {
		fmt.Printf("- %s (Score: %.2f)\n", tag.Value, tag.Score)
	}
}

2. 自定义配置

func main() {
	// 使用自定义配置
	config := tagify.Config{
		Language:    "en",          // 语言设置
		MaxTags:     10,            // 最大标签数
		MinScore:    0.5,           // 最低分数阈值
		StopWords:   []string{"is", "at", "to", "but", "with", "and"}, // 自定义停用词
		Stemming:    true,          // 启用词干提取
		Normalize:   true,          // 启用标准化
	}

	t := tagify.NewWithConfig(config)

	text := "The quick brown fox jumps over the lazy dog. Programming in Go is fun and efficient."
	
	tags, err := t.Generate(text)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println("Customized Tags:")
	for _, tag := range tags {
		fmt.Printf("- %s\n", tag.Value)
	}
}

3. 从 URL 生成标签

func main() {
	t := tagify.New()

	// 从 URL 内容生成标签
	url := "https://en.wikipedia.org/wiki/Go_(programming_language)"
	
	tags, err := t.GenerateFromURL(url)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println("Tags from URL:")
	for i, tag := range tags {
		if i >= 10 { // 只显示前10个标签
			break
		}
		fmt.Printf("%d. %s (%.2f)\n", i+1, tag.Value, tag.Score)
	}
}

高级功能

1. 使用自定义提取器

func main() {
	t := tagify.New()

	// 添加自定义提取器
	t.AddExtractor(func(input string) ([]tagify.Tag, error) {
		// 这里可以实现你自己的标签提取逻辑
		// 例如从特定格式的文本中提取标签
		return []tagify.Tag{
			{Value: "custom-tag1", Score: 0.9},
			{Value: "custom-tag2", Score: 0.7},
		}, nil
	})

	text := "This is some sample text."
	tags, _ := t.Generate(text)
	
	fmt.Println("Tags with custom extractor:")
	for _, tag := range tags {
		fmt.Println(tag.Value)
	}
}

2. 标签后处理

func main() {
	t := tagify.New()

	// 添加后处理函数
	t.AddPostProcessor(func(tags []tagify.Tag) []tagify.Tag {
		// 过滤掉长度小于3的标签
		var filtered []tagify.Tag
		for _, tag := range tags {
			if len(tag.Value) >= 3 {
				filtered = append(filtered, tag)
			}
		}
		return filtered
	})

	text := "Go is great for web services and cloud applications."
	tags, _ := t.Generate(text)
	
	fmt.Println("Filtered Tags:")
	for _, tag := range tags {
		fmt.Println(tag.Value)
	}
}

性能优化

对于大量文本处理,可以使用并发处理:

func main() {
	t := tagify.New()
	
	texts := []string{
		"First document about Go programming",
		"Second text discussing cloud computing",
		"Third article on machine learning",
	}
	
	results := make(chan []tagify.Tag, len(texts))
	
	// 并发处理多个文本
	for _, text := range texts {
		go func(txt string) {
			tags, _ := t.Generate(txt)
			results <- tags
		}(text)
	}
	
	// 收集结果
	for i := 0; i < len(texts); i++ {
		tags := <-results
		fmt.Printf("Document %d Tags:\n", i+1)
		for _, tag := range tags {
			fmt.Printf("- %s\n", tag.Value)
		}
		fmt.Println()
	}
}

总结

Tagify 是一个功能强大且灵活的标签生成库,通过简单的 API 提供了:

  1. 从文本、URL 等多种源生成标签
  2. 可定制的配置选项
  3. 可扩展的提取器和后处理器
  4. 支持并发处理

你可以根据项目需求调整配置,或扩展其功能来满足特定的标签生成需求。

回到顶部