I want to apply omni completion when writing python code with Neocomplete.vim. A quick way is to use jedi.vim, which I usually use, but Due to various reasons, I created a dictionary file and referred to it to complete it.
Scraping the index page of docs.python.jp, The listed items (functions, classes, terms) are now dictionary file items. The language is Ruby, and Nokogiri is used for scraping, and there are some powerful parts.
make_python_dicts.rb
require 'nokogiri'
require 'open-uri'
require 'net/http'
#Added check method for multibyte characters
class String
def has_multibytes?
self.bytes do |byte|
return true if (byte & 0b10000000) != 0
end
false
end
end
#Argument check (get version and output file name)
if ARGV.size == 1
versions = "2.7"
DICT_NAME = ARGV[0].to_s
elsif ARGV.size == 2
DICT_NAME = ARGV[0].to_s
versions = ARGV[1].to_s
else
puts "Argument error"
exit
end
#Index page URL generation
BASE_URL = "http://docs.python.jp/" + versions + '/'.freeze
INDEX_URL = BASE_URL + 'genindex.html'.freeze
#Check if the specified version of the index page exists
res = Net::HTTP.get_response(URI.parse(INDEX_URL))
if res.code != '200'
puts "status error : " + res.code.to_s
exit
end
#Get source for index page
index = Nokogiri::HTML(open(INDEX_URL))
link_tags = index.xpath('//div[@class="genindex-jumpbox"]')
# A~Index item extraction from index pages up to Z (excluding Japanese items & auxiliary items)
items = []
link_tags.search('//p[position()=1]//a').each do |tag|
urls = URI.escape(BASE_URL + tag[:href])
doc = Nokogiri::HTML(open(urls))
doc.search('.//table[@class="indextable"]//a[position()=1]').each do |item|
item = item.text.split[0]
unless item.match(/^[-.:(]|,$/) || item.has_multibytes?
items << item
end
end
end
#After sorting all the acquired items, omitting duplicates, export as a dictionary file
File.open(DICT_NAME, 'w') do |f|
items.uniq!.sort!
items.each { |item| f.puts(item) }
end
Specify the dictionary file name to be output with the first argument and the version of python you want to create with the second argument. If the second argument is omitted, a version 2.7 dictionary file will be created. (As of June 2016, you can specify versions 2.7 and 3.5 where the index page exists)
#Specify only the output file name
$ ruby make_python_dict.rb python.dict
#Output file and version specification (ver 3.5)
$ ruby make_python_dict.rb python.dict 3.5
It is OK if you set the following in .vimrc and read the created dictionary file.
"Completion candidate display pattern setting
let g:neocomplete#force_omni_input_patterns.python = '[^. \t]\.\w*'
"Dictionary reference setting
let g:neocomplete#sources#dictionary#dictionaries = {
\ 'python': $HOME . '/dicts/python.dict',
\ }
Since I just checked the brute force and rough, there may be some defects in the dictionary file. Also ver2.7 and [ver3.5](https://github.com/ligerbolt/dicts/blob/ Put each dictionary file of master / python_ver3.5.dict) on github. If you want to use it, please.
Recommended Posts