LoginSignup
4
4

More than 5 years have passed since last update.

Watson Discovery で PowerPoint も Excel もクロールして取り込む

Last updated at Posted at 2018-06-01

Watson Discovery で取り込めるファイル形式は Word、PDF、HTML、Json になりますが、以下の記事で紹介している Nutch を使用すると PowerPoint や Excel 等、Discover がサポートしていない形式のファイルも取り込み可能です。

Watson Discovery で Web クローリング(1)
Watson Discovery で Web クローリング(2)

Nutch では Web クロールをする代わりに、ファイルシステムをクロールするように設定することができます。また、Nutch は内部的にオープンソースのパーサーである Apache Tika を使用しており、この Apache Tika が PowerPoint や Excel を含むさまざまなフォーマットのファイルからテキストやメタデータを
抽出することができます。以降では Nutch でファイルシステムをクロールする手順を紹介します。

設定手順

ソースコードの取得&ビルド

Github 上のソースコードを以下のように取得します。

git clone https://github.com/schiyoda/nutch-indexer-discovery.git -b file

こちらの手順と同じように buildPlugin まで進めます。

$ ./gradlew
$ ./gradlew setupHbase
$ ./start-hbase.sh
conf/nutch-discovery/nutch-site.xml の修正
$ ./gradlew setupNutch
$ ./gradlew buildPlugin

少し解説

取得したソースコードはこちらにあるものに対し、ファイルシステムクロールのために以下のような修正をしています。

nutch-site.xml の修正

ファイルシステムクロールのために、plugin.includes で protocol-httpclient の代わりに protocol-file を指定します。また、ファイルのメタデータの取得のために、parse-metatags、index-metadata を追加します。

nutch-site.xml
  <property>
    <name>plugin.includes</name>
    <!-- do **NOT** enable the parse-html plugin, if you want proper HTML parsing. Use something like parse-tika! -->
    <value>protocol-file|urlfilter-regex|parse-(text|tika|js|metatags)|index-(basic|anchor|metadata)|query-(basic|site|url)|response-(json|xml)|summary-basic|scoring-opic|urlnormalizer-(pass|regex|basic)|indexer-discovery</value>
  </property>

さらに以下の設定を追加します。各々、
file.crawl.parent: ファイルシステムを指定フォルダから子方向にクロールする
file.content.limit: 取得するファイルサイズを無制限にする
index.metadata: 取得するメタデータの指定

nutch-site.xml
  <property>
    <name>file.crawl.parent</name>
    <value>false</value>
  </property>
  <property>
    <name>file.content.limit</name>
    <value>-1</value>
  </property>
  <property>
    <name>index.metadata</name>
    <value>Last-Modified</value>
  </property>

hbase-site.xml の修正

クロールが途中で止まるエラーに対する対応

hbase-site.xml
  <property>
    <name>hbase.regionserver.lease.period</name>
    <value>1200000</value>
  </property>

クロールの設定

seed/url.txt にクロールするファイルシステムの最上位のフォルダを記載します。

url.txt
file://Users/chiyoda/work/docs/

フォルダ名の最後に / (スラッシュ)を付ける必要があるようです。

build/apache-nutch-2.3.1/runtime/local/conf/regex-urlfilter.txt を修正し、デフォルトでは有効になっていないファイルクロールを有効にします。また PowerPoint や Excel がクロールの対象になるように修正します。

regex-urlfilter.txt
# skip file: ftp: and mailto: urls
-^(ftp|mailto):

# skip image and other suffixes we can't yet parse
# for a more extensive coverage use the urlfilter-suffix plugin
-\.(gif|GIF|jpg|JPG|png|PNG|ico|ICO|css|CSS|sit|SIT|eps|EPS|wmf|WMF|zip|ZIP|ppt|PPT|mpg|MPG|xls|XLS|gz|GZ|rpm|RPM|tgz|TGZ|mov|MOV|exe|EXE|jpeg|JPEG|bmp|BMP|js|JS)$

それでは、クロールしてみます。

$ ./crawl
Injecting urls from ./seed/urls.txt
./build/apache-nutch-2.3.1/runtime/local/bin/nutch inject ./seed/urls.txt
InjectorJob: starting at 2018-06-01 15:06:10
InjectorJob: Injecting urlDir: seed/urls.txt
InjectorJob: Using class org.apache.gora.hbase.store.HBaseStore as the Gora storage class.
InjectorJob: total number of urls rejected by filters: 0
InjectorJob: total number of urls injected after normalization and filtering: 1
Injector: finished at 2018-06-01 15:06:14, elapsed: 00:00:03
Generate urls: 
./build/apache-nutch-2.3.1/runtime/local/bin/nutch generate -topN 5
GeneratorJob: starting at 2018-06-01 15:06:16
GeneratorJob: Selecting best-scoring urls due for fetch.
GeneratorJob: starting
GeneratorJob: filtering: true
GeneratorJob: normalizing: true
GeneratorJob: topN: 5
GeneratorJob: finished at 2018-06-01 15:06:19, time elapsed: 00:00:03
GeneratorJob: generated batch id: 1527833176-1112895280 containing 1 URLs
Fetch urls: 
./build/apache-nutch-2.3.1/runtime/local/bin/nutch fetch -all
FetcherJob: starting at 2018-06-01 15:06:21
FetcherJob: fetching all
FetcherJob: threads: 10
FetcherJob: parsing: false
FetcherJob: resuming: false
FetcherJob : timelimit set for : -1
Using queue mode : byHost
Fetcher: threads: 10
QueueFeeder finished: total 1 records. Hit by time limit :0
fetching file:///Users/chiyoda/work/docs/ (queue crawl delay=5000ms)
-finishing thread FetcherThread1, activeThreads=1
-finishing thread FetcherThread2, activeThreads=1
-finishing thread FetcherThread3, activeThreads=1

クロールを複数回実行すると、ファイルが Discovey に取り込まれたことが確認できました。

image.png

各フォルダの情報も一件として取り込まれてしまうようなので、検索時には以下のような条件設定をした方がよいかもしれません。

title:!"Index of /Users/chiyoda/work/docs"

image.png

4
4
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
4
4