Colabにデータファイルから分析する機能が追加されました。さっそく、吉川英治の三国志(青空文庫)を分析させます。利用するテキストデータは、予め青空文庫からデータをプレーンテキストにパースしておきます。文字数は1,625,493文字となり、量はあります。データそのものをすべてLLMに突っ込んで解釈させる方法でもないので、大きさは関係ないのかもしれませんが、不用意にprintしちゃうと困っちゃうサイズ感ではあります。小説なので分析はやりにくいだろうと思いつつ開始。
日本語の解析ではやはりいろいろと厳しい面もありますが、指示を変えながらざっくりどんなことできそうかと試行錯誤はできそうです。また、出力の分析もGeminiがやってくれるので参考になります。最後に結果をまとめてくれるのも便利です。ただ、ルーチンのデータ解析ならフレームワークを利用したほうが速いのではと思います。慣れてきて、プロンプトが決まってくると便利になりそうです。
解説やまとめは日本語で指示すると素直に従ってくれました。
エラー発生時にポイントポイントでエージェントを利用できたら便利ですが、そういう建付けにはなったないのは残念です。
まずはざっくりやってみた。
「日本語の文章です。日本語の形態素解析を行い、曹操の心情を分析したい。ネットワーク図などを利用し詳細に図示してください。」と具体的に指示をし、データをuploadします。
結果は、曹操に関しては恐れと怒りの記述がそこそこあったんだなという分析にはなりましたが、分析そのものは、これをベースにやることがありそう。今回は、Colabがどういう動きをするのかが重要です。エラーが出ても、理由を説明して勝手に解決して進めます。もともとインストールされてないライブラリーも一度インストールしてエラーを出してからインストールするのはご愛敬でしょうか。
他には、matplotlibは日本語が豆腐になったので後付けで
!pip install japanize-matplotlib
import matplotlib.pyplot as plt
import japanize_matplotlib
として、日本語を表示できるようにして再実行しました。Colabならやりやすい。Network図も日本語表示できてなかったので、そこは別途追加が必要ですが、今回は実施していません。この辺りもGeminiに聞きながら手を入れていくというのはありです。
引き続きLDAで分析を実施させてみます。
「日本語の文章です。LDAを用いて文章の内容を大きく3つに分けて解析をしてください。」という雑な指示です。3つに分けられるかは謎です。
LDAのパラメータの弄りだしました。警告メッセージも見ている感じですね。
Topic 0: Seems to represent common Japanese particles and pronouns, suggesting a general topic.
Topic 1: Dominated by quotation marks, suggesting dialogue or quoted text.
Topic 2: Appears to be related to military or historical context.
に分析してくれました。ワードクラウドをみるとなんとなくそんな感じがするかもな・・・という感じです。
もう少し具体的に指示をしてみます。
これまでの味見を活かして、具体的に指示してみます。
「日本語で書かれた三国志の文章です。曹操軍の動きを解析して、軍隊の動きを説明してください。まずは、軍事に関する言葉を抜き出して分析する準備をしてから解析を始めます。また、適宜、検索も実施してより広い知識を加えてください。解説やまとめは日本語で行ってください。また、以下の事項も考慮してください。
1. 時間表現の精度の向上
より詳細な時間情報を含む表現を抽出するため、正規表現パターンを改良します。
例えば、"建安元年"、"三月"、"五日" といった表現を抽出できるようにパターンを拡張します。
抽出した時間情報を元に、イベントの発生順序をより正確に推定します。
2. 場所情報の精度の向上
地名と軍事行動を同時に含む文章を抽出します。
例えば、"許昌から出兵"、"官渡で戦う" といった表現を抽出します。
これにより、地名と軍事行動の関連性を明確化し、軍隊の移動経路などを把握します。
3. 軍隊の規模や編成情報の抽出
軍隊の規模や編成に関するキーワードを定義します。
例えば、"一万の兵"、"騎兵隊"、"歩兵隊" といったキーワードを定義します。
これらのキーワードを含む文章を抽出し、軍隊の規模や編成に関する情報を分析します。
4. 外部知識の活用
三国志に関する歴史的知識や地理情報などを外部から取得します。
ウィキペディアやDBpediaなどのデータベース、地理情報APIなどを活用します。
これらの情報を分析結果と照らし合わせ、より深い分析を行います。
5. より高度な自然言語処理技術の導入
固有名詞認識、関係抽出、イベント抽出といった技術を導入します。
これらの技術を活用することで、より詳細な情報をテキストから抽出します。
例えば、固有名詞認識を用いて人物名や地名を正確に識別し、関係抽出を用いて人物間の関係や場所間の距離などを分析します。
」
結果として、マップ上での表示までは作成できませんでしたが、いろいろと頑張ってくれました。図示されてものよりも途中で抜き出したデータが使えそうだなと思います。適宜データを保存するようなプロンプトを追加しておけばよかったと感じます。
サマリーです。
Summary:
1. Q&A
The analysis aimed to understand the sequence of Cao Cao's military movements and actions based on the provided text data. The key questions addressed were:What were the key events in Cao Cao's military campaigns? The analysis extracted time, location, military actions, and personnel involved from the text, grouping related information into potential events, ordered chronologically.
Where did Cao Cao's forces move, and what actions did they undertake? The analysis attempted to identify movements by observing consecutive locations in the sorted events. Military actions were linked with locations to depict the strategic context of the campaign.
When did these events take place? The analysis parsed various date formats and ordered the events chronologically. The parse_time function handled different time representations like "建安五年" and standard year/month/day formats.
Due to limitations in external knowledge retrieval, information on troop size and specific battle outcomes was not reliably added to the timeline.2. Data Analysis Key Findings
Chronological Timeline: The analysis successfully created a chronological timeline of events, parsing various time formats and sorting events accordingly. The ability to parse the Jian'an era (e.g., "建安五年") was particularly important.
Event Grouping with Proximity: Events were grouped not just by time but also by proximity within the text. This improved the association of details (location, actions, personnel) with specific events. A proximity threshold of 100 characters was used.
External Knowledge Integration (Partial): The analysis attempted to enrich the timeline with information from Wikipedia, including summaries and URLs for key figures (Cao Cao, Liu Bei, Sun Quan, and Zhuge Liang). While the code correctly identified these key figures, the retrieval of geographical information and battles was unsuccessful.
3. Insights or Next Steps
Improve External Data Retrieval: Enhance the external data retrieval process by: exploring alternative APIs or knowledge bases, refining search queries, and implementing more robust error handling for disambiguation issues and API failures. Coordinate extraction for battles and locations.
Visual Enhancement: Address the font rendering issues to correctly display Chinese characters in the visualizations. Investigate alternative visualization libraries or explore using different fonts to achieve the intended presentation. Consider incorporating geographical maps to display military movements more effectively.
この時のColabノート: