nltk--nltk
3454a55636
cffconvert / validate (push) Has been cancelled
ci-workflow / pre-commit (push) Has been cancelled
ci-workflow / Minimal NLTK Download Test (macos-latest) (push) Has been cancelled
ci-workflow / Minimal NLTK Download Test (ubuntu-latest) (push) Has been cancelled
ci-workflow / Minimal NLTK Download Test (windows-latest) (push) Has been cancelled
ci-workflow / Python 3.10 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.11 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.12 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.13 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.14 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.14t on macos-latest (push) Has been cancelled
ci-workflow / Python 3.10 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.11 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.12 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.13 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.14 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.14t on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.10 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.11 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.12 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.13 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.14 on windows-latest (push) Has been cancelled
67 行
1.8 KiB
Python
67 行
1.8 KiB
Python
import unittest
|
|
|
|
from nltk.metrics import (
|
|
BigramAssocMeasures,
|
|
QuadgramAssocMeasures,
|
|
TrigramAssocMeasures,
|
|
)
|
|
|
|
## Test the likelihood ratio metric
|
|
|
|
_DELTA = 1e-8
|
|
|
|
|
|
class TestLikelihoodRatio(unittest.TestCase):
|
|
def test_lr_bigram(self):
|
|
self.assertAlmostEqual(
|
|
BigramAssocMeasures.likelihood_ratio(2, (4, 4), 20),
|
|
2.4142743368419755,
|
|
delta=_DELTA,
|
|
)
|
|
self.assertAlmostEqual(
|
|
BigramAssocMeasures.likelihood_ratio(1, (1, 1), 1), 0.0, delta=_DELTA
|
|
)
|
|
self.assertRaises(
|
|
ValueError,
|
|
BigramAssocMeasures.likelihood_ratio,
|
|
*(0, (2, 2), 2),
|
|
)
|
|
|
|
def test_lr_trigram(self):
|
|
self.assertAlmostEqual(
|
|
TrigramAssocMeasures.likelihood_ratio(1, (1, 1, 1), (1, 1, 1), 2),
|
|
5.545177444479562,
|
|
delta=_DELTA,
|
|
)
|
|
self.assertAlmostEqual(
|
|
TrigramAssocMeasures.likelihood_ratio(1, (1, 1, 1), (1, 1, 1), 1),
|
|
0.0,
|
|
delta=_DELTA,
|
|
)
|
|
self.assertRaises(
|
|
ValueError,
|
|
TrigramAssocMeasures.likelihood_ratio,
|
|
*(1, (1, 1, 2), (1, 1, 2), 2),
|
|
)
|
|
|
|
def test_lr_quadgram(self):
|
|
self.assertAlmostEqual(
|
|
QuadgramAssocMeasures.likelihood_ratio(
|
|
1, (1, 1, 1, 1), (1, 1, 1, 1, 1, 1), (1, 1, 1, 1), 2
|
|
),
|
|
8.317766166719343,
|
|
delta=_DELTA,
|
|
)
|
|
self.assertAlmostEqual(
|
|
QuadgramAssocMeasures.likelihood_ratio(
|
|
1, (1, 1, 1, 1), (1, 1, 1, 1, 1, 1), (1, 1, 1, 1), 1
|
|
),
|
|
0.0,
|
|
delta=_DELTA,
|
|
)
|
|
self.assertRaises(
|
|
ValueError,
|
|
QuadgramAssocMeasures.likelihood_ratio,
|
|
*(1, (1, 1, 1, 1), (1, 1, 1, 1, 1, 2), (1, 1, 1, 1), 1),
|
|
)
|