Skip to content

Instantly share code, notes, and snippets.

@VGostyuzhov
Last active May 21, 2018 16:15
Show Gist options
  • Save VGostyuzhov/ca61693ee4df8c21c5c7fa78606c0cf2 to your computer and use it in GitHub Desktop.
Save VGostyuzhov/ca61693ee4df8c21c5c7fa78606c0cf2 to your computer and use it in GitHub Desktop.
buy guitars [location]
buy [brand] guitars [location]
buy [brand] guitars
[brand] guitars for sale
[brand] guitars for sale in [location]
[location] best [brand] guitars
import csv
phrases = []
with open ('words.csv', 'r') as wordfile:
csvreader = csv.DictReader(wordfile, skipinitialspace=True)
words = [{key: value for key, value in row.items()} for row in csvreader]
print(words)
with open('patterns.txt', 'r') as patternfile:
patterns = patternfile.readlines()
for pattern in patterns:
for word in words:
print(word)
# Transform the location to [location], brand to [brand]
word_type = '[{}]'.format(word['type'])
# Obviously it replaces just the first found token (location or brand)
if word_type in pattern:
phrase = pattern.replace(word_type, word['word'])
phrases.append(phrase)
with open('result.txt', 'w') as resultfile:
for phrase in phrases:
print(phrase.strip())
resultfile.write(phrase)
type word
location London
location New York
brand Nike
brand Google
location Berlin
brand Coca Cola
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment