将自然语言文本转换为结构化数据

数据挖掘 nlp 结构化数据
2022-03-05 00:37:46

将自然语言文本转换为结构化数据。

我正在开发一个机器人来帮助用户帮助识别服装。问题是将自然语言文本转换为结构化数据(服装列表)并查询商店的库存以找到与每件商品最接近的匹配项。

例如,考虑以下用户对机器人的输入。

"I would like to order regular fit blue jeans with hip size 32 inches"

并且所需的输出将如下

[
  {
    "quantity": 1,
    "size": "32 inches",
    "category": "jeans",
    "attributes":[
      {"colour": "blue"},
      {"fit": "regular fit"}
    ]
  }
]

我试图通过将它分成两部分来解决这个问题。

第 1 部分:使用条件随机场 (CRF) 进行命名实体识别。- 我已经使用这里讨论的方法来标记单个令牌,并且我能够提取实体,例如apparel type,apparel size等等attributes

标记器的示例输出(表示):

I would like to order regular fit   blue   jeans     with hip size   32 inches
|                     |   |         |   |  |   |   |     |           |   |       |
+----------------------   +---------+   +--+   +---+     +-----------+   +-------+
OTHERS                    FIT           COLOR  CATEGORY  ATTR_TYPE       SIZE

第 2 部分:基于规则的语法 - 假设来自用户的查询始终是已定义实体(如类型、颜色、适合度等)的组合,我编写了规则来捕获标签序列及其各自的标记并转换将它们转换成所需的格式。

以下是一些常见序列的示例:

OTHERS ~ FIT ~ COLOR ~ CATEGORY ~ ATTR_TYPE ~ SIZE ~ OTHERS
OTHERS ~ CATEGORY ~ OTHERS ~ COLOR ~ FIT ~ OTHERS ~ SIZE
OTHERS ~ COLOR ~ CATEGORY ~ FIT ~ SIZE ~ OTHER ~ ATT_STYLE    
QTY ~ COLOR ~ ATT_MATERIAL ~ CATEGORY ~ OTHERS
COLOR ~ FIT ~ ATT_STYLE ~ CATEGORY

我做了一些假设并挖掘了频繁出现的序列来编写这些规则。

第二部分不可扩展,成为瓶颈。我不能继续添加规则来捕获额外的数据点或处理系统没有看到的新模式。

我正在寻找一种通用的解决方案/数据管道,它可以从自然语言中提取实体(关系)并将它们转换为结构化数据。

我会很感激任何想法。

更多示例有助于更好地理解问题:

示例 1:

"find jeans with black color, slim fit and size 28"

find a   jeans     with     black color,     slim fit    and       size 28
|    |   |   |     |  |     |         |      |      |    | |       |     | 
+----+   +---+     +--+     +---------+      +------+    +-+       +-----+
OTHERS   CATEGORY  OTHERS   COLOR            FIT         OTHERS    SIZE

[
  {
    "quantity": 1,
    "size": "28",
    "category": "jeans",
    "attributes":[
      {"colour": "black"},
      {"fit": "slim fit"}
    ]
  }
]

示例 2:

"I would like to find a white shirt, slim fit, XL with long sleeve, one maroon silk tie, and a black color regular fit flat front trousers"

I would like to find a  white   shirt,     slim fit, XL    with       long sleeve. 
|                    |  |   |   |   |      |      |  ||    |  |       |         |  
+--------------------+  +---+   +---+      +------+  ++    +--+       +---------+  
OTHERS                  COLOR   CATEGORY   FIT       SIZE  OTHER      ATT_STYLE    

one   maroon    silk         tie       and a 
| |   |    |    |  |         | |       |   | 
+-+   +----+    +--+         +-+       +---+ 
QTY   COLOR     ATT_MATERIAL CATEGORY  OTHERS

black color  regular fit   flat front   trousers
|         |  |         |   |        |   |      |
+---------+  +---------+   +--------+   +------+
COLOR        FIT           ATT_STYLE    CATEGORY

[
  {
    "quantity": 1,
    "size": "XL",
    "category": "shirt",
    "attributes":[
      {"colour": "white"},
      {"fit": "slim fit"},
      {"sleeve_length": "long sleeve"}
    ]
  },
  {
    "quantity": 1,
    "size": "STANDARD",
    "category": "tie",
    "attributes": [
      {"color": "maroon"},
      {"material": "silk"},
    ]
  },
  {
    "quantity": 1,
    "size": null,
    "category": "trousers",
    "attributes":[
      {"fit": "regular fit"},
      {"style": "flat front"},
      {"color": "black"}
    ]
  }
]

编辑1: 我正在尝试解析实体序列并使用规则将其转换为结构化数据。当前基于规则的系统存在局限性,例如维护规则需要熟练的专家,它们需要一直手动制作和增强。有没有办法使用机器学习来克服这些限制?用基于 ML 的解析器替换基于规则的解析器?

1个回答
  1. 准备一个 Excel 表格,其中的列有 - 句子、颜色、材料、适合、样式、袖长等。将其视为训练数据。

  2. https://spacy.io/usage/spacy-101#training,使用 spacy 来训练一个模型,你可以在其中输入模型 - 句子、标签和 spantokens,比如 - [{'red color shirt':[['color' ,(0,3)],['类型',(10,14)]]。

  3. 学习如何创建 spacy 教程中给出的模型并训练模型。

  4. 您还可以在简单的英语模型上使用基于规则的快速方法 - https://spacy.io/usage/rule-based-matching of spacy 并为其添加模式。

  5. 我更喜欢第 4 点,一旦准备好基于规则的模型,您可以在一堆数据上运行它,形成第 1 点中提到的 excel 数据进行训练。