『IronPython の世界』を読む (3) の続き。
Part 4 .NET Framework のライブラリを使う p.210 のあたりを読む。.NET Framework のライブラリを使うときには、通常のモジュールのインポートと違って、あらかじめ参照を追加しなければならないのね。参照を追加しない状態で System.Xml としてもエラーになる。clr.AddReference で参照を追加すればインポートできるようになる。
IronPython 1.1.1 (1.1.1) on .NET 2.0.50727.1433
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import System
>>> import System.Xml
Traceback (most recent call last):
File , line 0, in <stdin>##10
File , line 0, in __import__##4
ImportError: No module named Xml
>>> import clr
>>> clr.AddReference("System.Xml")
>>> import System.Xml
じゃあ、先日、
自然言語処理関連のライブラリ SlothLib をちょこっと使う とか使えるかなぁとやってみる。野生の直感で適当にやる。やり方は正しいかよく分からないが、とりあえず、IronPython から使うことができた。
IronPython 1.1.1 (1.1.1) on .NET 2.0.50727.1433
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import clr
>>> import sys
>>>
>>> SlothLibPath = "\\Pathto\\SlothLib"
>>> sys.path.append(SlothLibPath)
>>>
>>> clr.AddReference("SlothLib.Web.Search.Base.dll")
>>> clr.AddReference("SlothLib.Web.Search.YahooJapanWebService.dll")
>>> import SlothLib.Web.Search
>>> yahoolib = SlothLib.Web.Search.YahooJpWebSearch
>>> dir(yahoolib)
['AdultOk', 'Country', 'DoSearch', 'Equals', 'Finalize', 'Format', 'GetHashCode'
, 'GetTotalNumber', 'GetType', 'Language', 'MakeDynamicType', 'MemberwiseClone',
'Proxy', 'Reduce', 'ReferenceEquals', 'SearchFormat', 'SearchType', 'SimilarOk'
, 'Site', 'ToString', 'Type', '__class__', '__doc__', '__init__', '__module__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__']
>>> yahoo = yahoolib("ApplicationID")
>>> result = yahoo.DoSearch("Python", 10)
>>> result.TotalResultsAvailable
5840000L
>>> result.ResultElements
SlothLib.Web.Search.YahooJpWebElement[](<SlothLib.Web.Search.YahooJpWebElement o
bject at 0x000000000000002B [Title: Python Japan User's Group...]>, <SlothLib.We
b.Search.YahooJpWebElement object at 0x000000000000002C [Title: Pythonの紹介...]
>, <SlothLib.Web.Search.YahooJpWebElement object at 0x000000000000002D [Title: P
ython - Wikipedia...]>, <SlothLib.Web.Search.YahooJpWebElement object at 0x00000
0000000002E [Title: Python...]>, <SlothLib.Web.Search.YahooJpWebElement object a
t 0x000000000000002F [Title: Pythonとは?...]>, <SlothLib.Web.Search.YahooJpWebEl
ement object at 0x0000000000000030 [Title: Python...]>, <SlothLib.Web.Search.Yah
ooJpWebElement object at 0x0000000000000031 [Title: oreilly.co.jp -- Online Cata
log ......]>, <SlothLib.Web.Search.YahooJpWebElement object at 0x000000000000003
2 [Title: Pythonとは - 意味・解説 : IT用語辞典...]>, <SlothLib.Web.Search.YahooJ
pWebElement object at 0x0000000000000033 [Title: Python Corner...]>, <SlothLib.W
eb.Search.YahooJpWebElement object at 0x0000000000000034 [Title: 「Python」最新
記事一覧 - ITmedia Keywords...]>)
>>> for r in result.ResultElements:
... print r.Title
...
Python Japan User's Group
Pythonの紹介
Python - Wikipedia
Python
Pythonとは?
Python
oreilly.co.jp -- Online Catalog ...
Pythonとは - 意味・解説 : IT用語辞典
Python Corner
「Python」最新記事一覧 - ITmedia Keywords
Visual Studio と C# を使わなくても、IronPython から使えるところが嬉しい。とりあえず、使えそうなので先に進むことにする。