gallicagram 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/gallicagram.rb +20 -26
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 550bf9ebdcf99a41e01e157ba09dc5e864ef4fc531036b4afdd0501a4d5c4ad3
4
- data.tar.gz: 6a18e0c6719baef37699c5035c7abc53008cca58040368fdbcf56ccb687de9f1
3
+ metadata.gz: d38fec4b1068d42da5daa346e992baf9215bf17d4c798c0ebca287a2453fb9a7
4
+ data.tar.gz: b0c072bc24228ae9c2432dc42aa6c7620f0dcb14146dac533bc1c8eaa753bd0e
5
5
  SHA512:
6
- metadata.gz: bbdb13303e02606ac3eea5911787815e5f52a0081adc59f5a7c96cfcfa962ca4edfb49dc2a93c020f973771bd18facd8b10812cb9dddad7b24f0404d9ae82c74
7
- data.tar.gz: 428ddb548775277156091035b04f9d72f3b4549ea39a9842bbc7294a2a93e081e6ce65db402dba921dd119235e4b3a3d9f8e3e9aeab415907a9876d243a91af2
6
+ metadata.gz: a450030b491f0aa6b9af684b409575756b6c30542e90e54cea60b6896cbfedc11dd89fa82e4b516bf1539d04ea89bfb253a74b427b6d808834e902b356fde974
7
+ data.tar.gz: 8c6338665df22d98808ab0cd817af0956fc8e34bff5c00510866a19ecea664481a1fefd120dde3cff7cad7591b3bae90be8b4d33a5b334e45479ebcab2800559
data/lib/gallicagram.rb CHANGED
@@ -6,25 +6,29 @@ class Gallicagram
6
6
 
7
7
  # query = string or array of strings
8
8
  # corpus = ["lemonde","livres","presse"]
9
- # resolution = ["year", "month"]
10
- def self.search(query,corpus="lemonde",start_date="1900",end_date="2000",resolution="month",sum=false)
9
+ # resolution = ["mois", "annee"]
10
+ def self.search(query,corpus="lemonde",start_date="1900",end_date="2000",resolution="annee",sum=false)
11
11
 
12
12
  query = [query] unless query.kind_of?(Array)
13
- output = ""
13
+ data = ""
14
14
  query.each_with_index do |word, index|
15
15
  query = format_query(word, sum)
16
- response = call_api(word, corpus, start_date, end_date)
16
+ response = call_api(word, corpus, start_date, end_date,resolution)
17
17
  unless index == 0
18
18
  response = response.gsub("n,gram,annee,mois,jour,total", "\n").strip
19
19
  end
20
- output << response
20
+ data << response
21
21
  end
22
22
 
23
- if corpus == "livres" && resolution == "month"
24
- resolution = "year"
23
+ if corpus == "livres" && resolution == "mois"
24
+ resolution = "annee"
25
25
  end
26
26
 
27
- data = group_by_resolution(output, resolution)
27
+ if resolution == "mois"
28
+ data = group_by_resolution(data, resolution)
29
+ else
30
+ data = CSV.parse(data, :headers => true)
31
+ end
28
32
 
29
33
  return data
30
34
  end
@@ -40,33 +44,23 @@ def format_query(query, sum)
40
44
 
41
45
  end
42
46
 
43
- def call_api(query, corpus, start_date, end_date)
47
+ def call_api(query, corpus, start_date, end_date, resolution)
44
48
  # We search through the Gallicagram API
45
- url = "https://shiny.ens-paris-saclay.fr/guni/query?corpus=#{corpus}&mot=#{CGI.escape(query)}&from=#{start_date}&to=#{end_date}"
49
+ url = "https://shiny.ens-paris-saclay.fr/guni/query?corpus=#{corpus}&mot=#{CGI.escape(query)}&from=#{start_date}&to=#{end_date}&resolution=#{resolution}"
46
50
 
47
51
  response = URI.open(url)
48
52
 
49
53
  return response.read
50
54
  end
51
55
 
52
- def group_by_resolution(data, resolution)
56
+ def group_by_resolution(data)
53
57
  csv_parsing = CSV.parse(data, :headers => true)
54
58
  clean_data_array = []
55
- case resolution
56
- when "year"
57
- csv_parsing.group_by { |word| [word["annee"], word["gram"]] }.each do |cp|
58
- sum = cp[1].sum { |word| word["total"].to_i }
59
- new_row = cp[0]
60
- new_row << sum
61
- clean_data_array << new_row
62
- end
63
- when "month"
64
- csv_parsing.group_by { |word| [word["annee"], word["mois"], word["gram"]] }.each do |cp|
65
- sum = cp[1].sum { |word| word["total"].to_i }
66
- new_row = cp[0]
67
- new_row << sum
68
- clean_data_array << new_row
69
- end
59
+ csv_parsing.group_by { |word| [word["annee"], word["mois"], word["gram"]] }.each do |cp|
60
+ sum = cp[1].sum { |word| word["total"].to_i }
61
+ new_row = cp[0]
62
+ new_row << sum
63
+ clean_data_array << new_row
70
64
  end
71
65
 
72
66
  return clean_data_array
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gallicagram
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Le Roux