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

brazil-cities-predictor

v1.1.0

Published

A typo-tolerant predictor for Brazil's cities

Downloads

16

Readme

brazil-cities-predictor

A typo-tolerant predictor for Brazil's cities

NPM version Mantainer Issues License: Unlicense Downloads

Index

Installation

$ npm install brazil-cities-predictor

Usage

Using brazil-cities-predictor is as simple as that:

import BrazilCitiesPredictor from 'brazil-cities-predictor'

const predictor = new BrazilCitiesPredictor()

predictor.predict('pontalna')

The code above returns an array of the 5 best-rated Brazil's cities according to the query, in this case 'pontalna', and the selected strategy, which, by default, is relativeDistance (worry not, it'll be better discussed later).

E.g.:

 [
  {
    city: {
      id: '5217708',
      name: 'Pontalina',
      state: 'GO',
      processedName: 'pontalina'
    },
    distance: 1,
    relativeDistance: 0.8888888888888888
  },
  {
    city: {
      id: '3540259',
      name: 'Pontalinda',
      state: 'SP',
      processedName: 'pontalinda'
    },
    distance: 2,
    relativeDistance: 0.8
  },
 // [...]
]

If you feel like five cities is not enough (or it's too many), you can change it by passing, as the second parameter, the number of cities you want the predict function to return, like this:

predictor.predict('pontalna', 10)

Strategies

Here, strategies are different manners to calculate a city's name fitness based on the query. There are plenty ways of calculating that, but for now this library offers two, both based on Levenshtein Distance, about which you can read more here: Levenshtein Distance.

absoluteDistance

This strategy rates the cities purely by the Levenshtein Distance between its name and the query. So, using the same example used above, Pontalina is rated 1 in relation to the query 'pontalna', as they are at least 1 edit apart of each other.

In this strategy, the bigger the distance the smaller the similarity; so the cities are sorted such that those with smaller distance come first.

relativeDistance

This strategy is an attempt to improve the absoluteDistance strategy. In this one, the cities are rated by the ratio between the Levenshtein Distance and the total characters, resulting in a rating between 0-1 (inclusive).

For my purpose, this strategy performed slightly better than the absoluteDistance, thus I defined it as the default, but you're free to use any.

How to determine which strategy to use?

You can set the strategy as follows:

import BrazilCitiesPredictor, { strategies } from 'brazil-cities-predictor'

// You can set it on instantiation
const predictor = new BrazilCitiesPredictor(strategies.absoluteDistance)

// Or after instantiation
predictor.setStrategy(strategies.relativeDistance)

License

Licensed under The Unlicense