【问题标题】:Turn words array from a document with their coordinates into sentences将带有坐标的文档中的单词数组转换为句子
【发布时间】:2017-07-30 08:00:00
【问题描述】:

我在文档中有一组带有坐标的单词,我想把它们变成句子。 我的数组输入:

  [
    {
        "bounds": [
          {
            "x": 10,
            "y": 10
          },
          {
            "x": 15,
            "y": 10
          },
          {
            "x": 15,
            "y": 15
          },
          {
            "x": 10,
            "y": 15
          }
        ],
        "desc": "Hey"
      },
    {
        "bounds": [
          {
            "x": 18,
            "y": 10
          },
          {
            "x": 24,
            "y": 10
          },
          {
            "x": 24,
            "y": 15
          },
          {
            "x": 18,
            "y": 15
          }
        ],
        "desc": "Name"
      },
          {
        "bounds": [
          {
            "x": 18,
            "y": 20
          },
          {
            "x": 24,
            "y": 20
          },
          {
            "x": 24,
            "y": 25
          },
          {
            "x": 18,
            "y": 25
          }
        ],
        "desc": "What"
      },
    {
        "bounds": [
          {
            "x": 18,
            "y": 20
          },
          {
            "x": 24,
            "y": 20
          },
          {
            "x": 24,
            "y": 25
          },
          {
            "x": 18,
            "y": 25
          }
        ],
        "desc": "Sup"
      }
]

程序输出应该是:

Hey Name
What Sup
  • 坐标不准确只是一个例子,算法需要处理句子中间的单词和其他极端情况。

最好的方法是什么(最好用 JavaScript 实现)?

【问题讨论】:

  • 请解释一下,您希望如何实现这一目标。用“机器学习”或“算法”标记这个问题并不能解释你想要做什么。
  • @MichaelHirschler 我正在寻找最好的方法......
  • @gal 这绝对没有增加问题。它是什么”?您想将一组单词变成句子。数组的结构是什么?你想创建什么类型的句子?
  • @victor 很抱歉造成混乱。数组在问题内容和输出中,我想根据单词位置而不是它们的含义来创建句子。我有一个 OCR 模型来查找单词位置,但我不知道如何将它们连接到句子。
  • @Lora129 完全正确!

标签: javascript algorithm machine-learning ocr document


【解决方案1】:

您可以使用哈希表并为行和位置排序,然后按此顺序获取文本。

var data = [{ bounds: [{ x: 10, y: 10 }, { x: 15, y: 10 }, { x: 15, y: 15 }, { x: 10, y: 15 }], desc: "Hey" }, { bounds: [{ x: 18, y: 10 }, { x: 24, y: 10 }, { x: 24, y: 15 }, { x: 18, y: 15 }], desc: "Name" }, { bounds: [{ x: 18, y: 20 }, { x: 24, y: 20 }, { x: 24, y: 25 }, { x: 18, y: 25 }], desc: "What" }, { bounds: [{ x: 18, y: 20 }, { x: 24, y: 20 }, { x: 24, y: 25 }, { x: 18, y: 25 }], desc: "Sup" }],
    hash = {},
    result;

data.forEach(function (a) {
    hash[a.bounds[0].y] = hash[a.bounds[0].y] || {};
    hash[a.bounds[0].y][a.bounds[0].x] = hash[a.bounds[0].y][a.bounds[0].x] || [];
    hash[a.bounds[0].y][a.bounds[0].x].push({ desc: a.desc, end: a.bounds[2] });

});

result = Object.keys(hash)
    .sort((a, b) => a - b)
    .map(k => Object.keys(hash[k])
        .sort((a, b) => a - b)
        .reduce((r, l) => [...r, ...hash[k][l].map(c => c.desc)], [])
        .join(' ')
    )
    .join('\n');
   
console.log(result);
console.log(hash);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

    猜你喜欢
    • 2010-10-11
    • 2011-11-22
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2011-06-08
    相关资源
    最近更新 更多