I made a python dictionary file for Neocomplete

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.

How to create a dictionary

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.

Dictionary creation code

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

Execution method

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

Completion setting example by Neocomplete.vim using the above dictionary file

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',
 \ }

Supplementary & reference site

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

I made a python dictionary file for Neocomplete
I made a configuration file with Python
Python> I made a test code for my own external file
I made a python text
I made a VM that runs OpenCV for Python
[Python] I made a classifier for irises [Machine learning]
I made a Line-bot using Python!
I made a fortune with Python.
I made a daemon with Python
[VSCode] I made a user snippet for Python print f-string
I made a dash docset for Holoviews
I made a payroll program in Python!
I made a character counter with Python
Beginner: I made a launcher using dictionary
I made a Hex map with Python
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made a simple blackjack with Python
I made a library for actuarial science
A textbook for beginners made by Python beginners
I made a neuron simulator with Python
I made a lot of files for RDP connection with Python
I made a scaffolding tool for the Python web framework Bottle
I made a library that adds docstring to a Python stub file.
[python] I made a class that can write a file tree quickly
I made a Python wrapper library for docomo image recognition API.
I made a script in Python to convert a text file for JSON (for vscode user snippet)
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
〇✕ I made a game
I made a spare2 cheaper algorithm for uWSGI
I made a useful tool for Digital Ocean
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
Procedure for creating a LineBot made with Python
[Python] I made a Youtube Downloader with Tkinter.
I made a downloader for word distributed expression
I tried reading a CSV file using Python
I made a program to check the size of a file in Python
I want to write to a file with Python
I made a peeping prevention product for telework.
I made a Caesar cryptographic program in Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Python Qiita API wrapper "qiipy"
I made a Twitter BOT with GAE (python) (with a reference)
I made a prime number generation program in Python
I made a user management tool for Let's Chat
I made a login / logout process using Python Bottle.
I made a Christmas tree lighting game with Python
I made a window for Log output with Tkinter
I made a cleaning tool for Google Container Registry
I tried running python etc. from a bat file
I made a net news notification app with Python
I made a Python module to translate comment outs
I made a Python3 environment on Ubuntu with direnv.
I want to randomly sample a file in Python
I made a LINE BOT with Python and Heroku
I made a prime number generation program in Python 2
I made a python library to do rolling rank