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

@wearerobos/text-juicer

v0.0.8

Published

This module extract entities from the given text using Regular Expressions

Downloads

4

Readme

Text juicer :tropical_drink:

This module extracts entities (juice :tropical_drink:) from a text string using Regex.

Entities are: e-mail, phones, numbers, dates, money, national ID...

Usage

Import Text Juicer and use it passing a text and the language it should use as a String or Array of Strings (it will iterate over them all).

If you want, you can select what "extractor" to use passing a third argument to the Text Juicer.

const textJuicer = require('text-juicer');

// Passing just text (Default -> language: 'pt-br', extractors: all)
const extracted1 = textJuicer.parseData('My text goes here');
// Selecting EN language
const extracted2 = textJuicer.parseData('My text goes here', 'en');
// Selecting 'pt-br' and 'email' extractor
const extracted3 = textJuicer.parseData('My text goes here', 'pt-br', 'email');
// Selecting lang: 'pt-br' and 'en' / extractors: 'email' and 'phone
const extracted4 = textJuicer.parseData('My text goes here', ['pt-br', 'en'], ['email', 'phone']);

What "juice" may you extract from your text?

EN

  • E-mails
  • Dates
  • Money values
  • Phone numbers
  • Zip codes
  • Numbers
  • Codes (like protocol codes)

PT-BR

  • E-mails
  • Dates
  • CPF
  • CNPJ
  • Money values
  • Phone numbers
  • CEP
  • Numbers
  • Codes (like protocol codes)

Results

Each component might have it own way of returning data, but all of them follow this default structure:

{
  "language": {
    "entity 1": [
     { // First element found for this entity
      "start": Starting index in String,
      "end": Ending index in String,
      "match": Matched text,
      "data": Extracted Data
     },
     { // Second element found for this entity
      "start": Starting index in String,
      "end": Ending index in String,
      "match": Matched text,
      "data": Extracted Data
     }
    ],
   "entity 2": [ ... ]
  },
  "language 2": { ... }
}

Above are the particularities for each extractor.

Date

reference: date used as reference for parser (check Chrono documentation: https://github.com/wanasit/chrono).

type: if data is a single date (plain) or a range (range). If range, there will be an Array with start and end properties for the range.

"date": [
 {
  "reference": "2017-03-22T02:37:37.537Z",
  "start": 39,
  "end": 42,
  "match": "4-9",
  "type": "range",
  "data": {
   "start": "2017-03-21T07:00:00.000Z",
   "end": "2017-03-21T12:00:00.000Z"
  }
 }
]

Money

currency: currency of the value (data).

"money": [
 {
  "start": 16,
  "end": 32,
  "match": "US$ 10 e 5 cents",
  "currency": "US$",
  "data": 10.05
 }
]

Phone

data.countryCode: Phone's country code (default: +55 [Brazil]).

data.areaCode: Phone's area code.

data.phone: Phone number.

"phone": [
 {
  "start": 10,
  "end": 23,
  "match": "078 55015 680",
  "data": {
   "countryCode": "+55",
   "ddd": "07",
   "phone": "85501-5680"
  }
 }
]

Number

Numbers data is always given as an Array, since it might have a sequence of numbers.

"number": [
 {
  "start": 25,
  "end":41,
  "match": "seventy thousand 4532",
  "data": [
   70000,
   4532
  ]
 }
]

Considerations

Numbers

Numbers are always extracted last and only on the text's "residue". This is because you may have money and dates in your text, and it shouldn't be mistaken by a "number value".

textJuicer.extractData('I spent $ 54,00 on this shirt, but I will use it only in 5 days', ['en']);

// Returns date and money, but no number
{
 "en": {
  "date": [
   {
    "reference": "2017-03-22T03:16:22.130Z",
    "start": 54,
    "end": 63,
    "match": "in 5 days",
    "type": "plain",
    "data": "2017-03-27T15:00:00.000Z"
   }
  ],
  "money": [
   {
    "start": 7,
    "end": 16,
    "match": " $ 54,00 ",
    "currency": "US$",
    "data": 5400
   }
  ]
 }
}

Another important thing about numbers is that you may get written numbers! (Only in Brazilian Portuguese for now)

juiceParser.extractData('meu numero da sorte é cinquenta mil quinhentos e setenta e sete');

// Returns
{
 "pt-br": {
  "number": [
   {
    "start": 22,
    "end": 63,
    "match": "cinquenta mil quinhentos e setenta e sete",
    "data": [
     50577
    ]
   }
  ]
 }
}

Contributing

You are very welcome to contribute to Text Juicer!

We style our code based on Airbnb eslint style (check the guide: https://github.com/airbnb/javascript).

Please, test your pull requests. We are using Mocha and Chai for testing.

Any doubt, feel free to ask.

License

MIT License

Copyright (c) 2017 ROBOS.im https://robos.im

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.