stopword
stopword
is a module for node and the browser that allows you to strip
stopwords from an input text. Covers 62 languages. In natural language processing, "Stopwords" are words that are so frequent that they can safely be removed from a text without altering its meaning.
All .min
minified files are approximately 130 Kb each.
Breaking change!
Language codes are changed from ISO-639-1
(two characters) to ISO-639-3
(three characters). This to have room for more small languages that wasn't specified in ISO-639-1
.
If you haven't specified any stopword lists and just gone with the default (which is English), it should continue working without the need for any change.
Getting the script in your environment
CJS - CommonJS
Deconstruction require:
const { removeStopwords, eng, fra } = require('stopword')
// 'removeStopwords', 'eng' and 'fra' available
Old style require:
const sw = require('stopword')
// sw.removeStopwords and sw.<language codes> now available
ESM - Ecmascript Modules
Deconstruction import:
import { removeStopwords, eng, fra } from './dist/stopword.esm.mjs'
// 'removeStopwords', 'eng' and 'fra' available
Old style import:
import * as sw from './dist/stopword.esm.mjs'
// 'sw.removeStopwords' + 'sw.<language codes>' available
UMD - Script tag method
<script src="https://cdn.jsdelivr.net/npm/stopword/dist/stopword.umd.min.js"></script>
<script>
// sw.removeStopwords and sw.<language codes> now available
</script>
TypeScript
TypeScript with CJS
import * as sw from 'stopword'
// 'sw.removeStopwords' + 'sw.<language codes>' available
TypeScript with ESM
import { removeStopwords, eng, fra } from 'stopword'
// 'removeStopwords', 'eng' and 'fra' available
Install TypeScript types
npm i @types/stopword --save-dev
Usage
Default (English)
By default, stopword
will strip an array of "meaningless" English words
const { removeStopwords } = require('stopword')
const oldString = 'a really Interesting string with some words'.split(' ')
const newString = removeStopwords(oldString)
// newString is now [ 'really', 'Interesting', 'string', 'words' ]
Other languages
You can also specify a language other than English:
const { removeStopwords, swe } = require('stopword')
const oldString = 'TrΓ€dgΓ₯rdsΓ€gare Γ€r beredda att prΓΆva vad som helst fΓΆr att bli av med de hatade mΓΆrdarsniglarna Γ₯Àâ'.split(' ')
// swe contains swedish stopwords
const newString = removeStopwords(oldString, swe)
// newString is now [ 'TrΓ€dgΓ₯rdsΓ€gare', 'beredda', 'prΓΆva', 'helst', 'hatade', 'mΓΆrdarsniglarna', 'Γ₯Àâ' ]
Numbers
Extract numbers (korean script/characters) with module words-n-numbers
and removing 0-9 'stopwords'
const { removeStopwords, _123 } = require('stopword')
const { extract, words, numbers } = require('words-n-numbers')
const oldString = 'μΎ°λ₯Έ λμ±λΉ(λ
μΌμ΄: KΓΆlner Dom, μ μ λͺ
μΉ: Hohe Domkirche St. Peter)μ λ
μΌ μΎ°λ₯Έμ μλ λ‘λ§ κ°ν¨λ¦κ΅νμ μ±λΉμ΄λ€. κ³ λ μμμΌλ‘ μ§μ΄μ‘λ€. μΎ°λ₯Έ λκ΅κ΅¬μ μ£Όκ΅μ’ μ±λΉμ΄λΌ μΎ°λ₯Έ μ£Όκ΅μ’ μ±λΉμ΄λΌκ³ λ λΆλ¦°λ€. νμ¬ μΎ°λ₯Έ λκ΅κ΅¬μ κ΅κ΅¬μ₯μ λΌμ΄λ λ§λ¦¬μ λ΅ν€ μΆκΈ°κ²½μ΄λ€. μ΄ μ±λΉμ λ
μΌμμ κ°μ₯ μ μλ €μ§ κ±΄μΆλ¬Όλ‘, μ± λ°μ€λ¦¬ λμ±λΉμ μ΄μ΄, 1996λ
μ λ€μ€μ½ μΈκ³μ μ°μΌλ‘ λ±μ¬λμλ€. μ λ€μ€μ½μμλ μΎ°λ₯Έ λμ±λΉμ μΌμ»¬μ΄ βμΈλ₯μ μ°½μ‘°μ μ¬λ₯μ 보μ¬μ£Όλ λλ¬Έ μνβμ΄λΌκ³ λ¬μ¬νμλ€.[1] λ§€μΌ 2λ§μ¬ λͺ
μ κ΄κ΄κ°μ΄ μ΄ μ±λΉμ μ°Ύλλ€.[2]'
let newString = extract(oldString, { regex: [numbers] })
newString = removeStopwords(newString, _123)
// newString is now [ '1996' ]
})
Custom list of stopwords
And last, but not least, it is possible to use your own, custom list of stopwords:
const { removeStopwords } = require('stopword')
const oldString = 'you can even roll your own custom stopword list'.split(' ')
// Just add your own list/array of stopwords
const newString = removeStopwords(oldString, [ 'even', 'a', 'custom', 'stopword', 'list', 'is', 'possible']
// newString is now [ 'you', 'can', 'roll', 'your', 'own']
Removing stopwords for i.e. two languages and a custom stopword list
With spread syntax you can easily combine several stopword arrays into one. Useful for situations where two langauages are used interchangeably. Or when you have certain words that are used in every document that is not in your existing stopword arrays.
const { removeStopwords, eng, swe } = require('stopword')
const oldString = 'a really interesting string with some words trΓ€dgΓ₯rdsΓ€gare Γ€r beredda att prΓΆva vad som helst fΓΆr att bli av med de hatade mΓΆrdarsniglarna'.split(' ')
const customStopwords = ['interesting', 'really']
const newString = sw.removeStopwords(oldString, [...eng, ...swe, ...customStopwords]
// newString is now ['string', 'words', 'trΓ€dgΓ₯rdsΓ€gare', 'beredda', 'prΓΆva', 'helst', 'hatade', 'mΓΆrdarsniglarna']
API
removeStopwords
Returns an Array that represents the text with the specified stopwords removed.
text
An array of wordsstopwords
An array of stopwords
const { removeStopwords } = require('stopword')
var text = removeStopwords(text[, stopwords])
// text is now an array of given words minus specified stopwords
<language code>
Language codes follow ISO 639-3 Language Code list. Arrays of stopwords for the following 62 languages are supplied:
_123
- 0-9 for different script (regular, Farsi, Korean and Myanmar)afr
- Afrikaansara
- Arabic, Macrolanguagehye
- Armenianeus
- Basqueben
- Bengalibre
- Bretonbul
- Bulgariancat
- Catalan, Valencianzho
- Chinese, Macrolanguagehrv
- Croatiances
- Czechdan
- Danishnld
- Dutcheng
- Englishepo
- Esperantoest
- Estonian, Macrolanguagefin
- Finnishfra
- Frenchglg
- Galiciandeu
- Germanell
- Greek, Modernguj
- Gujaratihau
- Hausaheb
- Hebrewhin
- Hindihun
- Hungarianind
- Indonesiangle
- Irishita
- Italianjpn
- Japanesekor
- Koreankur
- Kurdish, Macrolanguagelat
- Latinlav
- Latvian, Macrolanguagelit
- Lithuanianlgg
- LugbaralggNd
- Lugbara, No diacriticsmsa
- Malay, Macrolanguagemar
- Marathimya
- Myanmar (Burmese)nob
- Norwegian bokmΓ₯lfas
- Persian (Farsi)pol
- Polishpor
- PortugueseporBr
- Portuguese-BrazilianpanGu
- Punjabi (Panjabi), Gurmukhi scriptron
- Romanian (Moldavian, Moldovan)rus
- Russianslk
- Slovakslv
- Sloveniansom
- Somalisot
- Sotho, Southernspa
- Spanishswa
- Swahili, Macrolanguageswe
- Swedishtgl
- Tagalog (Filipino)tha
- Thaitur
- Turkishukr
- Ukrainianurd
- Urduvie
- Vietnameseyor
- Yorubazul
- Zulu
Languages with no space between words
jpn
Japanese, tha
Thai and zho
Chinese and some of the other languages supported have no space between words. For these languages you need to split the text into an array of words in another way than just textString.split(' ')
. You can check out SudachiPy, TinySegmenter for Japanese and chinese-tokenizer, jieba, pkuseg for Chinese.
Your language missing?
If you can't find a stopword file for your language, you can try creating one with stopword-trainer
. We're happy to help you in the process.
Contributions and licenses
Most of this work is from other projects and people, and wouldn't be possible without them. Thanks to among others the stopwords-iso project and the more-stoplist project. And thanks for all your code input: @arthurdenner, @micalevisk, @fabric-io-rodrigues, @behzadmoradi, @guysaar223, @ConnorKrammer, @GreXLin85, @nanopx, @virtual, @JustroX, @DavideViolante, @rodfeal, @BLKSerene and @dsdenes!
Licenses for this library and all third party code.