scws4go

package module
v0.0.0-...-41e9440 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2014 License: MIT Imports: 4 Imported by: 1

README

#scws4go 这是一个对scws的go绑定,区别于bylevel/goscws的是提供更加简单的可并发的接口.

#安装方法 ##安装hightman/scws 请参考scws安装方法.

在我机器上,我是这样把第三方库安装在我的$HOME/usr/目录下的:

cd scws-1.2.1 ; ./configure --prefix=$HOME/usr/ ; make install

如果你也这样安装,请确认环境变量的设置:

  • CPLUS_INCLUDE_PATH包含了$HOME/usr/include
  • LIBRARY_PATH包含了$HOME/usr/lib

cgo使用gcc编译器的时候需要靠这两个变量来找到scws.(这些都是太基础东西了,请原谅我啰嗦了一次.)

##安装 go get github.com/getwe/scws4go ##下载词典 到xunsearch下载词典和规则文件. #例子 ##例子1,简单使用 package main

import (
    "github.com/getwe/scws4go"
    "fmt"
    "os"
)

func main() {
    if len(os.Args) < 3 {
        fmt.Printf("Usage : %s dict rule\n",os.Args[0])
        os.Exit(0)
    }
    scws:= scws4go.NewScws()

    err := scws.SetDict(os.Args[1], scws4go.SCWS_XDICT_XDB)
    if err != nil { fmt.Println(err);os.Exit(0)}
    err = scws.SetRule(os.Args[2])
    if err != nil { fmt.Println(err);os.Exit(0)}

    scws.SetCharset("utf8")
    scws.SetIgnore(1)
    scws.SetMulti(scws4go.SCWS_MULTI_SHORT & scws4go.SCWS_MULTI_DUALITY & scws4go.SCWS_MULTI_ZMAIN)

    scws.Init(1)

    segResult,err := scws.Segment("scws4go是c语言版本的scws的go封装.")
    if err != nil {
        fmt.Println(err)
    } else {
        for _,t := range segResult {
            fmt.Printf("term[%s],attr[%s],idf[%f]\n",t.Term,t.Attr,t.Idf)
        }
    }
}

并发例子,详见example2目录

Documentation

Index

Constants

View Source
const (
	// SetMulti mode
	SCWS_MULTI_SHORT   = C.SCWS_MULTI_SHORT
	SCWS_MULTI_DUALITY = C.SCWS_MULTI_DUALITY
	SCWS_MULTI_ZMAIN   = C.SCWS_MULTI_ZMAIN
	SCWS_MULTI_ZALL    = C.SCWS_MULTI_ZALL

	// SetDict/AddDict mode
	SCWS_XDICT_TXT = C.SCWS_XDICT_TXT
	SCWS_XDICT_XDB = C.SCWS_XDICT_XDB
	SCWS_XDICT_MEM = C.SCWS_XDICT_MEM
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Scws

type Scws struct {
	// contains filtered or unexported fields
}

Scws是封装好的切词服务.提供一个简单的切词接口.

func NewScws

func NewScws() *Scws

Scws构造函数

func (*Scws) AddDict

func (this *Scws) AddDict(fPath string, mode int) error

添加词典文件到当前scws 对象 参数fpath 词典的文件路径,词典格式是XDB或XT 格式。 参数mode有3种值,分别为预定义的: SCWS_XDICT_TXT 表示要读取的词典文件是文本格式,可以和后2项结合用 SCWS_XDICT_XDB 表示直接读取 xdb 文件 SCWS_XDICT_MEM 表示将 xdb 文件全部加载到内存中,以 XTree 结构存放,可用异或结合另外2个使用。 具体用哪种方式需要根据自己的实际应用来决定。当使用本库做为守护进程时推荐使用 mem 方式, 当只是嵌入调用时应该使用 xdb 方式,将 xdb 文件加载进内存不仅占用了比较多的内存, 而且也需要一定的时间(35万条数据约需要0.3~0.5秒左右)

func (*Scws) Free

func (this *Scws) Free() error

释放Scws的全部资源.

func (*Scws) Init

func (this *Scws) Init(count int) error

内部复制多个scws实例,并发调用Segment的时候可以并发切词. 否则Segment也能正常使用,只是所有切词都是串行执行.

func (*Scws) Segment

func (this *Scws) Segment(text string) ([]ScwsRes, error)

func (*Scws) SetCharset

func (this *Scws) SetCharset(cs string)

设定当前 scws 所使用的字符集. 参数cs 新指定的字符集.若无此调用则系统缺省使用gbk,还支持utf8,指定字符集时参数的大小写不敏感 错误若指定的字符集不存在,则会自动使用gbk 字符集替代.

func (*Scws) SetDict

func (this *Scws) SetDict(fPath string, mode int) error

清除并设定当前scws 操作所有的词典文件

func (*Scws) SetDuality

func (this *Scws) SetDuality(yes int)

设定是否将闲散文字自动以二字分词法聚合。 参数yes 如果为1 表示执行二分聚合,0 表示不处理,缺省为0

func (*Scws) SetIgnore

func (this *Scws) SetIgnore(yes int)

设定分词结果是否忽略所有的标点等特殊符号(不会忽略\r和\n) 参数yes 1 表示忽略,0 表示不忽略,缺省情况为不忽略

func (*Scws) SetMulti

func (this *Scws) SetMulti(mode int)

设定分词执行时是否执行针对长词复合切分。(例:“中国人”分为“中国”、“人”、“中国人”)。 参数mode复合分词法的级别,缺省不复合分词.取值由下面几个常量异或组合: SCWS_MULTI_SHORT 短词 SCWS_MULTI_DUALITY 二元(将相邻的2个单字组合成一个词) SCWS_MULTI_ZMAIN 重要单字 SCWS_MULTI_ZALL 全部单字

func (*Scws) SetRule

func (this *Scws) SetRule(fPath string) error

设定规则集文件

type ScwsRes

type ScwsRes struct {
	Term string  //分词的结果
	Attr string  //词性
	Idf  float64 //idf值
}

分词结果

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL