Last active
May 21, 2018 16:15
-
-
Save VGostyuzhov/ca61693ee4df8c21c5c7fa78606c0cf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buy guitars [location] | |
buy [brand] guitars [location] | |
buy [brand] guitars | |
[brand] guitars for sale | |
[brand] guitars for sale in [location] | |
[location] best [brand] guitars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | word | |
---|---|---|
location | London | |
location | New York | |
brand | Nike | |
brand | ||
location | Berlin | |
brand | Coca Cola |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment