A search interface for the Performing Patents Otherwise publication as part of the Politics of Patents case study (part of Copim WP6): this parses data from the archive of RTF files and provides additional data from the European Patent Office OPS API. https://patents.copim.ac.uk
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

257 行
9.0KB

  1. # @name: solr.py
  2. # @creation_date: 2022-09-07
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <simon.bowie.19@gmail.com>
  5. # @purpose: Performs Solr functions
  6. # @acknowledgements:
  7. # pycountry module for country data: https://pypi.org/project/pycountry/
  8. import os
  9. import requests
  10. import re
  11. import urllib
  12. import random
  13. import pycountry
  14. from . import ops
  15. # get config variables from OS environment variables: set in env file passed through Docker Compose
  16. solr_hostname = os.environ.get('SOLR_HOSTNAME')
  17. solr_port = os.environ.get('SOLR_PORT')
  18. def solr_search(solrurl):
  19. # get result
  20. request = requests.get(solrurl)
  21. if request.status_code != 200:
  22. error = "Solr error: " + str(request.text)
  23. return error
  24. else:
  25. # turn the API response into useful Json
  26. json = request.json()
  27. num_found = json['response']['numFound']
  28. facets = []
  29. if (num_found == 0):
  30. output = 'no results found'
  31. else:
  32. output = []
  33. for result in json['response']['docs']:
  34. # set ID variable
  35. id = result['id']
  36. # set content variable
  37. content = result['content']
  38. # parse result
  39. result_output = parse_result(id, content)
  40. output.append(result_output)
  41. try:
  42. json['facet_counts']
  43. facets = json['facet_counts']['facet_fields']
  44. except KeyError:
  45. pass
  46. return output, num_found, facets
  47. def query_search(core, sort, query, country, year, page):
  48. # assemble parameters for the query string to Solr
  49. if (sort == 'relevance'):
  50. sort_parameter = ''
  51. else:
  52. sort_parameter = '&sort=' + sort
  53. if (query is None or query == 'None'):
  54. query_parameter = '&q=*%3A*'
  55. else:
  56. query_parameter = '&q=content%3A' + urllib.parse.quote_plus(query)
  57. if (country is None or country == 'None'):
  58. country_parameter = ''
  59. else:
  60. field = 'country'
  61. country_parameter = '&fq=%7B!term%20f%3D' + field + '%7D' + country
  62. if (year is None or year == 'None'):
  63. year_parameter = ''
  64. else:
  65. field = 'year'
  66. year_parameter = '&fq=%7B!term%20f%3D' + field + '%7D' + year
  67. if (page is None or page == 'None'):
  68. page_parameter = ''
  69. else:
  70. start = (int(page) * 10) - 10
  71. start = str(start)
  72. page_parameter = '&start=' + start + '&rows=10'
  73. # assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
  74. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&indent=true' + query_parameter + '&wt=json' + sort_parameter + country_parameter + year_parameter + page_parameter + '&facet.field=country&facet.field=year&facet.sort=count&facet.mincount=1&facet=true'
  75. output = solr_search(solrurl)
  76. return output
  77. def id_search(core, id):
  78. # assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
  79. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=id%3A"' + id + '"&wt=json'
  80. output = solr_search(solrurl)
  81. return output
  82. def random_search(core):
  83. rand = str(random.randint(0, 9999999))
  84. # assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
  85. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=*%3A*&wt=json&sort=random_' + rand + '%20asc&rows=1'
  86. output = solr_search(solrurl)
  87. return output
  88. def parse_result(id, input):
  89. output = {}
  90. output['id'] = id
  91. # set document reference number (used for OPS API)
  92. doc_ref = re.search('=D\s(([^\s]*)\s([^\s]*)\s([^\s]*))', input)
  93. if doc_ref is None:
  94. doc_ref = re.search('=D&locale=en_EP\s(([^\s]*)\s([^\s]*)\s([^\s]*))', input)
  95. if doc_ref is None:
  96. output['doc_ref'] = ""
  97. else:
  98. output['doc_ref'] = doc_ref.group(1).replace(" ","")
  99. else:
  100. output['doc_ref'] = doc_ref.group(1).replace(" ","")
  101. # search for the application ID in the content element and display it
  102. application_id = re.search('Application.*\n(.*)\n', input)
  103. output['application_id'] = application_id.group(1)
  104. # search for the EPO publication URL in the content element and display it
  105. epo_publication = re.search('Publication.*\n(.*)\n', input)
  106. output['epo_publication_url'] = epo_publication.group(1)
  107. # search for the IPC publication URL in the content element and display it
  108. ipc_publication = re.search('IPC.*\n(.*)\n', input)
  109. if ipc_publication is not None:
  110. if ipc_publication.group(1) is not None:
  111. output['ipc_publication_url'] = ipc_publication.group(1)
  112. # search for the title in the content element and display it
  113. title = re.search('Title.*?\\n(.*?)\\n|Tile.?\\n(.*?)\\n', input)
  114. if title is not None:
  115. if title.group(1) is not None:
  116. output['title'] = title.group(1)
  117. else:
  118. output['title'] = title.group(2)
  119. # search for the abstract in the content element and display it
  120. abstract = re.search('Abstract.*\n(.*)\n', input)
  121. if abstract is not None:
  122. if abstract.group(1) is not None:
  123. output['abstract'] = abstract.group(1)
  124. else:
  125. abstract = re.search('\(.*?\) (\\n\\n\\n\\n|\\n\\n\\n|\\n\\n)(.*)\\n', input)
  126. if abstract is not None:
  127. if abstract.group(2) is not None:
  128. output['abstract'] = abstract.group(2)
  129. # search for the year in the content element and display it
  130. year = re.search('=D[^\s]*\s[^\s]*\s[^\s]*\s[^\s]*\s(\d{4})', input)
  131. if year is not None:
  132. output['year'] = year.group(1)
  133. # search for the country in the content element and display it
  134. country_code = re.search('FT=D[^\s]*\s(\w{2})', input)
  135. if country_code is not None:
  136. country = pycountry.countries.get(alpha_2=country_code.group(1))
  137. if country is not None:
  138. output['country'] = country
  139. else:
  140. country = pycountry.historic_countries.get(alpha_2=country_code.group(1))
  141. if country is not None:
  142. output['country'] = country
  143. else:
  144. output['country'] = country_code.group(1)
  145. output['raw'] = input
  146. return output
  147. def get_number_random_records(core, number):
  148. results_list = []
  149. i = 0
  150. while i <= number-1:
  151. search_results = random_search(core)
  152. if "error" not in search_results:
  153. results = search_results[0]
  154. for result in results:
  155. results_list.append(result)
  156. i += 1
  157. else:
  158. results_list = search_results
  159. break
  160. return results_list
  161. def get_ten_random_elements(field):
  162. core = 'all'
  163. output = []
  164. i = 0
  165. while i <= 9:
  166. search_results = random_search(core)
  167. results = search_results[0]
  168. for result in results:
  169. if field in result:
  170. dict = {'id': result['id'], field: result[field]}
  171. output.append(dict)
  172. i += 1
  173. return output
  174. def get_random_images(number):
  175. core = 'all'
  176. output = []
  177. i = 0
  178. while i <= number-1:
  179. search_results = random_search(core)
  180. results = search_results[0]
  181. for result in results:
  182. if ops.get_images(result['doc_ref']):
  183. image = ops.get_images(result['doc_ref'])
  184. dict = {'id': result['id']}
  185. dict.update(image)
  186. output.append(dict)
  187. i += 1
  188. return output
  189. def get_total_number(core):
  190. # Assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
  191. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=*:*&wt=json'
  192. # get result
  193. request = requests.get(solrurl)
  194. # turn the API response into useful Json
  195. json = request.json()
  196. num_found = json['response']['numFound']
  197. return num_found
  198. def get_term_data(field, core):
  199. # Assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
  200. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/terms?terms.fl=' + field + '&wt=json&terms.limit=1000&terms.sort=index'
  201. # get result
  202. request = requests.get(solrurl)
  203. # turn the API response into useful Json
  204. json = request.json()
  205. output = json['terms'][field]
  206. return output