Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Updated test_text.py #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 45 additions & 45 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,55 @@
from utils import isclose, DataFile


def test_unigram_text_model():
def test_text_models():
flatland = DataFile("EN-text/flatland.txt").read()
wordseq = words(flatland)
P = UnigramTextModel(wordseq)
P1 = UnigramTextModel(wordseq)
P2 = NgramTextModel(2, wordseq)
P3 = NgramTextModel(3, wordseq)

# The most frequent entries in each model
assert P1.top(10) == [(2081, 'the'), (1479, 'of'), (1021, 'and'),
(1008, 'to'), (850, 'a'), (722, 'i'), (640, 'in'),
(478, 'that'), (399, 'is'), (348, 'you')]

assert P2.top(10) == [(368, ('of', 'the')), (152, ('to', 'the')),
(152, ('in', 'the')), (86, ('of', 'a')),
(80, ('it', 'is')),
(71, ('by', 'the')), (68, ('for', 'the')),
(68, ('and', 'the')), (62, ('on', 'the')),
(60, ('to', 'be'))]

assert P3.top(10) == [(30, ('a', 'straight', 'line')),
(19, ('of', 'three', 'dimensions')),
(16, ('the', 'sense', 'of')),
(13, ('by', 'the', 'sense')),
(13, ('as', 'well', 'as')),
(12, ('of', 'the', 'circles')),
(12, ('of', 'sight', 'recognition')),
(11, ('the', 'number', 'of')),
(11, ('that', 'i', 'had')), (11, ('so', 'as', 'to'))]

s, p = viterbi_segment('itiseasytoreadwordswithoutspaces', P)
assert isclose(P1['the'], 0.0611, rel_tol=0.001)

assert isclose(P2['of', 'the'], 0.0108, rel_tol=0.01)

assert isclose(P3['', '', 'but'], 0.0, rel_tol=0.001)
assert isclose(P3['', '', 'but'], 0.0, rel_tol=0.001)
assert isclose(P3['so', 'as', 'to'], 0.000323, rel_tol=0.001)

assert P2.cond_prob.get(('went',)) is None

assert P3.cond_prob['in', 'order'].dictionary == {'to': 6}


def test_viterbi_segmentation():
flatland = DataFile("EN-text/flatland.txt").read()
wordseq = words(flatland)
P = UnigramTextModel(wordseq)
text = "itiseasytoreadwordswithoutspaces"

s, p = viterbi_segment(text,P)
assert s == [
'it', 'is', 'easy', 'to', 'read', 'words', 'without', 'spaces']

Expand Down Expand Up @@ -56,48 +98,6 @@ def test_counting_probability_distribution():
assert 1 / 7 <= min(ps) <= max(ps) <= 1 / 5


def test_ngram_models():
flatland = DataFile("EN-text/flatland.txt").read()
wordseq = words(flatland)
P1 = UnigramTextModel(wordseq)
P2 = NgramTextModel(2, wordseq)
P3 = NgramTextModel(3, wordseq)

# The most frequent entries in each model
assert P1.top(10) == [(2081, 'the'), (1479, 'of'), (1021, 'and'),
(1008, 'to'), (850, 'a'), (722, 'i'), (640, 'in'),
(478, 'that'), (399, 'is'), (348, 'you')]

assert P2.top(10) == [(368, ('of', 'the')), (152, ('to', 'the')),
(152, ('in', 'the')), (86, ('of', 'a')),
(80, ('it', 'is')),
(71, ('by', 'the')), (68, ('for', 'the')),
(68, ('and', 'the')), (62, ('on', 'the')),
(60, ('to', 'be'))]

assert P3.top(10) == [(30, ('a', 'straight', 'line')),
(19, ('of', 'three', 'dimensions')),
(16, ('the', 'sense', 'of')),
(13, ('by', 'the', 'sense')),
(13, ('as', 'well', 'as')),
(12, ('of', 'the', 'circles')),
(12, ('of', 'sight', 'recognition')),
(11, ('the', 'number', 'of')),
(11, ('that', 'i', 'had')), (11, ('so', 'as', 'to'))]

assert isclose(P1['the'], 0.0611, rel_tol=0.001)

assert isclose(P2['of', 'the'], 0.0108, rel_tol=0.01)

assert isclose(P3['', '', 'but'], 0.0, rel_tol=0.001)
assert isclose(P3['', '', 'but'], 0.0, rel_tol=0.001)
assert isclose(P3['so', 'as', 'to'], 0.000323, rel_tol=0.001)

assert P2.cond_prob.get(('went',)) is None

assert P3.cond_prob['in', 'order'].dictionary == {'to': 6}


def test_ir_system():
from collections import namedtuple
Results = namedtuple('IRResults', ['score', 'url'])
Expand Down