Python: NLTK: NLP to SQL Where Clause

54 Views Asked by At

I have a Python script that loads an sql parser and tries to convert a query string into a where clause. I am having problems with parsing my string into a query. How do I load a regex pattern as grammar?

Below is my Python code:

import nltk    
from nltk.tokenize import word_tokenize
from nltk import load_parser




nltk.data.show_cfg('./my_sql0.fcfg')    
cp = load_parser('./my_sql0.fcfg')
query = 'find a red car with license plate matching AMC'
trees = list(cp.parse(query.split()))
print(trees)
for tree in trees:
    print(tree)

~  

Any help is appreciated.

1

There are 1 best solutions below

0
SoftwareDveloper On BEST ANSWER

I figured out how to use the regexp_tagger in tagging my NNP text to my regex pattern. I did some list manipulation in appending and removing, but the idea is to tag a text with regex pattern. This solved my problem of using a regex patter in my code. See the code below:

regexp_tagger = nltk.RegexpTagger(
        [
                (r"^[A-Za-z0-9]+(?:[A-Za-z0-9]+)*", "ALN")
        ])
tagged_text = regexp_tagger.tag(mysentence.split())