LoginSignup
0
1

More than 1 year has passed since last update.

Rubyのwp2txtライブラリを使えるようにする

Last updated at Posted at 2022-04-29

背景

私はRubyをほぼ使ったことがありません。pだけでプリントができるということを知っているくらいです。
今回はpythonを使って自然言語処理をしようと思ったときにWikipediaの全文データ(xml)を取得してテキストファイルに変換する際にwp2txtというライブラリ?を使うためにインストールしました。

エラーが起きる

機械学習の本を買ってその通りにやっていたところ

[root@server ~]$ wp2txt --input-file ./jawiki-latest-pages-articles.xml
[DEPRECATION] This gem has been renamed to optimist and will no longer be supported. Please switch to optimist as soon as possible.
/root/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/wp2txt-0.9.1/lib/wp2txt.rb:37:in `initialize': uninitialized constant Wp2txt::Runner::Etc (NameError)
	from /root/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/wp2txt-0.9.1/lib/wp2txt.rb:62:in `new'
	from /root/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/wp2txt-0.9.1/lib/wp2txt.rb:62:in `<top (required)>'
	from /usr/local/bin/wp2txt:23:in `load'
	from /usr/local/bin/wp2txt:23:in `<main>'


というエラーが出ました。
調べても古い結果しか出てこなかったですがteratailで素晴らしい回答を見つけました。

こちらを見ていただけるとわかります。
このようなことを見つけられる方は本当にすごいですね。

解決策

find / | grep wp2txt.rb

などで検索して出てきたライブラリの場所をvimで編集します。

[root@server ~]$ vi /root/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/wp2txt-0.9.1/lib/wp2txt.rb
~~~~~~
      @parent = parent
      @fp = nil

      @input_file = input_file
      @output_dir = output_dir
      @tfile_size = tfile_size
      @convert = convert
      @strip_tmarker = strip_tmarker
      num_cores_available = Etc.nprocessors      ←ここ
      @num_threads = num_threads <= num_cores_available ? num_threads : num_cores_available
~~~~~~

37行目付近です。
ここはプロセッサ数を初期化しているらしいので2や4といった数値を直接入れることで動きます。
これで無事解決です。

num_cores_available = 4 #Etc.nprocessors

終わり

今回はこれだけです。
初めてライブラリを書き換えました。こんなこともあるんですね。
このteratailの回答者の方もそうですが、世の中のエンジニアの人は本当にすごいですね。
私も、いつか誰かの役に立てるようなエンジニアになれるように勉強します。
とりあえず機械学習系をいろいろやってみようかな。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1