Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # concept: A program which searches through a book and maps out every metaphor.
- # to make this work, I'll need to find the phrases that ordinarly signal the presence of a metaphor or imagery.
- # I'll need some way of indexing place in the book - line number, paragraph number, something.
- # I'll need to export the results to a text file and print out some meta-information about them.
- # I'll want to match nouns together, maybe, too.
- import sys
- def checkForMetaphor(line):
- splitLine = line.split()
- for i in range(0, len(splitLine)):
- try:
- if splitLine[i] == "as":
- if splitLine[i+2] == "as":
- if splitLine[i+1] == "much" or splitLine[i+1] == "soon" or splitLine[i+1] == "well":
- return False
- else:
- return True
- elif splitLine[i+1] == "though":
- return True
- elif splitLine[i+1] == "if":
- return True
- elif splitLine[i] == "resembled":
- return True
- elif splitLine[i] == "like":
- if splitLine[i-1] == "are" or splitLine[i-1] == "were" or splitLine[i-1] == "was":
- return True
- except IndexError:
- return False
- def main():
- print "Let's make a list of similes.\n"
- command = ""
- listOfSentences = []
- resultList = []
- counter = 0
- while command != "exit":
- command = raw_input("Type in the name of the text file you want to search.\n")
- f = open(command, 'r')
- g = open("output.txt","a")
- for line in f:
- newSentence = ""
- splitLine = line.split()
- for word in splitLine:
- if "." in word:
- if word == "Mr." or word == "Mrs.":
- newSentence += word + " "
- else:
- newSentence += word + " "
- listOfSentences.append(newSentence)
- newSentence = ""
- else:
- newSentence += word + " "
- for x in range(0, len(listOfSentences)):
- if checkForMetaphor(listOfSentences[x]):
- resultList.append(listOfSentences[x])
- for x in range(0, len(resultList)):
- newString = "Metaphor number " + str(x) + "\n"
- g.write(newString)
- g.write(resultList[x])
- g.write("\n\n")
- print "Found " + str(len(resultList)) + " similes. Outputted to output.txt."
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement