首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据起始值和结束值将数组分组到第一次迭代,并减少

如何根据起始值和结束值将数组分组到第一次迭代,并减少
EN

Stack Overflow用户
提问于 2022-07-21 10:47:03
回答 1查看 35关注 0票数 1

在下面的句子中,在Asteroid A at 0d at 7一词中,space is counted as 8之后的位置。为了一张清晰的照片,我下面有很多空间。

代码语言:javascript
复制
 // 0-7,     9-10,  12,  14-18, 20-26,   28-31,  33-37,  39-41,  43-46
 // Asteroid is      a   rocky  objects  that    orbit   the     Sun

现在,我在12 - 19这里有一个带有word 12 - 19的对象,19th是空白。

代码语言:javascript
复制
   {
      "start_offset": 12,
      "end": 19,
      "text": "a rocky",
      "entity_type": "adjective",
    },

现在,我应该找到这个start and end之间的所有单词,并将其推入上面的splits键下的对象中,如下所示。

代码语言:javascript
复制
           {
              "start_offset": 12,
              "end": 19,
              "text": "a rocky",
              "entity_type": "adjective",
              "splits": [
                {
                  "start_offset": 14,
                  "end": 19,
                  "text": "rocky",
                  "entity_type": "adjective",
                },
              ]
            }, 

这个迭代,我需要做n次,最后应该分组所有的元素给出的输出。

现在,我已经尝试了像下面这样的接近的结果,但仍然需要很多改进。有人能指点我吗?

代码语言:javascript
复制
const res = arr.reduce((pv, cv) => {
    const [{ start_offset, end }] = arr
      .filter((s) => (s.start_offset <= cv.start_offset) && (s.end >= cv.end))
      .sort((s1, s2) => (s2.end - s2.start_offset) - (s1.end - s1.start_offset));
      
    const hash = `${start_offset}-${end}`;
    pv[hash] = pv[hash]
      ? { ...pv[hash], splits: [...pv[hash].splits, cv] }
      : { start_offset, end, splits: [cv] };
    
    return pv;
}, {});
const result = Object.values(res);
console.log(result)

给定输入:

代码语言:javascript
复制
let arr = [
    {
      "start_offset": 0,
      "end": 38,
      "text": "Asteroid is a rocky objects that orbit",
      "entity_type": "adjective",
    },
    {
      "start_offset": 12,
      "end": 19,
      "text": "a rocky",
      "entity_type": "adjective",
    },
    {
      "start_offset": 14,
      "end": 27,
      "text": "rocky objects",
      "entity_type": "adjective",
    },
    {
      "start_offset": 20,
      "end": 32,
      "text": "objects that",
      "entity_type": "adjective",
    },
    {
      "start_offset": 14,
      "end": 19,
      "text": "rocky",
      "entity_type": "adjective",
    },
    {
      "start_offset": 20,
      "end": 27,
      "text": "objects",
      "entity_type": "adjective",
    },
    {
      "start_offset": 33,
      "end": 47,
      "text": "orbit the Sun",
      "entity_type": "adjective",
    },
    {
      "start_offset": 43,
      "end": 47,
      "text": "Sun",
      "entity_type": "adjective",
    }
  ]

预期输出:

代码语言:javascript
复制
  let output = [
    {
      "start_offset": 0,
      "end": 38,
      "text": "Asteroid is a rocky objects that orbit",
      "entity_type": "adjective",
      "splits": [
            {
              "start_offset": 12,
              "end": 19,
              "text": "a rocky",
              "entity_type": "adjective",
              "splits": [
                {
                  "start_offset": 14,
                  "end": 19,
                  "text": "rocky",
                  "entity_type": "adjective",
                },
              ]
            },
            {
              "start_offset": 14,
              "end": 27,
              "text": "rocky objects",
              "entity_type": "adjective",
              "splits": [
                {
                  "start_offset": 20,
                  "end": 27,
                  "text": "objects",
                  "entity_type": "adjective",
                },
              ]
            },
            {
              "start_offset": 20,
              "end": 32,
              "text": "objects that",
              "entity_type": "adjective",
            },
      ]
    },
    {
      "start_offset": 33,
      "end": 47,
      "text": "orbit the Sun",
      "entity_type": "adjective",
    },
    {
      "start_offset": 43,
      "end": 47,
      "text": "Sun",
      "entity_type": "adjective",
    }
 ]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-21 11:19:02

您可以预先排序数组,并通过查找较小级别的开始范围和结束范围来减少数组,直到找到子级别为止。

代码语言:javascript
复制
const
    data = [{ start_offset: 0, end: 38, text: "Asteroid is a rocky objects that orbit", entity_type: "adjective" }, { start_offset: 12, end: 19, text: "a rocky", entity_type: "adjective" }, { start_offset: 14, end: 27, text: "rocky objects", entity_type: "adjective" }, { start_offset: 20, end: 32, text: "objects that", entity_type: "adjective" }, { start_offset: 14, end: 19, text: "rocky", entity_type: "adjective" }, { start_offset: 20, end: 27, text: "objects", entity_type: "adjective" }, { start_offset: 33, end: 47, text: "orbit the Sun", entity_type: "adjective" }, { start_offset: 43, end: 47, text: "Sun", entity_type: "adjective" }],
    result = data
        .sort((a, b) => a.start_offset - b.start_offset || b.end - a.end)
        .reduce((r, { ...o }) => {
            let temp,
                child = { split: r };
            do {
                temp = child;
                temp.split = temp.split || [];
                child = temp.split.find(q => q.start_offset <= o.start_offset && q.end >= o.end);
            } while (child)
            temp.split.push(o);            
            return r;
        }, []);

console.log(result);
代码语言:javascript
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73064805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档