indepcites.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Author: Esty Thomas
  2. # License: GPL v3.0
  3. # If the list of authors will return a search term
  4. # under 2048 characters, indep() will use this
  5. # which simply returns a string of the format
  6. # author:'name 1' OR author:'name 2' etc
  7. def indep_short(authl)
  8. authors=authl.join("' OR author:'")
  9. authors="author:'"+authors+"'"
  10. return authors
  11. end
  12. # If the list of authors will return a search term
  13. # over 2048 characters, indep() will use this
  14. # which recursively splits the list until each
  15. # separate output term will be under 2048 characters
  16. # and returns them in an array.
  17. # It's not the prettiest hack but it will do until
  18. # I start figuring out how to make this into a browser
  19. # extension or something.
  20. def indep_long(authl)
  21. half = (authl.length/2).floor
  22. authl1 = authl.take(half)
  23. authl2 = authl.drop(half)
  24. authors1 = "author:'"+authl1.join("' OR author:'")+"'"
  25. authors2 = "author:'"+authl2.join("' OR author:'")+"'"
  26. if (authors1.length>2048) && (authors2.length<2048)
  27. return [indep_long(authl1), authors2] #split authl1
  28. elsif (authors1.length<2048) && (authors2.length>2048)
  29. return [authors1, indep_long(authl2)] #split authl2
  30. elsif (authors1.length>2048) && (authors2.length>2048)
  31. return [indep_long(authl1), indep_long(authl2)] #split both
  32. else
  33. return [authors1, authors2]
  34. end
  35. end
  36. # only use 'sort' when you have an author list like this:
  37. # "Last, First, First Last, First Last, and First Last"
  38. # it will give you the author list back like this:
  39. # "First Last, First Last, First Last, First Last"
  40. # so you can put it into 'indep'
  41. def sort(auth)
  42. authl = auth.split(", ")
  43. authl[1] = authl[1] + " " + authl[0]
  44. authl = authl.drop(1)
  45. last = authl[-1]
  46. lastl = last.split(" ")
  47. lastnew = lastl.drop(1).join(" ")
  48. authl[-1] = lastnew
  49. return authl.join(", ")
  50. end
  51. # this will give you search terms you can use in Google Scholar
  52. # use this for author lists like this:
  53. # "First Last, First Last, First Last, First Last"
  54. # If the list starts "Last, First" and ends "and First Last"
  55. # put it into 'sort' and use the result
  56. def indep(auth)
  57. authl = auth.split(", ")
  58. if (auth.length+9+(authl.length-1)) < 2048
  59. return indep_short(authl)
  60. else
  61. return indep_long(authl)
  62. end
  63. end