确认文档的出现次数 [英] Confirm number of occurrences of a document

查看:32
本文介绍了确认文档的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建程序以根据我设置的规则检查文档的出现次数时遇到了巨大的困难.在正则表达式的帮助下,我检查了一些字段,如果某个特定字段存在,我可以计算它出现的次数,或者我创建一个更深入的扫描.有点乱,不知道具体怎么解释.

我正在检查文本文件,但为了降低复杂性,我将使用数组.

我有以下数组:

let 字符串 = ['公司:姓名 ID:12','公司:姓名 ID:12','公司:姓名 ID:12','公司:NAME2 ID:10'];

这是欲望输出:

<代码>{'姓名':{'12':3},'NAME2':{'10':1}}

为了实现这一点,我需要做一些检查,所以我想出了以下地图":

let 模式 = [{'pattern': 'COMPANY:\\s*?([\\w]+)\\s','修饰符':''},{'模式' : 'ID:\\s*?(\\d{2})\\s*','修饰符':''}];

我很难创建伪代码,我知道它可以递归完成,但我被卡住了.最大的问题是因为嵌套,我可以有多层嵌套,不一定是两层.

在过去的几个小时里,我创建了以下代码:

'use strict';让模式 = [{'pattern': 'COMPANY:\\s*?([\\w]+)\\s','修饰符':''},{'模式' : 'ID:\\s*?(\\d{2})\\s*','修饰符':''}];让字符串 = ['公司:姓名 ID:12','公司:姓名 ID:12','公司:姓名 ID:12','公司:NAME2 ID:10'];var_data = {};for(让一串字符串){var root = _data;for (let i = 0, length = patterns.length; i < length; i++) {让 item = patterns[i];let regex = new RegExp(item.pattern, item.modifier);让结果 = regex.exec(string);if (i < patterns.length -1) {root = root[result[1]] = {};} 别的 {根=根[结果[1]] = 1;}}}document.body.innerHTML = JSON.stringify({_data});

现在我正在尝试获取最后一部分,计算出现的次数,这很麻烦.也许递归或生成器可以解决这个问题.

更新 -

重要的是要了解应该使用 3、4、5 个对象.示例:

let 模式 = [{'pattern': 'COMPANY:\\s*?([\\w]+)\\s','修饰符':''},{'模式' : 'ID:\\s*?(\\d{2})\\s*','修饰符':''},{'模式' : '东西:\\s*?(\\d+)\\s*','修饰符':''}];让字符串 = ['公司:姓名 ID:12 某事:1010','公司:姓名 ID:12 某事:1010','公司:姓名 ID:12 某事:1010','公司:NAME2 ID:10 某事:1010'];

输出应该是:

<代码>{'名称': {'12':{1010":3}},'NAME2':{'10':{'1010':1}}}

解决方案

您可以这样做.Array.prototype.reduce() 对于这些工作非常方便.

var strings = ['公司:姓名 ID:12','公司:姓名 ID:12','公司:姓名 ID:12','公司:NAME2 ID:10'],Reduced = strings.reduce((p,c) => {var co = c.match(/\w+(?=\s*ID)/)[0],id = c.match(/\d+$/)[0];p[co] ?p[co][id]++ : p[co] = {[id]:1};返回 p},{});document.write("<pre>" +JSON.stringify(reduced,null,2) + "</pre>");

所以现在我修改了代码以使用无限的嵌套属性.我不得不使用两个我的发明对象方法Object.prototype.getNestedValue()Object.prototype.setNestedValue() 用于通过动态提供的参数访问和设置/修改嵌套对象属性及其值.提供的最后一个参数是要获取或设置的值.previopus 参数是嵌套的属性.对于这些用例,它们是非常方便的方法.就这样吧

Object.prototype.getNestedValue = function(...a) {返回 a.length >1 ?(this[a[0]] !== void 0 && this[a[0]].getNestedValue(...a.slice(1))) : this[a[0]];};Object.prototype.setNestedValue = function(...a) {a.长度>2 ?typeof this[a[0]] === "object" &&this[a[0]] !== null ?this[a[0]].setNestedValue(...a.slice(1)):(this[a[0]] = typeof a[1] === "string" ? {} : new Array(a[1]),this[a[0]].setNestedValue(...a.slice(1))): 这个[a[0]] = a[1];返回这个;};var 字符串 = ['公司:姓名 ID:12 某事:1010 更多:857','公司:姓名 ID:12 某事:1010 更多:857','公司:姓名 ID:12 某事:1010 更多:857','公司:NAME2 ID:10 东西:1010 更多:333'],Reduced = strings.reduce((p,c) => {var props = c.match(/(?::\s*)[^\s]+/g).map(e => e.split(":")[1].trim()),value = p.getNestedValue(...props);!!价值 ?p.setNestedValue(...props,++value) : p.setNestedValue(...props,1);返回 p},{});document.write("

" + JSON.stringify(reduced,null,2) + "

");

I'm having a huge difficulty in creating a program to check the number of occurrences of a document based on rules set by me. With the help of regex, I check some fields, and if a particular field exists , I can count the number of occurrences of it, or I create a deeper scan. It's a little confusing, and I do not know exactly how to explain.

I 'm checking text files, but to reduce the complexity , I will use arrays.

I have the following array:

let strings = [
  'COMPANY: NAME  ID: 12',
  'COMPANY: NAME  ID: 12',
  'COMPANY: NAME  ID: 12',
  'COMPANY: NAME2 ID: 10'
];

And this is the desire output:

{
  'NAME' :  { '12': 3 },
  'NAME2':  { '10': 1 }
}

To achieve this, I need to do some checks, so I came up with the following 'MAP':

let patterns = [
  {
    'pattern': 'COMPANY:\\s*?([\\w]+)\\s',
    'modifier': ''
  },
  {
    'pattern'  : 'ID:\\s*?(\\d{2})\\s*',
    'modifier' : ''
  }
];

I 'm having a hard time creating the pseudo- code, I know it's something that can be done recursively, but I'm stuck . The biggest problem is because of nested, I can have several levels of nested, not necessarily two.

In the last hours I created the following code:

'use strict';

let patterns = [
  {
    'pattern': 'COMPANY:\\s*?([\\w]+)\\s',
    'modifier': ''
  },
  {
    'pattern'  : 'ID:\\s*?(\\d{2})\\s*',
    'modifier' : ''
  }
];

let strings = [
  'COMPANY: NAME  ID: 12',
  'COMPANY: NAME  ID: 12',
  'COMPANY: NAME  ID: 12',
  'COMPANY: NAME2 ID: 10'
];

var _data = {};
for (let string of strings) {

  var root = _data;

  for (let i = 0, length = patterns.length; i < length; i++) {

    let item   = patterns[i];

    let regex  = new RegExp(item.pattern, item.modifier);
    let result = regex.exec(string);

    if (i < patterns.length -1) {
      root = root[result[1]] = {};
    } else {
      root = root[result[1]] = 1;
    }
  }
}

document.body.innerHTML = JSON.stringify({_data});

Now i'm trying to get the last part, count the number of occurrences, which is being a pain in the ass. Maybe recursion or generator could resolve this.

UPDATE -

It's important understand that should work with 3, 4, 5, objects. Example:

let patterns = [
  {
    'pattern': 'COMPANY:\\s*?([\\w]+)\\s',
    'modifier': ''
  },
  {
    'pattern'  : 'ID:\\s*?(\\d{2})\\s*',
    'modifier' : ''
  },
  {
    'pattern'  : 'SOMETHING:\\s*?(\\d+)\\s*',
    'modifier' : ''
  }
];

let strings = [
  'COMPANY: NAME  ID: 12 SOMETHING: 1010',
  'COMPANY: NAME  ID: 12 SOMETHING: 1010',
  'COMPANY: NAME  ID: 12 SOMETHING: 1010',
  'COMPANY: NAME2 ID: 10 SOMETHING: 1010'
];

Output should be:

{
  'NAME': {
    '12': {
      '1010': 3
    }
  },
  'NAME2': {
    '10': {
      '1010': 1
    }
  }
}

解决方案

You may do like this. Array.prototype.reduce() is very handy for these jobs.

var strings = [
  'COMPANY: NAME  ID: 12',
  'COMPANY: NAME  ID: 12',
  'COMPANY: NAME  ID: 12',
  'COMPANY: NAME2 ID: 10'
],

reduced = strings.reduce((p,c) => {var co = c.match(/\w+(?=\s*ID)/)[0],
                                   id = c.match(/\d+$/)[0];
                                   p[co] ? p[co][id]++ : p[co] = {[id]:1};
                                   return p},{});
document.write("<pre>" +JSON.stringify(reduced,null,2) + "</pre>");

So now i modified the code to work with unlimited nested properties. I had to use two of my invention Object methods Object.prototype.getNestedValue() and Object.prototype.setNestedValue() which are used to access and set / modify nested object properties and their values through dynamicaly provided arguments. The last argument provided is the value to get or set. The previopus arguments are the nested properties. They are very handy methods for these use cases. So here it goes

Object.prototype.getNestedValue = function(...a) {
  return a.length > 1 ? (this[a[0]] !== void 0 && this[a[0]].getNestedValue(...a.slice(1))) : this[a[0]];
};
Object.prototype.setNestedValue = function(...a) {
  a.length > 2 ? typeof this[a[0]] === "object" && this[a[0]] !== null ? this[a[0]].setNestedValue(...a.slice(1))
                                                                       : (this[a[0]] = typeof a[1] === "string" ? {} : new Array(a[1]),
                                                                                 this[a[0]].setNestedValue(...a.slice(1)))
                      : this[a[0]] = a[1];
  return this;
};


var strings = [
  'COMPANY: NAME  ID: 12 SOMETHING: 1010 MORE: 857',
  'COMPANY: NAME  ID: 12 SOMETHING: 1010 MORE: 857',
  'COMPANY: NAME  ID: 12 SOMETHING: 1010 MORE: 857',
  'COMPANY: NAME2 ID: 10 SOMETHING: 1010 MORE: 333'
],

reduced = strings.reduce((p,c) => {var props = c.match(/(?::\s*)[^\s]+/g).map(e => e.split(":")[1].trim()),
                                       value = p.getNestedValue(...props);
                                   !!value ? p.setNestedValue(...props,++value) : p.setNestedValue(...props,1);
                                   return p},{});

document.write("<pre>" + JSON.stringify(reduced,null,2) + "</pre>");

这篇关于确认文档的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆