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
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. # turn the API response into useful Json
  22. json = request.json()
  23. num_found = json['response']['numFound']
  24. facets = []
  25. if (num_found == 0):
  26. output = 'no results found'
  27. else:
  28. output = []
  29. for result in json['response']['docs']:
  30. # set ID variable
  31. id = result['id']
  32. # set content variable
  33. content = result['content']
  34. # parse result
  35. result_output = parse_result(id, content)
  36. output.append(result_output)
  37. try:
  38. json['facet_counts']
  39. facets = json['facet_counts']['facet_fields']
  40. except KeyError:
  41. pass
  42. return output, num_found, facets
  43. def query_search(core, sort, query, country, year):
  44. # assemble parameters for the query string to Solr
  45. if (sort == 'relevance'):
  46. sort_parameter = ''
  47. else:
  48. sort_parameter = '&sort=' + sort
  49. if (query is None or query == 'None'):
  50. query_parameter = '&q=*%3A*'
  51. else:
  52. query_parameter = '&q=content%3A' + urllib.parse.quote_plus(query)
  53. if (country is None or country == 'None'):
  54. country_parameter = ''
  55. else:
  56. field = 'country'
  57. country_parameter = '&fq=%7B!term%20f%3D' + field + '%7D' + country
  58. if (year is None or year == 'None'):
  59. year_parameter = ''
  60. else:
  61. field = 'year'
  62. year_parameter = '&fq=%7B!term%20f%3D' + field + '%7D' + year
  63. # 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
  64. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&indent=true' + query_parameter + '&wt=json' + sort_parameter + country_parameter + year_parameter + '&facet.field=country&facet.field=year&facet.sort=count&facet.mincount=1&facet=true'
  65. output = solr_search(solrurl)
  66. return output
  67. def id_search(core, id):
  68. # 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
  69. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=id%3A"' + id + '"&wt=json'
  70. output = solr_search(solrurl)
  71. return output
  72. def random_search(core):
  73. rand = str(random.randint(0, 9999999))
  74. # 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
  75. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=*%3A*&wt=json&sort=random_' + rand + '%20asc&rows=1'
  76. output = solr_search(solrurl)
  77. return output
  78. def parse_result(id, input):
  79. output = {}
  80. output['id'] = id
  81. # set document reference number (used for OPS API)
  82. doc_ref = re.search('=D\s(([^\s]*)\s([^\s]*)\s([^\s]*))', input)
  83. if doc_ref is None:
  84. doc_ref = re.search('=D&locale=en_EP\s(([^\s]*)\s([^\s]*)\s([^\s]*))', input)
  85. if doc_ref is None:
  86. output['doc_ref'] = ""
  87. else:
  88. output['doc_ref'] = doc_ref.group(1).replace(" ","")
  89. else:
  90. output['doc_ref'] = doc_ref.group(1).replace(" ","")
  91. # search for the application ID in the content element and display it
  92. application_id = re.search('Application.*\n(.*)\n', input)
  93. output['application_id'] = application_id.group(1)
  94. # search for the EPO publication URL in the content element and display it
  95. epo_publication = re.search('Publication.*\n(.*)\n', input)
  96. output['epo_publication_url'] = epo_publication.group(1)
  97. # search for the IPC publication URL in the content element and display it
  98. ipc_publication = re.search('IPC.*\n(.*)\n', input)
  99. if ipc_publication is not None:
  100. if ipc_publication.group(1) is not None:
  101. output['ipc_publication_url'] = ipc_publication.group(1)
  102. # search for the title in the content element and display it
  103. title = re.search('Title.*?\\n(.*?)\\n|Tile.?\\n(.*?)\\n', input)
  104. if title is not None:
  105. if title.group(1) is not None:
  106. output['title'] = title.group(1)
  107. else:
  108. output['title'] = title.group(2)
  109. # search for the abstract in the content element and display it
  110. abstract = re.search('Abstract.*\n(.*)\n', input)
  111. if abstract is not None:
  112. if abstract.group(1) is not None:
  113. output['abstract'] = abstract.group(1)
  114. else:
  115. abstract = re.search('\(.*?\) (\\n\\n\\n\\n|\\n\\n\\n|\\n\\n)(.*)\\n', input)
  116. if abstract is not None:
  117. if abstract.group(2) is not None:
  118. output['abstract'] = abstract.group(2)
  119. # search for the year in the content element and display it
  120. year = re.search('=D[^\s]*\s[^\s]*\s[^\s]*\s[^\s]*\s(\d{4})', input)
  121. if year is not None:
  122. output['year'] = year.group(1)
  123. # search for the country in the content element and display it
  124. country_code = re.search('FT=D[^\s]*\s(\w{2})', input)
  125. if country_code is not None:
  126. country = pycountry.countries.get(alpha_2=country_code.group(1))
  127. if country is not None:
  128. output['country'] = country
  129. else:
  130. country = pycountry.historic_countries.get(alpha_2=country_code.group(1))
  131. if country is not None:
  132. output['country'] = country
  133. else:
  134. output['country'] = country_code.group(1)
  135. output['raw'] = input
  136. return output
  137. def get_ten_random_elements(field):
  138. core = 'all'
  139. output = []
  140. i = 0
  141. while i <= 9:
  142. search_results = random_search(core)
  143. results = search_results[0]
  144. for result in results:
  145. if field in result:
  146. dict = {'id': result['id'], field: result[field]}
  147. output.append(dict)
  148. i += 1
  149. return output
  150. def get_ten_random_images():
  151. core = 'all'
  152. output = []
  153. i = 0
  154. while i <= 9:
  155. search_results = random_search(core)
  156. results = search_results[0]
  157. for result in results:
  158. if ops.get_images(result['doc_ref']):
  159. image = ops.get_images(result['doc_ref'])
  160. result.update(image)
  161. output.append(result)
  162. i += 1
  163. return output
  164. def get_total_number(core):
  165. # 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
  166. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=*:*&wt=json'
  167. # get result
  168. request = requests.get(solrurl)
  169. # turn the API response into useful Json
  170. json = request.json()
  171. num_found = json['response']['numFound']
  172. return num_found
  173. def get_term_data(field, core):
  174. # 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
  175. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/terms?terms.fl=' + field + '&wt=json&terms.limit=1000&terms.sort=index'
  176. # get result
  177. request = requests.get(solrurl)
  178. # turn the API response into useful Json
  179. json = request.json()
  180. output = json['terms'][field]
  181. return output