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

occurences

v2.2.2

Published

Calculate the number of occurrences of each word in a text. Words smaller than two letters will be ignored.

Downloads

298

Readme

Occurrences GitHub license

Calculate the number of occurrences of each word in a text. Get varisous stats: smallest, longest words, etc.

| NPM GitHub package version npm Npm downloads | Maintenance GitHub last commit Open issues | Build Status Sonar quality gate Code Climate Coverage Status | Greenkeeper badge Dependencies Status DevDependencies Status | | --- | :--- | :---- | :----


Installation

npm i -S occurences

Usage

Javascript

const Occurrences = require('occurences');
let occ = new Occurrences(data, [options])

Where data is a string. Options object isn't required.

Running example on Runkit.com: https://runkit.com/proustibat/occurences-example-request

Typescript

Wanna use it with Angular 2? For example in an Ionic application. Import as follows:

import * as Occurences from 'Occurences';

Note that stats of an instance is an object. So to list the words in an ionic template with *ngFor, proceed as follows to transform it in an array:

Typescript file:

    this.textOccurrences = new Occurences(this.text);
    this.statsArray = Object.keys(this.textOccurrences.stats).map( key => {
        return { word: key, number: this.textOccurrences.stats[key] };
    });

HTML :

<table>
    <tr *ngFor="let item of statsArray">
        <td>{{ item.word }}</td>
        <td>{{ item.number }}</td>
    </tr>
</table>

Options

Option | Type | Default | Description ------ | ---- | ------- | ----------- sensitiveCase | Boolean | false | If defined to true, counts as 2 different words same word with uppercases ignored | String or Array | - | One or several words to ignore when counting occurrences biggerThan | int | 2 | Considers only words larger than this number of letters

Properties

Property | Type | Description -------- | ---- | ----------- stats | Object | Each words occurrences: word as key, occurence number as value (read-only) meta | Object | Global stats about the data: total number of words, number of different words, total number of characters with spaces (charsWS) or no (charsNS) Returns an object as follows: {totalWords:int, differentWords:int, charsWS:int, charsNS:int} lessUsed | Array | The less used word of the data (read-only) mostUsed | Array | The most used word of the data (read-only) smallest | Array | The smallest used word (read-only) longest | Array | The longest used word (read-only) options | Object | Settings of the instance (read-only)

Methods

Property | Parameters | Default | Description -------- | ---------- | ------- | ----------- getSorted | String: 'desc', 'asc' | 'desc' | Returns an array with objects sorted by order descendant or ascendant, each index of the array is an object as follows : {word:'three', number: '3'}

Examples

Example with latin alphabet

Simple stats

const Occurrences = require('occurences'); // note the lib is named with only one R
const latinText = "Not connected to power. Power is it good or bad. What is power? Dunno what power is but I know what it's not.";
let occurrencesLatin = new Occurrences(latinText);
console.log(occurrencesLatin.stats);

Output:

{ 
    not: 2,
    connected: 1,
    power: 4,
    good: 1,
    bad: 1,
    what: 3,
    dunno: 1,
    but: 1,
    know: 1,
    'it\'s': 1 
}

Other properties

console.log("longest: ", occurrencesLatin.longest);
console.log("smallest: ", occurrencesLatin.smallest);
console.log("lessUsed: ", occurrencesLatin.lessUsed);
console.log("mostUsed: ", occurrencesLatin.mostUsed);
console.log("getSorted: ", occurrencesLatin.getSorted());

Output:

longest:  ['connected']
smallest:  [ 'not', 'bad', 'but' ]
lessUsed:  [ 'connected', 'good', 'bad', 'dunno', 'but', 'know', 'it\'s' ]
mostUsed:  ['power']
getSorted:  [ { value: 'power', number: 4 },
  { value: 'what', number: 3 },
  { value: 'not', number: 2 },
  { value: 'connected', number: 1 },
  { value: 'good', number: 1 },
  { value: 'bad', number: 1 },
  { value: 'dunno', number: 1 },
  { value: 'but', number: 1 },
  { value: 'know', number: 1 },
  { value: 'it\'s', number: 1 } ]

Example with hebrew alphabet

const Occurrences = require('occurences'); // note the lib is named with only one R
const hebrewText = "שלום! חג פסח שמח ו שבת שלום לכולם!";
let occurrencesHebrew = new Occurrences(hebrewText);
console.log(occurrencesHebrew.stats);

Output:

{ 
	'שלום': 2, 
	'פסח': 1, 
	'שמח': 1, 
	'שבת': 1, 
	'לכולם': 1 
}

Note that text editor don't outputs from left to right but the object is ok in real life

Example with async data

const Occurrences = require('occurences');  // note the lib is named with only one R
const request = require('request');         // note you have to install request lib
const url = "http://faker.hook.io/?property=lorem.sentences";
request({
    url: url,
    json: true
}, function (error, response, data) {
    if (!error && response.statusCode === 200) {
        let myResult = new Occurrences(data);
        console.log(myResult.stats);
    }
    else {
        console.log("It seems an error occured when requesting ", url);
    }
});

Output:

{ 
    nobis: 1,
    quam: 1,
    sapiente: 1,
    fugiat: 1,
    cumque: 2,
    nisi: 1,
    voluptatem: 1,
    sint: 1,
    quibusdam: 1,
    impedit: 1,
    modi: 2,
    expedita: 1,
    deserunt: 1,
    non: 1 
}

Tests

npm test

Coverage

npm run cover

Continuous Code Quality

I use Sonarqube on Sonarcloud.io to maintain clean code. Public dashboard is here: https://sonarcloud.io/dashboard?id=proustibat_occurences

Some results:

Comments (%) Open issues Code smells Technical debt Bugs Reliability remediation effort Coverage

Using Sonar Scanner

Be sure you have downloaded and installed the Sonarqube Scanner. You need to add sonar-project.properties to the root of the project as folllows:

sonar.projectName=Occurences
sonar.projectKey=proustibat_occurences
sonar.host.url=https://sonarcloud.io
sonar.organization=proustibat-github
sonar.login=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
sonar.sources=.
sonar.exclusions=node_modules/**/*,coverage/**/*,example.js,test/**/*
sonar.javascript.lcov.reportPath=coverage/lcov.info
sonar.java.source=1.8
sonar.java.binaries=.

And then run sonar scanner as follows:

sonar-scanner -X -Dsonar.projectVersion=x.x.x

More information on Sonarcloud.io

Contributing