如何将大量文本文件导入JavaScript数组?

时间:2014-05-08 18:36:23

标签: javascript arrays node.js

我使用a list of words with positive and negative sentiment from AFINN进行文字分析。

问题是,列表以下列格式出现在.txt文件中(左边的单词,右边的pos vs neg索引):

casualty    -2
catastrophe -3
catastrophic    -4
cautious    -1
celebrate   3
celebrated  3
celebrates  3
celebrating 3

要使用它,我需要采用以下格式:

var array = [{word:"casualty",score:-2},{word:"catastrophe",score:-3},{word:"catastrophic",score:-4}, etc etc]

我实际上更喜欢使用shell脚本执行此操作,而不是在浏览器中执行此操作。这就是为什么我认为Node.js可以在这里派上用场的原因。但我对Node不是很熟悉。

Direct link to the zip containing the raw text files.

1 个答案:

答案 0 :(得分:1)

如果你真的不关心如何将文本读入javascript数组,而你只需要JSON中的AFINN,我就找到了here版本。