当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - NodeJS - all short(abbreviated) word to full word feature module

问题描述:

I want to convert some abbreviated word to full word of string sentence make simple and clear sentence in NodeJS

For Example

i'm => I am

i've => I have

w'ill => we will

lets => Let us

it's => It is

I have installed gingerbread and using this module it is checking grammar mistake only. Is any module available for processing text like this?

网友答案:

The short forms in English is called contractions. You can use contractions npm module for you purposes npm install contractions

It's quite simple to use. Quick example:

let contractions = require('contractions');

console.log(contractions.expand("I don't know who ya'll are."));
// Outputs: I do not know who you all are. 

console.log(contractions.contract("You all will have not seen this."));
// Outputs: Y'all'll'ven't seen this. 

You can find more info here https://www.npmjs.com/package/contractions

网友答案:

There's no such package for JS, however, there could be fast and hard rules for replacing contractions:

var replacements = {
    "'m":  " am",
    "'ll": " will",
    "'re": " are",
    "n't": " not",
    "'ve": " have",
}

I use it from time to time.

But the real problem is to distinguish sentences that contain "he could", "he would", "he had" - no way simple algorithm can tell the difference in "he'd". This would require some serious pattern recognition, which is accomplished only through machine learning.

This is, I suppose, the reason why such package is a no-go.


Update:

As it turns out, I was wrong about such package existence(from @itereshchenkov answer), but right about nature of meaningful contractions. Simple package that uses dictionary of replacements (no matter how big it is) can't correctly decide which full form is used without understanding context.

网友答案:
var replacements = {
  "I'm": "I am",
  "we'll": "we will"
}

var sentence = "I'm sure we'll have a good time."

for (var k in replacements) {
  sentence = sentence.replace(k, replacements[k])
}

console.log(sentence)
分享给朋友:
您可能感兴趣的文章:
随机阅读: