Advertisement
Guest User

Untitled

a guest
May 25th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. import sys
  2. from PyQt5 import QtWidgets, QtCore, QtGui
  3. import tkinter as tk
  4. import image
  5. from PIL import ImageGrab
  6. import numpy as np
  7. import cv2
  8. import os
  9. import io
  10. from google.cloud import vision
  11. from google.cloud.vision import types
  12. import json
  13. import requests
  14. import pandas as pd
  15. from nltk.corpus import stopwords
  16. import nltk
  17. import urllib.request
  18. from http.cookiejar import CookieJar
  19.  
  20. fountq1=0
  21. fountq2=0
  22. fountq3=0
  23.  
  24. def searchTerms(serchTerms):
  25. serchTerms=serchTerms.lower()
  26. serchTerms = serchTerms.replace('-', ' ')
  27. serchTerms = serchTerms.replace(' of ', ' ')
  28. serchTerms = serchTerms.replace(' in ', ' ')
  29. serchTerms = serchTerms.replace(' a ', ' ')
  30. serchTerms = serchTerms.replace(' to ', ' ')
  31. serchTerms = serchTerms.replace('the ', '')
  32. serchTerms = serchTerms.replace('.', '')
  33. serchTerms = serchTerms.replace(',', '')
  34. serchTerms = serchTerms.replace('"', '')
  35. return(serchTerms)
  36.  
  37. def ser(bol,a1,a2,a3,question):
  38.  
  39. serchTerms1= a1
  40. serchTerms2= a2
  41. serchTerms3= a3
  42. serchTerms1= searchTerms(serchTerms1)
  43. serchTerms2= searchTerms(serchTerms2)
  44. serchTerms3= searchTerms(serchTerms3)
  45.  
  46. temp1=a1
  47. temp2=a2
  48. temp3=a3
  49.  
  50. a1 = a1.replace(',', '')
  51. a1 = a1.replace('.', '')
  52. a1 = a1.replace('"', '')
  53. a2 = a2.replace(',', '')
  54. a2 = a2.replace('.', '')
  55. a2 = a2.replace('"', '')
  56. a3 = a3.replace(',', '')
  57. a3 = a3.replace('.', '')
  58. a3 = a3.replace('"', '')
  59.  
  60. OGquestion = question
  61. question = question.replace(',', '')
  62. question = question.replace('.', '')
  63. question = question.replace('"', '')
  64. question = question.replace('?', '')
  65.  
  66. if(bol==0):
  67. serchTerms=serchTerms1
  68. question = question + a1
  69. elif(bol==1):
  70. serchTerms=serchTerms2
  71. question = question + a2
  72. else:
  73. serchTerms=serchTerms3
  74. question = question + a3
  75.  
  76. question = question.replace(' 93 ', '')
  77.  
  78.  
  79. #################################################################
  80. key = AIzaSyBJEBtSaPXdzoxRu_IzDRmuinP2g5zU25A
  81. cx = 006743955312219076665:wn3iz7ppz08
  82. q = question
  83.  
  84. url = "https://www.googleapis.com/customsearch/v1"
  85. parameters = {"q": q,
  86. "cx": cx,
  87. "key": key,
  88. "orTerms": serchTerms,
  89. #orTerms
  90. #exactTerms
  91. "filter": "1"
  92. }
  93.  
  94. page = requests.request("GET", url, params=parameters)
  95. results = json.loads(page.text)
  96. results.keys()
  97.  
  98. #########################Searching the snippets and titles#############################################
  99. global fountq1
  100. global fountq2
  101. global fountq3
  102.  
  103. s1=a1.lower()
  104. s2=a2.lower()
  105. s3=a3.lower()
  106.  
  107. s1=s1.split()
  108. s2=s2.split()
  109. s3=s3.split()
  110.  
  111. s1=[word for word in s1 if not word in stopwords.words('english')]
  112. s2=[word for word in s2 if not word in stopwords.words('english')]
  113. s3=[word for word in s3 if not word in stopwords.words('english')]
  114.  
  115. #for item in results["items"]:
  116. # print(item["snippet"])
  117.  
  118. for item in results["items"]:
  119. fountq1=fountq1+(2*item["snippet"].lower().count(temp1.lower()))
  120. fountq1=fountq1+(2*item["title"].lower().count(temp1.lower()))
  121. if(a1.find('-')):
  122. temp1=temp1.replace('-', '')
  123. fountq1=fountq1+(2*item["snippet"].lower().count(temp1.lower()))
  124. fountq1=fountq1+(2*item["title"].lower().count(temp1.lower()))
  125. for x in s1:
  126. if(2<len(x)):
  127. fountq1+=item["snippet"].lower().count(x)
  128. fountq1+=item["title"].lower().count(x)
  129.  
  130. for item in results["items"]:
  131. fountq2=fountq2+(2*item["snippet"].lower().count(temp2.lower()))
  132. fountq2=fountq2+(2*item["title"].lower().count(temp2.lower()))
  133. if(a2.find('-')):
  134. temp2=temp2.replace('-', '')
  135. fountq2=fountq2+(2*item["snippet"].lower().count(temp2.lower()))
  136. fountq2=fountq2+(2*item["title"].lower().count(temp2.lower()))
  137. for x in s2:
  138. if(2<len(x)):
  139. fountq2+=item["snippet"].lower().count(x)
  140. fountq2+=item["title"].lower().count(x)
  141.  
  142. for item in results["items"]:
  143. fountq3=fountq3+(2*item["snippet"].lower().count(temp3.lower()))
  144. fountq3=fountq3+(2*item["title"].lower().count(temp3.lower()))
  145. if(a3.find('-')):
  146. temp3=temp3.replace('-', '')
  147. fountq3=fountq3+(2*item["snippet"].lower().count(temp3.lower()))
  148. fountq3=fountq3+(2*item["title"].lower().count(temp3.lower()))
  149. for x in s3:
  150. if(2<len(x)):
  151. fountq3+=item["snippet"].lower().count(x)
  152. fountq3+=item["title"].lower().count(x)
  153.  
  154. print("**************************************************************")
  155. print("question: ", end='')
  156. print(OGquestion)
  157.  
  158. print("")
  159. print(a1)
  160. print("snipet/title: ", end='')
  161. print(fountq1)
  162. print("")
  163.  
  164. print("")
  165. print(a2)
  166. print("snipet/title: ", end='')
  167. print(fountq2)
  168. print("")
  169.  
  170. print("")
  171. print(a3)
  172. print("snipet/title: ", end='')
  173. print(fountq3)
  174. print("")
  175.  
  176. pp=0
  177. pp+=question.count(" NOT ")
  178. if(pp>0):
  179. print("")
  180. print("-------------NOT-----------")
  181.  
  182.  
  183. def searchNow(alal):
  184. ########################Pre Processing#########################################
  185. list = alal.split("\n")
  186. length=len(list)-1
  187.  
  188. if(length<3):
  189. print("string too small EXITING")
  190. exit()
  191.  
  192. a1=list[length-3]
  193. a2=list[length-2]
  194. a3=list[length-1]
  195.  
  196. question=""
  197. i=length-4
  198. while (i>=0):
  199. question=list[i]+" "+question
  200. i-=1
  201.  
  202. print("*****************************************************************")
  203. print(question)
  204.  
  205. for t in range(3):
  206. ser(t,a1,a2,a3,question)
  207.  
  208.  
  209. class MyWidget(QtWidgets.QWidget):
  210. def __init__(self):
  211. super().__init__()
  212. root = tk.Tk()
  213. screen_width = root.winfo_screenwidth()
  214. screen_height = root.winfo_screenheight()
  215. self.setGeometry(0, 0, screen_width, screen_height)
  216. self.setWindowTitle(' ')
  217. self.begin = QtCore.QPoint()
  218. self.end = QtCore.QPoint()
  219. self.setWindowOpacity(0.3)
  220. QtWidgets.QApplication.setOverrideCursor(
  221. QtGui.QCursor(QtCore.Qt.CrossCursor)
  222. )
  223. self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
  224. self.show()
  225.  
  226. def paintEvent(self, event):
  227. qp = QtGui.QPainter(self)
  228. qp.setPen(QtGui.QPen(QtGui.QColor('black'), 3))
  229. qp.setBrush(QtGui.QColor(128, 128, 255, 128))
  230. qp.drawRect(QtCore.QRect(self.begin, self.end))
  231.  
  232. def mousePressEvent(self, event):
  233. self.begin = event.pos()
  234. self.end = self.begin
  235. self.update()
  236.  
  237. def mouseMoveEvent(self, event):
  238. self.end = event.pos()
  239. self.update()
  240.  
  241. def mouseReleaseEvent(self, event):
  242. self.close()
  243.  
  244. x1 = min(self.begin.x(), self.end.x())
  245. y1 = min(self.begin.y(), self.end.y())
  246. x2 = max(self.begin.x(), self.end.x())
  247. y2 = max(self.begin.y(), self.end.y())
  248.  
  249. img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
  250. img.save('capture.png')
  251. img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
  252.  
  253. #cv2.imshow('Captured Image', img)
  254. cv2.waitKey(0)
  255. cv2.destroyAllWindows()
  256.  
  257. #**************************aa.py***********************************************
  258. print("inputting image")
  259.  
  260. path=r"C:\Users\honol\Desktop\trivia\for-Github\capture.png"
  261.  
  262. client = vision.ImageAnnotatorClient()
  263.  
  264. with io.open(path, 'rb') as image_file:
  265. content = image_file.read()
  266.  
  267. image = types.Image(content=content)
  268.  
  269. response = client.text_detection(image=image)
  270. texts = response.text_annotations
  271.  
  272. count=0
  273. alal="empty"
  274. for text in texts:
  275.  
  276. if(count==0):
  277. alal=text.description
  278. count=1
  279.  
  280. vertices = (['({},{})'.format(vertex.x, vertex.y)
  281. for vertex in text.bounding_poly.vertices])
  282.  
  283. if(alal!="empty"):
  284. searchNow(alal)
  285.  
  286. if __name__ == '__main__':
  287. app = QtWidgets.QApplication(sys.argv)
  288. window = MyWidget()
  289. window.show()
  290. app.aboutToQuit.connect(app.deleteLater)
  291. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement