kanecain
kanecain

Reputation: 51

Recommendation of a Python Text Match and Replace Technique

I am new to python, and trying to find the best and most efficient way of performing a match and replace. Here is my problem.

I have a dictionary with entries such as below.

myDict = {'I HAVE A * NAMED *':'A <star=1> named <star=2> is cool!'}

My goal is to have an input:

myInput = raw_input()
# Example input: I HAVE A DOG NAMED MAX

And then match this input with the key in myDict:

input: 'I HAVE A DOG NAMED *MAX*' matches with dictionary key: 'I HAVE A * NAMED *'

And then output the key value with the star tags replaced with the missing myInput words, DOG and MAX.

output = 'A DOG named MAX is cool!'

Any sage advice is greatly appreciated!

Upvotes: 1

Views: 69

Answers (2)

sangheestyle
sangheestyle

Reputation: 1077

This might be another solution for you even Shoo Limberger already showed us a good solution :)

import re


def convert(sentence, mapping, predefined_regex):
    for k, v in mapping.iteritems():
        p = re.compile(k.format(**predefined_regex))
        g = p.search(sentence)
        if g:
            keywords = g.groupdict()
            converted_sentence = v.format(**keywords)
            return converted_sentence
    return None


predefined_regex = {
    "type": "(?P<type>\w+)",
    "name": "(?P<name>\w+)"
}
my_dict = {
    "I have [a|an] {type} named {name}": "A {type} named {name} is cool!",
    "You have [a|an] {type} named {name}": "{name} likes you!"
}
sentences = [
    "I have a dog named Max.",
    "You have a cat named Kitty.",
    "He has a pig named Max"
]
for sentence in sentences:
    converted_sentence = convert(sentence, my_dict, predefined_regex)
    if not converted_sentence:
        converted_sentence = "Not found"
    print("{} -> {}".format(sentence, converted_sentence))

Reference:

Upvotes: 1

Shoo Limberger
Shoo Limberger

Reputation: 376

This is what you want?

import re 

myDict = {'I HAVE A (.+) NAMED (.+)':'A <star=1> named <star=2> is cool!'}
input="I HAVE A dog NAMED max"

for x in myDict.keys():
   if re.match(x,input) :
        d=myDict[x]
        for n in range(1, 3):
           d = d.replace('<star='+str(n)+'>',re.match(x,input).group(n))
        print '=>',  d

Upvotes: 1

Related Questions