NodeJS - 所有短(缩写)词到全词功能模块

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

我想将一些缩写词转换为字符串句子的完整单词,在 NodeJS 中制作简单明了的句子

例如

i'm => I am
i've => I have
w'ill => we will
lets => Let us
it's => It is

我已经安装了 gingerbread 并使用这个模块,它只检查语法错误。是否有任何模块可用于处理这样的文本?

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)

没有这样的 JS 包,但是,可能有快速和硬性的规则来替换 contractions:

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

我偶尔会用到它。

但真正的问题是区分包含 "he could"、"he would"、"he had" 的句子——简单的算法无法分辨 "he'd" 中的区别。这将需要一些严格的模式识别,而这只能通过机器学习来完成。

我想这就是为什么这样的包裹不可行的原因。


更新:

事实证明,我 错误 关于这样的包的存在(来自 @itereshchenkov 的回答),但是 关于有意义收缩的性质。使用替换字典的简单包(无论它有多大)在不了解上下文的情况下无法正确决定使用哪种完整形式。

英语中的简称称为contractions。 您可以根据需要使用 contractions npm 模块 npm install contractions

使用起来非常简单。快速示例:

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. 

您可以在此处找到更多信息https://www.npmjs.com/package/contractions