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

truly-unique

v2.0.0

Published

Simple utility to get unique / repeated words from strings or phrases

Downloads

13

Readme

truly unique

This module analyze the text provided and identify unique or repeated words, the most common words in the text, the phrases with unique words across the text or the phrases with repeated words.

The module supports 'stopwords' with the {stopwords: true} argument so the most common words in English are ignored in the process. Look at the following example:

> const {mostCommonWordsCount} = require('truly-unique')
> const sample = 'In computing, stop words are words which are filtered out before or after processing of natural language data'
> mostCommonWordsCount(sample)
[ { key: 'are', value: 2 }, { key: 'words', value: 2 } ]
> mostCommonWordsCount(sample, {stopwords: true})
[ { key: 'words', value: 2 } ]

The module supports for now English only, but it will attempt to remove the accents in the foreign languages words and treat them as English as well.

Install

You can install with [npm]:

$ npm install --save truly-unique

Usage

The module provides two type of functions:

  • Functions to deal with words: (uniqueWords, repeatedWords and mostCommonWordsCount)
  • Functions to deal with phrases: (uniquePhrases, repeatedPhrases, phrasesWithUniqueWords and phrasesWithRepeatedWords)

Maybe the easiest way to understand the module is through and example. Lets get the text of the traditional Irish song Molly Malone and lets assign the lyrics to a variable, so we can start to answer questions.

> const malone = 'In Dublin\'s fair city,\nWhere the girls are so pretty,\nI first set my eyes on sweet Molly Malone,\nAs she wheeled her wheel-barrow,\nThrough streets broad and narrow,\nCrying, "Cockles and mussels, alive, alive, oh!"\n"Alive, alive, oh,\nAlive, alive, oh,"\nCrying "Cockles and mussels, alive, alive, oh".\nShe was a fishmonger\nBut sure \'twas no wonder\nFor so were her father and mother before\nAnd they each wheel\'d their barrow\nThrough streets broad and narrow\nCrying "Cockles and mussels alive, alive oh!"\nShe died of a fever,\nAnd no one could save her,\nAnd that was the end of sweet Molly Malone.\nBut her ghost wheels her barrow,\nThrough streets broad and narrow,\nCrying, "Cockles and mussels, alive, alive, oh!"'

So maybe the first question you want to ask is what is the most common word on those lyrics?

> const {mostCommonWordsCount} = require('truly-unique')
> mostCommonWordsCount(malone)
[ { key: 'alive', value: 10 },
  { key: 'and', value: 8 },
  { key: 'cockles', value: 4 }
  ...

// But if you want to remove the stopwords like 'and'
> const {mostCommonWordsCount} = require('truly-unique')
> mostCommonWordsCount(malone, {stopwords: true})
[ { key: 'alive', value: 10 },
  { key: 'cockles', value: 4 },
  { key: 'mussels', value: 4 },

Now lets say you want to find what words are unique on that text:

> const {uniqueWords} = require('truly-unique')
> uniqueWords(malone)
[ 'in',
  'dublins',
  'fair',
  'city',
  'where',
  'girls',
  'are',
  'pretty',
  'i',
  'first',
  ...

  // But if you want to remove the stopwords like 'i'
> uniqueWords(malone, {stopwords: true})
[ 'dublins',
  'fair',
  'city',
  'girls',
  'pretty',
  'first',
  'set',
  'eyes',
  'wheeled',
  'wheelbarrow',
  'fishmonger',

The next step could be what are the unique phrases?

> const {uniquePhrases} = require('truly-unique')
> uniquePhrases(malone)
[ 'In Dublin\'s fair city,',
  'Where the girls are so pretty,',
  'I first set my eyes on sweet Molly Malone,',
  'As she wheeled her wheel-barrow,',
  ...

Or repeated phrases as well:

> const {repeatedPhrases} = require('truly-unique')
> repeatedPhrases(malone)
[ 'Through streets broad and narrow,',
  'Crying, "Cockles and mussels, alive, alive, oh!"',
  '"Alive, alive, oh,',
  'Alive, alive, oh,"',
  ...

And finally let's say you want to find if those lyrics have phrases composed of unique words across the text:

> const {phrasesWithUniqueWords} = require('truly-unique')
> phrasesWithUniqueWords(malone)
[ 'In Dublin\'s fair city,' ]

// And ommiting the stop words again

> phrasesWithUniqueWords(malone, {stopwords: true})
[ 'In Dublin\'s fair city,',
  'Where the girls are so pretty,',
  'As she wheeled her wheel-barrow,',
  'She was a fishmonger',
  'But sure \'twas no wonder',
  'For so were her father and mother before',
  'She died of a fever,',
  'And no one could save her,' ]
>

License

Copyright © 2019, Juan Convers. Released under the MIT License.