2
\$\begingroup\$

Task: Transform sentence to n-gram and remove all punctuation

Tests:

Input: ngram('Name, + other ~ name!', 1)

Result: ['Name', 'other', 'name']

Input: ngram('Name, + other ~ name!', 2)

Result: ['Name other', 'other name']

Input: ngram('Name, + other ~ name!', 10)

Result: ['Name other name']

Input: ngram('Мене також.', 10)

Result: ['Мене також']

Code:

function ngram(data, order) {
    const regex = /[^A-Za-zА-ЯЄІЇа-яєії0-9_']/g
    let ngramsArray = []

    data = data
      .replace(regex, ' ')
      .split(' ')
      .filter(element => element.length > 0)

    if (order > data.length) {
      order = data.length
    }

    for (let i = 0; i < data.length - (order - 1); i++) {
      let subNgramsArray = []

      for (let j = 0; j < order; j++) {
        subNgramsArray.push(data[i + j])
      }

      ngramsArray = [...ngramsArray, subNgramsArray.join(' ')]
    }

    return ngramsArray
  }
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.