Guest User

Simile Finder

a guest
Oct 29th, 2016
175
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # concept: A program which searches through a book and maps out every metaphor.
  2. # to make this work, I'll need to find the phrases that ordinarly signal the presence of a metaphor or imagery.
  3. # I'll need some way of indexing place in the book - line number, paragraph number, something.
  4. # I'll need to export the results to a text file and print out some meta-information about them.
  5. # I'll want to match nouns together, maybe, too.
  6.  
  7. import sys
  8. def checkForMetaphor(line):
  9.     splitLine = line.split()
  10.     for i in range(0, len(splitLine)):
  11.         try:
  12.             if splitLine[i] == "as":
  13.                 if splitLine[i+2] == "as":
  14.                     if splitLine[i+1] == "much" or splitLine[i+1] == "soon" or splitLine[i+1] == "well":
  15.                         return False
  16.                     else:
  17.                         return True
  18.                 elif splitLine[i+1] == "though":
  19.                     return True
  20.                 elif splitLine[i+1] == "if":
  21.                     return True
  22.             elif splitLine[i] == "resembled":
  23.                 return True
  24.             elif splitLine[i] ==  "like":
  25.                 if splitLine[i-1] == "are" or splitLine[i-1] == "were" or splitLine[i-1] == "was":
  26.                     return True
  27.         except IndexError:
  28.             return False
  29.  
  30.  
  31. def main():
  32.     print "Let's make a list of similes.\n"
  33.     command = ""
  34.     listOfSentences = []
  35.     resultList = []
  36.     counter = 0
  37.     while command != "exit":
  38.         command = raw_input("Type in the name of the text file you want to search.\n")
  39.         f = open(command, 'r')
  40.         g = open("output.txt","a")
  41.         for line in f:
  42.             newSentence = ""
  43.             splitLine = line.split()
  44.             for word in splitLine:
  45.                 if "." in word:
  46.                     if word == "Mr." or word == "Mrs.":
  47.                         newSentence += word + " "
  48.                     else:
  49.                         newSentence += word + " "
  50.                         listOfSentences.append(newSentence)
  51.                         newSentence = ""
  52.                 else:
  53.                     newSentence += word + " "
  54.         for x in range(0, len(listOfSentences)):
  55.             if checkForMetaphor(listOfSentences[x]):
  56.                 resultList.append(listOfSentences[x])
  57.         for x in range(0, len(resultList)):
  58.             newString = "Metaphor number " + str(x) + "\n"
  59.             g.write(newString)
  60.             g.write(resultList[x])
  61.             g.write("\n\n")
  62.         print "Found " + str(len(resultList)) + " similes. Outputted to output.txt."
  63.  
  64. main()
RAW Paste Data