npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@iarna/word-count

v1.1.2

Published

Count words, with Unicode! Uses Unicode 9.0.0 character classes for improved clarity of implementation.

Downloads

1,774

Readme

@iarna/word-count

Count words, with Unicode! Uses Unicode 11.0.0 character classes for improved clarity of implementation.

const wordCount = require('@iarna/word-count')
console.log(wordCount("how many words is this?")) // 5

Specifically, we consider a word a run of 1 or more characters in these sets:

  • General_Category/Letter
  • General_Category/Letter_Number
  • General_Category/Connector_Punctuation
  • General_Category/Number
  • Word_Break/MidNum
  • Word_Break/MidNumLet
  • Word_Break/Single_Quote
  • Soft hyphens (\u00AD)
  • A singular hyphen

Multiple hyphens in a row, that is -- and --- are considered word separators as they're often used as stand-ins for endashes and emdashes respectively.

The tests make it pretty clear what it's doing:

  t.is(wordCount('This is a test'), 4, 'plain text')
  t.is(wordCount('now with 23 a number'), 5, 'integer')
  t.is(wordCount('now with 23.17'), 3, 'decimal')
  t.is(wordCount("emoji 😍😍 do not count"), 4, 'emoji')
  t.is(wordCount("possessive's are one word"), 4, 'possessive')
  t.is(wordCount('possessive’s are one word'), 4, 'possessive unicode')
  t.is(wordCount('some "quoted text" does not impact'), 6, 'quotes')
  t.is(wordCount("also 'single quotes' are ok"), 5, 'single quotes')
  t.is(wordCount("don't do contractions"), 3, 'contractions count as a single word')
  t.is(wordCount('hyphenated words-are considered whole'), 4, 'hyphenated words')
  t.is(wordCount('underbars are_too just one'), 4, 'underbars')
  t.is(wordCount('n-dash ranges 1–3 are NOT'), 6, 'en-dash')
  t.is(wordCount('m-dash connected—bits also are not'), 6, 'em-dash')

Many more naive implementations match just \w but that only get's you (some) English and even then things like possessives and, depending on how you look at, contractions get over counted.

To the best of my knowledge this should successfully count words in any language that uses word-separators. Counting words in languages without word-separators is rather harder and the heuristics are language specific.

If you happen to give this a run of, say, Chinese characters, it will consider each group outside of punctuation to be a word, massively under counting. So yeah, use a language specific counter:

For instance, for Chinese there's nseg an implementation of MMSEG.

PRIOR ART

  • word-count — A word counter that matches \w plus some ranges of CJK characters. CJK characters are each counted as one-word-per-character.
  • wordcount — A fork of the origin of word-count, it adds ranges for Cyrillic.
  • split-string-words — not a word counter per say, but you could count the words it returns. It looks for \S while allowing double quoted strings.
  • wordcount-stream — A stream based word counter that matches using \w. *
  • string_utils — Splits words on the space character.
  • wordcounter — Counts words using iso-8859-1, that is, some English and European characters.
  • string-stats — Uses a list it documents as "Latin, Greek, Coptic, Cyrillic, Armenian, Hebrew, Syriac, Arabic" though it's ad-hoc so I don't know if its complete or if that's all it has.
  • word-counter — Splits on \s. Has a run-time dependency on coffee-script.

FOR FUTURE REFERENCE

Unicode has a report relating to word boundaries that may be useful. It's focused on determining boundaries for things like double-clicking and spell check, but it's definitately a related function.