Search for English words from terminal

Introduction

I believe that any programmer wants to complete all the operations from the terminal. (Since emacs cannot be used, it is a secret that it is vscode group)

So, look up the meaning of English words from terminal with ruby.

Program body

old version

dic.rb


system "curl -s https://ejje.weblio.jp/content/#{ARGV[0]} |grep main> ~/dic/tmp.txt"

File.open("#{Dir.home}/dic/tmp.txt", "r") do |f|
  f.readlines[0].match(/.*<td class="content-explanation .*\">(.*)<\/td>.*/)
  puts $1
end

system "rm ~/dic/tmp.txt"

Line 1: Use curl in system calls. I am using Weblio English-Japanese Dictionary. The arguments are expanded in the url and narrowed down by grep. Write it to ~ / dic / tmp.txt.

2nd line: Open the previous file in read mode. Since "~" could not be used in the .File method, it is set to Dir.home.

Line 3: Read the file and extract the Japanese translation using regular expressions. Output it.

Finally, delete the file.

New version

@scivola pointed out in the comments. Thank you.

dic.rb


result = `curl -s https://ejje.weblio.jp/content/#{ARGV[0]}`
puts result.match(%r{main.*<td class="content-explanation .*">(.*)</td>}).to_a[1]

I can't describe it so concisely! I realized that I still have a lot to learn.

Add alias

config.fish


alias dic='ruby ~/dic/dic.rb'

Example of use

~ 
❯❯❯ dic dog
dog,(Of canines)雄、雄dog,くだらない人間、魅力のない男、醜い女、「ぶす」、やつ、くだらないもの、失敗作

Words of course

~ 
❯❯❯ dic takecareof
Take care of ..., take care of ..., take care of ..., take on ..., handle, survive, get rid of yourself, defeat, kill

Collocation is also ok

~ 
❯❯❯ dic physics
natural philosophy、physics

On the contrary, English translation is also ok

Recommended Posts

Search for English words from terminal
Search for character string B from character string A (duplicate count)
Linear search problem for arrays