GithubHelp home page GithubHelp logo

snowball's Introduction

Snowball

A Go (golang) implementation of the Snowball stemmer for natural language processing.

Status
Latest release v0.9.0 (2023-11-14)
Latest build status Build
Languages available English, Spanish (español), French (le français), Russian (ру́сский язы́к), Swedish (svenska), Norwegian (norsk), Hungarian (magyar)
License MIT

Usage

Here is a minimal Go program that uses this package in order to stem a single word.

package main
import (
	"fmt"
	"github.com/kljensen/snowball"
)
func main(){
	stemmed, err := snowball.Stem("Accumulations", "english", true)
	if err == nil{
		fmt.Println(stemmed) // Prints "accumul"
	}
}

Organization & Implementation

The code is organized as follows:

  • The top-level snowball package has a single exported function snowball.Stem, which is defined in snowball/snowball.go.
  • The stemmer for each language is defined in a "sub-package", e.g snowball/spanish.
  • Each language exports a Stem function: e.g. spanish.Stem, which is defined in snowball/spanish/stem.go.
  • Code that is common to multiple languages may go in a separate package, e.g. the small romance package.

Some notes about the implementation:

  • In order to ensure the code is easily extended to non-English languages, I avoided using bytes and byte arrays, and instead perform all operations on runes. See snowball/snowballword/snowballword.go and the SnowballWord struct.
  • In order to avoid casting strings into slices of runes numerous times, this implementation uses a single slice of runes stored in the SnowballWord struct for each word that needs to be stemmed.
  • In spite of the foregoing, readability requires that some strings be kept around and repeatedly cast into slices of runes. For example, in the Spanish stemmer, one step requires removing suffixes with accute accents such as "ución", "logía", and "logías". If I were to hard-code those suffices as slices of runes, the code would be substantially less readable.
  • Instead of carrying around the word regions R1, R2, & RV as separate strings (or slices or runes, or whatever), we carry around the index where each of these regions begins. These are stored as R1start, R2start, & RVstart on the SnowballWord struct. I believe this is a relatively efficient way of storing R1 and R2.
  • The code does not use any maps or regular expressions 1) for kicks, and 2) because I thought they'd negatively impact the performance. (But, mostly for #1; I realize #2 is silly.)
  • I end up refactoring the snowballword package a bit every time I implement a new language.
  • Clearly, the Go implentation of these stemmers is verbose relative to the Snowball language. However, it is much better than the Java version and others.

Testing

To run the tests, do go test ./... in the top-level directory.

Future work

I'd like to implement the Snowball stemmer in more languages. If you can help, I would greatly appreciate it: please fork the project and send a pull request!

(Also, if you are interested in creating a larger NLP project for Go, please get in touch.)

Related work

I know of a few other stemmers availble in Go:

Contributors

License (MIT)

Copyright (c) the Contributors (see above)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

snowball's People

Contributors

arhuman avatar eivindam avatar hermanschaaf avatar kljensen avatar marfi avatar matryer avatar shawnps avatar tgulacsi avatar zncoder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

snowball's Issues

Rename of file in #8 causes tests not to run

Exceptions look like the following

# github.com/kljensen/snowball/russian
russian/russian_test.go:12: undefined: romance.WordBoolTestCase
russian/russian_test.go:18: undefined: romance.RunRunewiseBoolTest
russian/russian_test.go:25: undefined: romance.WordBoolTestCase
russian/russian_test.go:32: undefined: romance.RunWordBoolTest
russian/russian_test.go:36: undefined: romance.FindRegionsTestCase
russian/russian_test.go:139: undefined: romance.RunFindRegionsTest
russian/russian_test.go:145: undefined: romance.StepTestCase
russian/russian_test.go:298: undefined: romance.RunStepTest
russian/russian_test.go:305: undefined: romance.StepTestCase
russian/russian_test.go:408: undefined: romance.RunStepTest

Happy -> Happi?

Hi,

I noticed that the library converts the word "happy" to "happi". Is it intentional ?

I've tried this text:

Are Hunter-Gatherers The Happiest Humans To Inhabit Earth? There's an idea percolating up from the anthropology world that may make you rethink what makes you happy.

Got this result:

hunter-gatherers happiest humans inhabit earth there's idea percolating anthropology world make rethink makes happi

Badly named file

Hi

You named romance/common_testing.go instead of romance/common_test.go

This files is not seen as a testing package and is compiled with the whole package.

So, when I use your package using flag package, using "-h" flag, I see the entire test.XXX flags.

I forked your package to rename the file and it works.

BTW: Thanks for your work, it works great

How to detect language

Can you provide example, how to use this package to detect language from stream of text

It is not working for linux or docker

This is an amazing implementation, congrats!
But i'm trying to use it in a project and everything works perfect in MacOS, but when i try to use it in linux or docker i receive the following error

/usr/local/go/src/runtime/internal/sys/consts.go:18:7: DefaultPhysPageSize redeclared in this block
	/usr/local/go/src/runtime/internal/sys/arch_amd64.go:10:2: other declaration of DefaultPhysPageSize
/usr/local/go/src/runtime/internal/sys/consts.go:22:7: PCQuantum redeclared in this block
	/usr/local/go/src/runtime/internal/sys/arch_amd64.go:11:2: other declaration of PCQuantum
/usr/local/go/src/runtime/internal/sys/consts.go:25:7: Int64Align redeclared in this block
	/usr/local/go/src/runtime/internal/sys/arch_amd64.go:12:2: other declaration of Int64Align
/usr/local/go/src/runtime/internal/sys/consts.go:32:7: MinFrameSize redeclared in this block
	/usr/local/go/src/runtime/internal/sys/arch_amd64.go:13:2: other declaration of MinFrameSize
/usr/local/go/src/runtime/internal/sys/intrinsics_common.go:9:5: len8tab redeclared in this block
	/usr/local/go/src/runtime/internal/sys/intrinsics.go:76:7: other declaration of len8tab
/usr/local/go/src/runtime/internal/sys/intrinsics_common.go:28:5: ntz8tab redeclared in this block
	/usr/local/go/src/runtime/internal/sys/intrinsics.go:25:7: other declaration of ntz8tab
/usr/local/go/src/runtime/internal/sys/intrinsics_common.go:48:6: Len64 redeclared in this block
	/usr/local/go/src/runtime/internal/sys/intrinsics.go:99:6: other declaration of Len64
/usr/local/go/src/runtime/internal/sys/intrinsics_common.go:66:7: m0 redeclared in this block
	/usr/local/go/src/runtime/internal/sys/intrinsics.go:117:7: other declaration of m0
/usr/local/go/src/runtime/internal/sys/intrinsics_common.go:67:7: m1 redeclared in this block
	/usr/local/go/src/runtime/internal/sys/intrinsics.go:118:7: other declaration of m1
/usr/local/go/src/runtime/internal/sys/intrinsics_common.go:68:7: m2 redeclared in this block
	/usr/local/go/src/runtime/internal/sys/intrinsics.go:119:7: other declaration of m2
/usr/local/go/src/runtime/internal/sys/intrinsics_common.go:68:7: too many errors
# math
/usr/local/go/src/math/acosh.go:43:6: Acosh defined in both Go and assembly
/usr/local/go/src/math/asin.go:20:6: Asin defined in both Go and assembly
/usr/local/go/src/math/asin.go:58:6: Acos defined in both Go and assembly
/usr/local/go/src/math/asinh.go:40:6: Asinh defined in both Go and assembly
/usr/local/go/src/math/atan.go:96:6: Atan defined in both Go and assembly
/usr/local/go/src/math/atan2.go:30:6: Atan2 defined in both Go and assembly
/usr/local/go/src/math/atanh.go:48:6: Atanh defined in both Go and assembly
/usr/local/go/src/math/cbrt.go:26:6: Cbrt defined in both Go and assembly
/usr/local/go/src/math/erf.go:189:6: Erf defined in both Go and assembly
/usr/local/go/src/math/erf.go:274:6: Erfc defined in both Go and assembly
/usr/local/go/src/math/erf.go:274:6: too many errors
github.com/tebeka/snowball: build constraints exclude all Go files in /home/ubuntu/go/pkg/mod/github.com/tebeka/[email protected] ```

Specs of the Swedish stemmer are badly formed

Hello!

I implemented the Swedish stemmer according to the "specs", and it seems to work fine according to the tests on that very web page. The issue I'm having is that those specs performs badly in real situations.

For example

gevär -> gevär
(gun) -> (gun)
geväret   -> geväret
(the gun) -> (the gun)

I you were to ask me what the word stem of the determined form of the word "geväret" is, I would answer the undetermined form "gevär". This is not a one-off type of deal, but the rules do not seem to handle determined forms of nouns in general, which I would consider to be one of the first things to implement in a stemmer in Swedish.

I could extend the rules and break from the "specs" or keep to the specs, and accept that the results are not satisfactory.

Which way do you think I should go?

Support for additional languages

Hello,

Are the authors of this package interested in supporting additional languages? I am interested in German, Mandarin and Czech myself. (I realize that Mandarin doesn't have inflection in the IE sense, but it does have the equivalent of stop words and particles that attach themselves as pseudo-affixes.)

The structure of this library looks easily extensible - would you accept PRs that add support for the languages listed above?

Better yet, do you maybe already have plans to support some of them?

Best,
Adam

"iOS"

Is it possible to return "ios" (instead of "io" currently) as the stem of "iOS"?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.