Skip to main content

Pure-math text intelligence engine for Japanese — vocabulary richness, contamination detection, writing habit analysis, character voice fingerprinting

Project description

japhrase

辞書もLLMも使わずに、テキストからフレーズを見つける。

License: MIT Python 3.8+ PyPI Tests

pip install japhrase

何をするツールか

日本語テキストの中から、繰り返し出現するフレーズを統計だけで検出する。MeCab も辞書ファイルも外部 AI も要らない。テキストを入れれば、そこに何度も現れているフレーズが出てくる——それが既知語であろうと、辞書に載っていない新語・造語・専門用語であろうと関係ない。

N-gram による頻度抽出

テキストを「連続する N 文字」の断片(N-gram)に分解し、出現頻度を数える。

たとえば生成 AI に関する記事を入力すると:

from japhrase import PhraseExtracter

extractor = PhraseExtracter(min_count=2, max_length=10, min_length=2)
df = extractor.extract(sentences)
     seqchar  freq
      ている    12   ← 意味のないフレーズも拾ってしまう
      生成AI    10
  大規模言語モデル     4
      である     4   ← これも意味がない

「生成AI」「大規模言語モデル」は見つかった。しかし「ている」「である」のような、どんなテキストにも現れる意味のないフレーズも一緒に出てくる。N-gram の頻度だけでは、意味のある結合と偶然の並びを区別できない。

PMI による洗練

ここで PMI(自己相互情報量)を使う。PMI は「2つの要素が偶然一緒に現れる確率」と「実際に一緒に現れた頻度」の比を測る。

extractor = PhraseExtracter(min_count=2, use_pmi=True)
df = extractor.extract(sentences)
     seqchar  freq    pmi
      生成AI    10   10.0   ← PMI が高い = 意味のある結合
  大規模言語モデル     4   10.0   ← 偶然ではない
      である     4    7.2   ← PMI が低い = ありふれた並び

PMI が高いフレーズは、構成要素が偶然隣り合っただけでは説明できないほど頻繁に共起している。「生成」と「AI」がこれほど一緒に出現するのは、それが1つの意味単位だからだ。一方「である」はどんな文脈にも現れるので PMI が低くなる。

PMI を有効にすることで、意味的に結合したフレーズだけを上位に浮かび上がらせることができる。

分岐エントロピーによる境界検出

さらに分岐エントロピーを加えると、フレーズの自然な切れ目を特定できる。「あるフレーズの次にどんな文字が来るか」の多様性を測り、多様性が急に上がる位置をフレーズの境界とみなす。

extractor = PhraseExtracter(min_count=3, use_pmi=True, use_branching_entropy=True)
df = extractor.extract(sentences)

N-gram が「繰り返されているもの」を広く拾い、PMI が「意味のある結合」だけを残し、分岐エントロピーが「切れ目」を正確にする。この3層が japhrase のコアである。

プリセット

テキスト種別ごとに最適化されたパラメータセット(Optuna による実験的最適化済み)。

extractor = PhraseExtracter.preset('sns')     # SNS/Twitter(短文・高頻度)
extractor = PhraseExtracter.preset('news')    # ニュース(専門用語重視)
extractor = PhraseExtracter.preset('novel')   # 小説(繰り返し表現・長め)
extractor = PhraseExtracter.preset('report')  # 論文/レポート(定型・学術用語)

ファイルからの読み込み

df = PhraseExtracter.from_file("input.txt")
# エンコーディング自動検出(UTF-8 / Shift-JIS / EUC-JP)

コアの上に積み上げた分析機能

フレーズ抽出エンジンを土台として、以下の統計分析機能を提供する。

テキスト比較・分析

機能 説明 主な指標
SimilarityAnalyzer 複数テキスト間の類似度計算 Jaccard / Levenshtein / コサイン
DistributionComparator 2テキスト間の語彙分布比較 JS距離、対数尤度比(G²)、Effect Size、キーネス
CollocationScorer 語の結びつきの強さを多角評価 PMI、MI³、t値、z値、Log-Dice、Delta-P
CooccurrenceAnalyzer ターゲット語の周辺の特異語を抽出 コンテキスト頻度、特異度スコア
TemporalAnalyzer 複数テキスト間の時系列変化を追跡 バースト検出、語彙飽和度、トレンド

計量言語学

機能 説明 主な指標
StylometryAnalyzer 語彙多様性の定量測定 TTR、MATTR、Hapax比、Brunet's W、Honoré's R、Simpson's D、Heaps則
ComplexityAnalyzer テキスト複雑度・情報密度 パープレキシティ、圧縮率、語彙密度、情報率
StatisticalScorer フレーズの統計的有意性評価 カイ二乗、相互情報量、Zipf異常、Wilson信頼区間

テキスト品質

機能 説明
TextVariantDetector 表記ゆれ検出(サーバー/サーバ、出来る/できる)
WritingHabitDetector 高頻度×低PMIで書き癖を検出
Summarizer 統計的要約(LLM不要)

その他

  • 汚染検出: 文字化け・重複・句読点揺れ等を8軸で検出(japhrase.contamination
  • 執筆ワークフロー: 公開前品質ゲート、話数間推移、キャラ文体指紋等(japhrase.applied
  • NMF文書ベクトル化 / ストリーミング処理 / パラメータ自動最適化 / 自動インサイト生成
  • 出力形式: CSV、JSON、Excel、HTML レポート

インストール

pip install japhrase                  # コア(numpy, pandas, scipy)
pip install japhrase[similarity]      # + sklearn, Levenshtein
pip install japhrase[viz]             # + matplotlib, seaborn
pip install japhrase[all]             # 全部入り

Python 3.8+

使い方を調べる

CLI

japhrase --help                     # コマンド一覧
japhrase extract --help             # フレーズ抽出の引数とオプション
japhrase stats --help               # 統計出力の形式指定
japhrase check --help               # 文書品質チェック
japhrase detect-habits --help       # 書き癖検出

Python

全クラス・全関数に docstring がある。help() で引数・戻り値・使用例が出る。

# フレーズ抽出
from japhrase import PhraseExtracter
help(PhraseExtracter)               # クラス全体
help(PhraseExtracter.extract)       # 個別メソッド
help(PhraseExtracter.preset)        # プリセットの使い方

# 統計エンジン
from japhrase import DistributionComparator, CollocationScorer
from japhrase import StylometryAnalyzer, ComplexityAnalyzer, TemporalAnalyzer
help(DistributionComparator)        # 2テキスト間の分布比較
help(CollocationScorer)             # 語の結びつきの強さ(6指標)
help(StylometryAnalyzer)            # 語彙多様性(7指標)
help(ComplexityAnalyzer)            # テキスト複雑度
help(TemporalAnalyzer)              # 時系列分析

# 汚染検出
from japhrase.contamination import scan, quick_check, compare, batch_scan
help(scan)                          # 8軸汚染スキャン
help(quick_check)                   # True/False だけ返す最小API

# 執筆ワークフロー
from japhrase.applied import PreflightChecker, EPDashboard, PartHealthReport
help(PreflightChecker)              # 公開前品質ゲート
help(EPDashboard)                   # 話数間推移ダッシュボード
help(PartHealthReport)              # A〜E 健康診断

サンプルスクリプト

python examples/quickstart.py    # 全機能の動作確認(コピペで動く)

使用ガイド

ファイルから抽出

from japhrase import PhraseExtracter

df = PhraseExtracter.from_file("input.txt")                          # テキスト
df = PhraseExtracter.from_file("data.csv", column="text")            # CSV(列指定)
df = PhraseExtracter.from_file("input.txt", min_count=10, max_length=20)  # パラメータ指定

パラメータ

extractor = PhraseExtracter(
    min_count=6,                # 最小出現回数(小さいと計算時間が増える)
    max_length=16,              # フレーズの最大文字数
    min_length=4,               # フレーズの最小文字数
    threshold_originality=0.5,  # 類似フレーズ除去の閾値(0.0〜1.0)
    use_pmi=True,               # PMI で意味的結合を評価
    use_branching_entropy=True, # 分岐エントロピーで境界を検出
    knowns=["既知語1"],         # 優先的に抽出したい既知語
    verbose=1,                  # 進捗表示(0:非表示, 1:表示)
)

結果のエクスポート

extractor.export_csv(df, "output.csv")      # CSV(Excel対応BOM付きUTF-8)
extractor.export_json(df, "output.json")    # JSON
extractor.export_excel(df, "output.xlsx")   # Excel(要 pip install japhrase[excel])

対応ファイル形式

テキスト(.txt)、CSV(.csv)、TSV(.tsv)。エンコーディングは自動検出(UTF-8 / Shift-JIS / EUC-JP)。

トラブルシューティング

# メモリ不足 → バッチサイズを小さくする
extractor = PhraseExtracter(size_sentence=1000)

# 処理が遅い → min_count を上げる / max_length を下げる
extractor = PhraseExtracter(min_count=20, max_length=10)

# 結果が多すぎる → 類似度閾値を上げる
extractor = PhraseExtracter(threshold_originality=0.8)

テスト

pytest                   # 290件以上
pytest --cov=japhrase    # カバレッジ付き

English Summary

japhrase finds phrases in Japanese text without a dictionary or LLM. It scans for recurring N-gram patterns, then uses PMI (pointwise mutual information) to separate meaningful collocations from noise — "生成AI" ranks high because its components co-occur far more than chance predicts, while "である" ranks low. Branching entropy further refines phrase boundaries. On top of this core: text similarity, distribution comparison (JSD, G², keyness), collocation scoring (6 metrics), vocabulary richness (7 metrics), text complexity, and more. Pure math, no external AI, 290+ tests, numpy/scipy only.

ライセンス

MIT License — Takeshi SHIMIZU

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

japhrase-0.3.5.tar.gz (795.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

japhrase-0.3.5-py3-none-any.whl (281.3 kB view details)

Uploaded Python 3

File details

Details for the file japhrase-0.3.5.tar.gz.

File metadata

  • Download URL: japhrase-0.3.5.tar.gz
  • Upload date:
  • Size: 795.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for japhrase-0.3.5.tar.gz
Algorithm Hash digest
SHA256 5bf0b691d280def695290cf36cb93a36c7151bcc58459a94bf60c1bb01b806a5
MD5 790a2cf612a9d3bd1bdce55c96afa328
BLAKE2b-256 6d10ba4ff596817224cd31dc8173ec0973b61f1f78e11ec44ccc7da309629671

See more details on using hashes here.

File details

Details for the file japhrase-0.3.5-py3-none-any.whl.

File metadata

  • Download URL: japhrase-0.3.5-py3-none-any.whl
  • Upload date:
  • Size: 281.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for japhrase-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e0451072858363035b723039d5d5ae1e5f6e8117b5b641892de58fda8b69e7df
MD5 c7afbe5126102592852313d78ccc1a6d
BLAKE2b-256 cda22ed69f12774a57ad16c6633df0d7593dfff709fda1dddb73086d13ab26fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page