1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from builtins import str, bytes, dict, int
from builtins import map, zip, filter
from builtins import object, range
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import unittest
import subprocess
from pattern import it
from io import open
try:
PATH = os.path.dirname(os.path.realpath(__file__))
except:
PATH = ""
#---------------------------------------------------------------------------------------------------
class TestInflection(unittest.TestCase):
def setUp(self):
pass
def test_article(self):
# Assert definite and indefinite article inflection.
for a, n, g in (
("il" , "giorno" , it.M),
("l'" , "altro giorno", it.M),
("lo" , "zio" , it.M),
("l'" , "amica" , it.F),
("la" , "nouva amica" , it.F),
("i" , "giapponesi" , it.M + it.PL),
("gli", "italiani" , it.M + it.PL),
("gli", "zii" , it.M + it.PL),
("le" , "zie" , it.F + it.PL)):
v = it.article(n, "definite", gender=g)
self.assertEqual(a, v)
for a, n, g in (
("uno", "zio" , it.M),
("una", "zia" , it.F),
("un" , "amico", it.M),
("un'", "amica", it.F)):
v = it.article(n, "indefinite", gender=g)
self.assertEqual(a, v)
v = it.referenced("amica", gender="f")
self.assertEqual(v, "un'amica")
print("pattern.it.article()")
print("pattern.it.referenced()")
def test_gender(self):
# Assert the accuracy of the gender disambiguation algorithm.
from pattern.db import Datasheet
i, n = 0, 0
for pos, sg, pl, mf in Datasheet.load(os.path.join(PATH, "corpora", "wordforms-it-wiktionary.csv"), encoding='utf8'):
g = it.gender(sg)
if mf in g and it.PLURAL not in g:
i += 1
g = it.gender(pl)
if mf in g and it.PLURAL in g:
i += 1
n += 2
self.assertTrue(float(i) / n > 0.92)
print("pattern.it.gender()")
def test_pluralize(self):
# Assert the accuracy of the pluralization algorithm.
from pattern.db import Datasheet
i, n = 0, 0
for pos, sg, pl, mf in Datasheet.load(os.path.join(PATH, "corpora", "wordforms-it-wiktionary.csv"), encoding='utf8'):
if it.pluralize(sg) == pl:
i += 1
n += 1
self.assertTrue(float(i) / n > 0.93)
print("pattern.it.pluralize()")
def test_singularize(self):
# Assert the accuracy of the singularization algorithm.
from pattern.db import Datasheet
i, n = 0, 0
for pos, sg, pl, mf in Datasheet.load(os.path.join(PATH, "corpora", "wordforms-it-wiktionary.csv"), encoding='utf8'):
if it.singularize(pl) == sg:
i += 1
n += 1
self.assertTrue(float(i) / n > 0.84)
print("pattern.it.singularize()")
def test_predicative(self):
# Assert the accuracy of the predicative algorithm ("cruciali" => "cruciale").
from pattern.db import Datasheet
i, n = 0, 0
for pos, sg, pl, mf in Datasheet.load(os.path.join(PATH, "corpora", "wordforms-it-wiktionary.csv"), encoding='utf8'):
if pos != "j":
continue
if it.predicative(pl) == sg:
i += 1
n += 1
self.assertTrue(float(i) / n > 0.87)
print("pattern.it.predicative()")
def test_find_lemma(self):
# Assert the accuracy of the verb lemmatization algorithm.
i, n = 0, 0
r = 0
for v1, v2 in it.inflect.verbs.inflections.items():
if it.inflect.verbs.find_lemma(v1) == v2:
i += 1
n += 1
self.assertTrue(float(i) / n > 0.81)
print("pattern.it.inflect.verbs.find_lemma()")
def test_find_lexeme(self):
# Assert the accuracy of the verb conjugation algorithm.
i, n = 0, 0
for v, lexeme1 in it.inflect.verbs.infinitives.items():
lexeme2 = it.inflect.verbs.find_lexeme(v)
for j in range(len(lexeme2)):
if lexeme1[j] == lexeme2[j]:
i += 1
n += 1
self.assertTrue(float(i) / n > 0.89)
print("pattern.it.inflect.verbs.find_lexeme()")
def test_conjugate(self):
# Assert different tenses with different conjugations.
for (v1, v2, tense) in (
("essere", "essere", it.INFINITIVE),
("essere", "sono", (it.PRESENT, 1, it.SINGULAR)),
("essere", "sei", (it.PRESENT, 2, it.SINGULAR)),
("essere", "è", (it.PRESENT, 3, it.SINGULAR)),
("essere", "siamo", (it.PRESENT, 1, it.PLURAL)),
("essere", "siete", (it.PRESENT, 2, it.PLURAL)),
("essere", "sono", (it.PRESENT, 3, it.PLURAL)),
("essere", "essendo", (it.PRESENT + it.PARTICIPLE)),
("essere", "stato", (it.PAST + it.PARTICIPLE)),
("essere", "ero", (it.IMPERFECT, 1, it.SINGULAR)),
("essere", "eri", (it.IMPERFECT, 2, it.SINGULAR)),
("essere", "era", (it.IMPERFECT, 3, it.SINGULAR)),
("essere", "eravamo", (it.IMPERFECT, 1, it.PLURAL)),
("essere", "eravate", (it.IMPERFECT, 2, it.PLURAL)),
("essere", "erano", (it.IMPERFECT, 3, it.PLURAL)),
("essere", "fui", (it.PRETERITE, 1, it.SINGULAR)),
("essere", "fosti", (it.PRETERITE, 2, it.SINGULAR)),
("essere", "fu", (it.PRETERITE, 3, it.SINGULAR)),
("essere", "fummo", (it.PRETERITE, 1, it.PLURAL)),
("essere", "foste", (it.PRETERITE, 2, it.PLURAL)),
("essere", "furono", (it.PRETERITE, 3, it.PLURAL)),
("essere", "sarei", (it.CONDITIONAL, 1, it.SINGULAR)),
("essere", "saresti", (it.CONDITIONAL, 2, it.SINGULAR)),
("essere", "sarebbe", (it.CONDITIONAL, 3, it.SINGULAR)),
("essere", "saremmo", (it.CONDITIONAL, 1, it.PLURAL)),
("essere", "sareste", (it.CONDITIONAL, 2, it.PLURAL)),
("essere", "sarebbero", (it.CONDITIONAL, 3, it.PLURAL)),
("essere", "sarò", (it.FUTURE, 1, it.SINGULAR)),
("essere", "sarai", (it.FUTURE, 2, it.SINGULAR)),
("essere", "sarà", (it.FUTURE, 3, it.SINGULAR)),
("essere", "saremo", (it.FUTURE, 1, it.PLURAL)),
("essere", "sarete", (it.FUTURE, 2, it.PLURAL)),
("essere", "saranno", (it.FUTURE, 3, it.PLURAL)),
("essere", "sii", (it.PRESENT, 2, it.SINGULAR, it.IMPERATIVE)),
("essere", "sia", (it.PRESENT, 3, it.SINGULAR, it.IMPERATIVE)),
("essere", "siamo", (it.PRESENT, 1, it.PLURAL, it.IMPERATIVE)),
("essere", "siate", (it.PRESENT, 2, it.PLURAL, it.IMPERATIVE)),
("essere", "siano", (it.PRESENT, 3, it.PLURAL, it.IMPERATIVE)),
("essere", "sia", (it.PRESENT, 1, it.SINGULAR, it.SUBJUNCTIVE)),
("essere", "sia", (it.PRESENT, 2, it.SINGULAR, it.SUBJUNCTIVE)),
("essere", "sia", (it.PRESENT, 3, it.SINGULAR, it.SUBJUNCTIVE)),
("essere", "siamo", (it.PRESENT, 1, it.PLURAL, it.SUBJUNCTIVE)),
("essere", "siate", (it.PRESENT, 2, it.PLURAL, it.SUBJUNCTIVE)),
("essere", "siano", (it.PRESENT, 3, it.PLURAL, it.SUBJUNCTIVE)),
("essere", "fossi", (it.PAST, 1, it.SINGULAR, it.SUBJUNCTIVE)),
("essere", "fossi", (it.PAST, 2, it.SINGULAR, it.SUBJUNCTIVE)),
("essere", "fosse", (it.PAST, 3, it.SINGULAR, it.SUBJUNCTIVE)),
("essere", "fossimo", (it.PAST, 1, it.PLURAL, it.SUBJUNCTIVE)),
("essere", "foste", (it.PAST, 2, it.PLURAL, it.SUBJUNCTIVE)),
("essere", "fossero", (it.PAST, 3, it.PLURAL, it.SUBJUNCTIVE))):
self.assertEqual(it.conjugate(v1, tense), v2)
print("pattern.it.conjugate()")
def test_lexeme(self):
# Assert all inflections of "essere".
v = it.lexeme("essere")
self.assertEqual(v, [
'essere', 'sono', 'sei', 'è', 'siamo', 'siete', 'essendo',
'fui', 'fosti', 'fu', 'fummo', 'foste', 'furono', 'stato',
'ero', 'eri', 'era', 'eravamo', 'eravate', 'erano',
'sarò', 'sarai', 'sarà', 'saremo', 'sarete', 'saranno',
'sarei', 'saresti', 'sarebbe', 'saremmo', 'sareste', 'sarebbero',
'sii', 'sia', 'siate', 'siano',
'fossi', 'fosse', 'fossimo', 'fossero'
])
print("pattern.it.inflect.lexeme()")
def test_tenses(self):
# Assert tense recognition.
self.assertTrue((it.PRESENT, 3, it.SG) in it.tenses("è"))
self.assertTrue("2sg" in it.tenses("sei"))
print("pattern.it.tenses()")
#---------------------------------------------------------------------------------------------------
class TestParser(unittest.TestCase):
def setUp(self):
pass
def test_find_lemmata(self):
# Assert lemmata for nouns, adjectives, verbs and determiners.
v = it.parser.find_lemmata([
["I", "DT"], ["gatti", "NNS"], ["neri", "JJ"],
["seduti", "VB"], ["sul", "IN"], ["tatami", "NN"]])
self.assertEqual(v, [
["I", "DT", "il"],
["gatti", "NNS", "gatto"],
["neri", "JJ", "nero"],
["seduti", "VB", "sedutare"],
["sul", "IN", "sul"],
["tatami", "NN", "tatami"]])
print("pattern.it.parser.find_lemmata()")
def test_parse(self):
# Assert parsed output with Penn Treebank II tags (slash-formatted).
# "il gatto nero" is a noun phrase, "sulla stuoia" is a prepositional noun phrase.
v = it.parser.parse("Il gatto nero seduto sulla stuoia.")
self.assertEqual(v,
"Il/DT/B-NP/O gatto/NN/I-NP/O nero/JJ/I-NP/O " +
"seduto/VB/B-VP/O " + \
"sulla/IN/B-PP/B-PNP stuoia/NN/B-NP/I-PNP ././O/O"
)
# Assert the accuracy of the Italian tagger.
i, n = 0, 0
with open(os.path.join(PATH, "corpora", "tagged-it-wacky.txt"), encoding='utf8') as f:
sentences = f.readlines()
for sentence in sentences:
sentence = sentence.strip()
s1 = [w.split("/") for w in sentence.split(" ")]
s2 = [[w for w, pos in s1]]
s2 = it.parse(s2, tokenize=False)
s2 = [w.split("/") for w in s2.split(" ")]
for j in range(len(s1)):
t1 = s1[j][1]
t2 = s2[j][1]
# WaCKy test set tags plural nouns as "NN", pattern.it as "NNS".
# Some punctuation marks are also tagged differently,
# but these are not necessarily errors.
if t1 == t2 or (t1 == "NN" and t2.startswith("NN")) or s1[j][0] in "\":;)-":
i += 1
n += 1
#print(float(i) / n)
self.assertTrue(float(i) / n > 0.92)
print("pattern.it.parser.parse()")
def test_tag(self):
# Assert [("il", "DT"), ("gatto", "NN"), ("nero", "JJ")].
v = it.tag("il gatto nero")
self.assertEqual(v, [("il", "DT"), ("gatto", "NN"), ("nero", "JJ")])
print("pattern.it.tag()")
def test_command_line(self):
# Assert parsed output from the command-line (example from the documentation).
p = ["python3", "-m", "pattern.it", "-s", "Il gatto nero.", "-OTCRL"]
with subprocess.Popen(p, stdout=subprocess.PIPE) as p:
p.wait()
v = p.stdout.read().decode('utf-8')
v = v.strip()
self.assertEqual(v, "Il/DT/B-NP/O/O/il gatto/NN/I-NP/O/O/gatto nero/JJ/I-NP/O/O/nero ././O/O/O/.")
print("python3 -m pattern.it")
#---------------------------------------------------------------------------------------------------
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestInflection))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestParser))
return suite
if __name__ == "__main__":
result = unittest.TextTestRunner(verbosity=1).run(suite())
sys.exit(not result.wasSuccessful())
|