Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Accept-Encoding header to a request for edict #101

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

niku
Copy link
Contributor

@niku niku commented Aug 24, 2015

Because, Server returns raw content when a client requests without header.

$ irb
irb(main):001:0> require 'zlib'
=> true
irb(main):002:0> require 'open-uri'
=> true
irb(main):003:0> url = 'http://ftp.monash.edu.au/pub/nihongo/edict.gz'
=> "http://ftp.monash.edu.au/pub/nihongo/edict.gz"
irb(main):004:0> Zlib::GzipReader.open(open(url))
Zlib::GzipFile::Error: not in gzip format
from (irb):4:in initialize' from (irb):4:inopen'
from (irb):4
from /usr/local/bin/irb:11:in `

'
irb(main):005:0> Zlib::GzipReader.open(open(url, { "Accept-Encoding" => "gzip, deflate" }))
=> #Zlib::GzipReader:0x007ff97ee5bf80

Because, Server returns raw content when a client requests without header.

$ irb
irb(main):001:0> require 'zlib'
=> true
irb(main):002:0> require 'open-uri'
=> true
irb(main):003:0> url = 'http://ftp.monash.edu.au/pub/nihongo/edict.gz'
=> "http://ftp.monash.edu.au/pub/nihongo/edict.gz"
irb(main):004:0> Zlib::GzipReader.open(open(url))
Zlib::GzipFile::Error: not in gzip format
        from (irb):4:in `initialize'
        from (irb):4:in `open'
        from (irb):4
        from /usr/local/bin/irb:11:in `<main>'
irb(main):005:0> Zlib::GzipReader.open(open(url, { "Accept-Encoding" => "gzip, deflate" }))
=> #<Zlib::GzipReader:0x007ff97ee5bf80>
@kou
Copy link
Member

kou commented Aug 25, 2015

How about just removing Zlib::GzipReader?
net/http has auto inflate feature.

Because net/http has auto inflate feature.
@niku
Copy link
Contributor Author

niku commented Aug 26, 2015

OK, I replaced it.
Would you like me to rebase or squash commits?

@kou
Copy link
Member

kou commented Aug 27, 2015

Ah, I like more simple code like the following:

diff --git a/lib/logaling/external_glossaries/edict.rb b/lib/logaling/external_glossaries/edict.rb
index a269679..8ce5511 100644
--- a/lib/logaling/external_glossaries/edict.rb
+++ b/lib/logaling/external_glossaries/edict.rb
@@ -14,8 +14,6 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.

 require 'open-uri'
-require 'zlib'
-require 'stringio'

 module Logaling
   class Edict < ExternalGlossary
@@ -29,18 +27,12 @@ module Logaling
     def convert_to_csv(csv)
       puts "downloading edict file..."
       url = 'http://ftp.monash.edu.au/pub/nihongo/edict.gz'
-      Zlib::GzipReader.open(open(url)) do |gz|
+      open(url) do |edict|
         puts "importing edict file..."

-        lines = StringIO.new(gz.read).each_line
-
-        lines.next # skip header
-
-        preprocessed_lines = lines.map do |line|
-          line.encode("UTF-8", "EUC-JP").chomp
-        end
-
-        preprocessed_lines.each do |line|
+        edict.gets # skip header
+        edict.each_line do |raw_line|
+          line = raw_line.encode("UTF-8", "EUC-JP").chomp
           source, target = line.split('/', 2)
           source = source.strip
           csv << [source, target]

Would you like me to rebase or squash commits?

I like rebase.

@niku
Copy link
Contributor Author

niku commented Oct 19, 2015

Sorry for late reply.
I update this PR. Would you check it again?

@kou
Copy link
Member

kou commented Oct 20, 2015

Thanks!

It looks good except niku@9777cef .
EDICT is a ja -> en dictionary. source should be Japanese.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants