Socket
Socket
Sign inDemoInstall

persian-preprocess

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    persian-preprocess

Persian (Farsi) text pre processing (normalize, number, punctuation, white space, stop word & ...)


Version published
Weekly downloads
4
increased by33.33%
Maintainers
1
Install size
61.0 kB
Created
Weekly downloads
 

Readme

Source

persian-preprocess

Persian (Farsi) text pre processing (normalize, number, punctuation, white space, stop word & ...)

Table of contents

  • Installation
  • Setup
  • Usage
  • API Features
  • Full Sample
  • Debug Info
  • Tests

Installation

npm install --save persian-preprocess

Setup

const persianPreProcess = require('persian-preprocess');

Usage

ParameterTypeRequiredDescriptiopn
textStringYesText to process
debugBooleanYesDebug system status
const text = 'text to process';
const debug = true;
const processedText = persianPreProcess(text, debug)
    .normalize()
    .number()
    .lowercase()
    .punctuation()
    .remove()
    .stopword()
    .emoticon()
    .whitespace();
  • Above code is just a sample of all pre process methods and because some of the methods require parameters this code won't work correctly. For complete functional sample please check Full Sample

API Features

Normalization MethodsDescription
normalizeNormalization process
numberChange numbers locale
lowercaseLowercase all characters
punctuationRemove punctuation
removeRemove selected characters
stopwordRemove stop words
emoticonRemove emoticons
whitespaceRemove duplicate whitespaces
Result MethodsDescription
toStringGet processed text
toArrayGet list of all words
toUniqueGet list of unique words
getDebugGet pre process debug data

normalize

Normalization process

ParameterTypeRequiredDescriptiopn
configObjectNoNormalization config (table below)
ConfigurationTypeDescriptionSample Characters
persianbooleanNormalize persian charactersﭐ ݓ ك ﻱ
englishbooleanNormalize english charactersᗩ ℳ Ѡ ⓡ ⒵
arabicbooleanNormalize arabic charactersﷲ ﷺ
numberbooleanNormalize number characters٥ ⑩
mathbooleanNormalize math characters¼ ⅞
htmlbooleanNormalize html characters  <
punctuationbooleanNormalize punctuation charactersʕ ʔ ℅ ٪
specialbooleanNormalize special charactersᴁ lj st
  • Default value for all configurations sets is true and the normalization process will use for all of them by default
  • Setting configurations value to false will ignore normalization process for the set
// No configuration (using default value)
const processedText = persianPreProcess(text, debug).normalize();

// Default configuration (Same result with the code above)
const processedText = persianPreProcess(text, debug).normalize({
    persian: true,
    english: true,
    arabic: true,
    number: true,
    math: true,
    html: true,
    punctuation: true,
    special: true
});

// Ignore HTML characters normalization
const processedText = persianPreProcess(text, debug).normalize({
    html: false
});

number

Change numbers locale

ParameterTypeRequiredDescriptiopn
languageEnum: 'persian', 'english'YesNumeric characters locale
// 0, 1 ... 9
const processedText = persianPreProcess(text, debug).number('english');

// ۰, ۱, ... ۹
const processedText = persianPreProcess(text, debug).number('persian');

lowercase

Lowercase all characters

// 'Hello' => 'hello'
const processedText = persianPreProcess(text, debug).lowercase();

punctuation

Remove punctuation

ParameterTypeRequiredDescriptiopn
configObjectNoPunctuation removal config (table below)
ConfigurationTypeDescriptionSample Characters
basicboolean or nullBasic punctuations' " \ / , ( | )
markboolean or nullSpecial punctuations\r \n \t \0
diacriticboolean or nullArabic diacriticsٌ ٍ ً ّ
unicodeboolean or nullUnicode punctuationsZERO WIDTH NON-JOINER
  • Default value for all configurations sets is true and the punctuations will remove using space character
  • Setting value to null will remove the punctuations and wont replace them with any character
  • Setting value to false will ignore punctuations removal for the set
// No configuration (using default value)
const processedText = persianPreProcess(text, debug).punctuation();

// Default configuration (Same result with the code above)
const processedText = persianPreProcess(text, debug).punctuation({
    basic: true,
    mark: true,
    diacritic: true,
    unicode: true
});

// Ignore UNICODE punctuation removal
const processedText = persianPreProcess(text, debug).punctuation({
    unicode: false
});

/**
 * Using NULL as configuration value for basic punctuations
 *
 * Using true (default value): 'in-line' > 'in line'
 * Using null                : 'in-line' > 'inline'
 */
const processedText = persianPreProcess(text, debug).punctuation({
    basic: null
});

remove

Remove selected characters

ParameterTypeRequiredDescriptiopn
configObjectNoCharacter removal config (table below)
ConfigurationTypeDescriptionSample Characters
numberboolean or nullNumeric characters0 9 ۰ ۹
persianboolean or nullPersian charactersآ ا ی
englishboolean or nullEnglish charactersA Z a z
lengthnumberWords with specific length
  • for number, persian and english configurations

    • Default value is false and the character removal process will be ignored by default
    • Setting value to true will remove all the chacters in set and replace them with space character
    • Setting value to null will remove the characters and wont replace them with any character
  • Setting length configuration will remove all words with the length equal or less than given value

/**
 * No configuration (using default value)
 * Using method with no configuration wont make any changes to text
 */
const processedText = persianPreProcess(text, debug).remove();

// Removing all characters
const processedText = persianPreProcess(text, debug).remove({
    number: true,
    persian: true,
    english: true
});

/**
 * Using NULL as configuration value for english characters
 *
 * Using true : 'in-line' > 'in line'
 * Using null : 'in-line' > 'inline'
 */
const processedText = persianPreProcess(text, debug).remove({
    english: null
});

/**
 * Using length configuration
 * 'this is a text' > 'this   text'
 */
const processedText = persianPreProcess(text, debug).remove({
    length: 2
});

stopword

Remove stop words

ParameterTypeRequiredDescriptiopn
configObjectNoStopword removal config (table below)
ConfigurationTypeDescriptionSample Words
persianbooleanPersian stopwordsدر با به
englishbooleanEnglish stopwordsin at on
customstring[]List of custom Words
  • for persian and english configurations

    • Default value is false and the stopwords removal process will be ignored by default
    • Setting value to true will remove all the stopwords in set
  • Setting custom configuration will remove all words in given list

/**
 * No configuration (using default value)
 * Using method with no configuration wont make any changes to text
 */
const processedText = persianPreProcess(text, debug).stopword();

// Removing all stopwords
const processedText = persianPreProcess(text, debug).stopword({
    persian: true,
    english: true
});

/**
 * Using custom list configuration
 * 'this is a text' > ' is  text'
 */
const processedText = persianPreProcess(text, debug).stopword({
    custom: ['this', 'a']
});

emoticon

Remove emoticons

ParameterRequiredDescriptiopn
replaceNoValue of this parameter can only be NULL
  • Be default (calling method with no parameter) all emoticons will remove using space character
  • Setting replace value to null will remove the emoticons and wont replace them with any character
// No configuration (using default value)
const processedText = persianPreProcess(text, debug).emoticon();

/**
 * Using NULL as replace parameter value
 *
 * No parameter : 'I💓U' > 'I U'
 * Using null   : 'I💓U' > 'IU'
 */
const processedText = persianPreProcess(text, debug).emoticon(null);

whitespace

Remove duplicate whitespaces

// 'this is     a  text    ' => 'this is a text '
const processedText = persianPreProcess(text, debug).whitespace();

toString

Get processed text

/**
 * text   : '❶ text and ❶ number'
 * result : '1 text and 1 number'
 */
const stringValue = processedText.toString();

toArray

Get list of all words

/**
 * text   : '❶ text and ❶ number'
 * result : ['1', 'text', 'and', '1', 'number']
 */
const arrayList = processedText.toArray();

toUnique

Get list of unique words

/**
 * text   : '❶ text and ❶ number'
 * result : ['1', 'text', 'and', 'number']
 */
const uniqueList = processedText.toUnique();

getDebug

Get pre process debug data

// See Full Sample
const debugInfo = processedText.getDebug();

Full Sample

const text = `
    استفاده از حرف ك عربی و کاراکتر خاص ﷼ و عدد عربی ٦
    انگلیسی: using ß character and ⅜ and <  ⒄℅
    حط دوم انگلیسی: and special character: NJ
    شکلک:  😃 👦🏿 🚩 👱🏽 🍉 🏒 🚍 🥬
    انتهای متن
`;

const persianPreProcess = require('persian-preprocess');
const processedText = persianPreProcess(text, true)
    // Normalize
    .normalize()
    // Change number locale to persian
    .number('persian')
    // Lowercase all characters
    .lowercase()
    // Remove all punctuation except marks (i.e.: \n)
    .punctuation({
        mark: false
    })
    // Remove all numeric characters (not using space space character)
    .remove({
        number: null,
    })
    // Remove persian, english and two custom stop words
    .stopword({
        custom: ['حرف', 'خط']
    })
    // Remove emoticons
    .emoticon()
    // Remove duplicate whitespaces
    .whitespace();

const result = processedText.toString();
/**
استفاده عربی کاراکتر خاص ریال عدد عربی
 انگلیسی using b character
 انگلیسی special character nj
 شکلک
 انتهای متن
*/

console.log(processedText.getDebug());
/**
 * Setting debug parameter as true for persianPreProcess
 * will activate debug system and debug data will be like:
 */
{
  TOTAL: { duration: 0.054, change: -96, length: 212 },
  normilize: {
    duration: 0.018,
    change: 4,
    length: 216,
    match: [
      'ك',    'ß', '﷼',
      '٦',    '⒄', '⅜',
      '<', '℅', 'NJ'
    ]
  },
  number: { duration: 0.001, change: 0, length: 216, match: [] },
  lowercase: { duration: 0, change: 0, length: 216 },
  punctuation: {
    duration: 0,
    change: 0,
    length: 216,
    match: [ ':', '/', '<', '%' ]
  },
  remove: {
    duration: 0,
    change: -5,
    length: 211,
    match: [ '۶', '۳', '۸', '۱', '۷' ]
  },
  stopword: {
    duration: 0.028,
    change: -6,
    length: 205,
    match: [
      'and',  ' دوم ',
      ' از ', ' ک ',
      ' و ',  ' حرف ',
      ' حط '
    ]
  },
  emoticon: {
    duration: 0.002,
    change: -10,
    length: 195,
    match: [
      '😃', '👦', '🏿',
      '🚩', '👱', '🏽',
      '🍉', '🏒', '🚍',
      '🥬'
    ]
  },
  whitespace: { duration: 0.001, change: -79, length: 116 }
}

Debug Info

NameDescription
durationProcess time in millisecond
changeNumber of characters added or removed from Text value
lengthText value length after process
matchList of matched characters/words in process

Tests

git clone https://github.com/webilix/persian-preprocess.git
npm install
npm test

Keywords

FAQs

Last updated on 26 Jul 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc