Convert natural language text to structured data.
I'm developing a bot to help user assist in identifying Apparels. The problem is to convert natural language text to structured data (list of apparels) and query the store's inventory to find the closest match for each item.
For example, consider the following user input to the bot.
"I would like to order regular fit blue jeans with hip size 32 inches"
and the desired output will be the following
[
{
"quantity": 1,
"size": "32 inches",
"category": "jeans",
"attributes":[
{"colour": "blue"},
{"fit": "regular fit"}
]
}
]
I've attempted to solve the problem by splitting it into two parts.
Part 1: Named entity recognition using conditional random fields (CRF). - I've used approached discussed here to tag individual tokens and I'm able to extract entities like apparel type
, apparel size
and attributes
etc.
example output (representation) of tagger :
I would like to order regular fit blue jeans with hip size 32 inches
| | | | | | | | | | | |
+---------------------- +---------+ +--+ +---+ +-----------+ +-------+
OTHERS FIT COLOR CATEGORY ATTR_TYPE SIZE
Part 2: Rule-based grammar - Assuming a query from a user will always be a combination of defined entities (like type, color, fit, etc), I've written rules to capture the sequence of tags and their respective tokens and transform them into the required format.
Following are a few examples of commonly occurring sequences:
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
I've made some assumptions and mined frequently occurring sequences to write these rules.
The second part is not scalable and becomes a bottleneck. I cannot keep adding rules for capturing additional data points or handling new patterns that the system has not seen.
I'm looking for a generalized solution/data pipeline that can extract entities (relational) from natural language and convert them to structured data.
I would appreciate any ideas.
More examples to help understand the problem better:
Example 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"}
]
}
]
Example 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"}
]
}
]
Edit 1: I'm trying to parse the sequence of entities and transform it into structured data using rules. The current rule-based system has limitations like maintaining rules require skilled experts, they need to be manually crafted and enhanced all the time. Is there a way to overcome these limitations using ML? Replace the rule-based parser with an ML-based parser?
Part 1
. Simply train a NER model and write a python script to convert the extracted entities into a dataframe. Why isPart 1
of your solution not working? $\endgroup$