如何将非结构化数据转换为结构化数据?

时间:2021-05-05 10:08:54

标签: javascript json algorithm

我正在尝试编写一个将非结构化数据转换为结构化数据的 javaScript 程序。 问题看起来像这样。 The contexts are chosen by me and the aim of the program is to find the data that are related to the context

上下文由我选择,程序的目的是找到与上下文相关的数据。目前我一直在使用正则表达式来提取相关信息,如 RAM、ppi 等,但大多数时候它不起作用。还有什么可以帮助我解决这个问题的吗? 这是我写的东西:

//modules
const fs = require('fs');

//main code section
function extractInfo(sentence,context)
{
    var regex= / /gm;
    if(context==='processor')
        regex =/\w{1,}.\w{1,}.\d{1,}/gm;
    else if(context==='RAM')
        regex= /\d{1,}GB/gm;
    else if(context==='price')
        regex= /Rs. \d{1,}.\d{1,}/gm;
    else if(context==='camera')
        regex = /\d{1,}.megapixel/gm;
    else if(context==='storage')
        regex = /\d{1,}GB/gm;


    let match = regex.exec(sentence);
    if(match)
    {
        return(match[0]);
    }
    else
    {
        return('null');
    }
}
 const context = ['price','screen size','pixel density','processor','GPU','camera','battery capacity','RAM','storage'];
 var paragraph = fs.readFileSync("./text.txt").toString('utf-8').replace(/\n/g, '');
// //paragraph = paragraph.toLowerCase();
var sentences = paragraph.replace(/([.?!])\s*(?=[A-Z])/g, "$1|").split("|");
sentences.forEach(item=>{
 for(var i=0;i<context.length;i++)
    {
        if(item.includes(context[i]))
            {
                console.log(context[i]+" : "+ extractInfo(item,context[i]));
             }
        }
     });

使用它,我可以获得这样的输出,稍后我会将其放入 json 文档中(目前我只专注于算法):

$ node app.js 
processor : Qualcomm Snapdragon 865
RAM : 12GB
camera : 108-megapixel
camera : null
camera : 25-megapixel
storage : 256GB
price : Rs. 64,299

0 个答案:

没有答案