|
| 1 | +""" |
| 2 | +Module for running a bunch of simple unit tests. Should be expanded more in |
| 3 | +the future. |
| 4 | +
|
| 5 | +:author: Nitin Madnani (nmadnani@ets.org) |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import (absolute_import, division, print_function, |
| 9 | + unicode_literals) |
| 10 | + |
| 11 | +import itertools |
| 12 | +import os |
| 13 | + |
| 14 | +from io import open |
| 15 | +from os.path import abspath, dirname, exists, join |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +from nose.tools import eq_, raises, assert_equal, assert_not_equal |
| 19 | + |
| 20 | +_my_dir = abspath(dirname(__file__)) |
| 21 | + |
| 22 | + |
| 23 | +def check_dep_parse_sentence(tokenize=False): |
| 24 | + """ |
| 25 | + Check dep_parse_sentence method with and without tokenization |
| 26 | + """ |
| 27 | + from tests import depparser |
| 28 | + |
| 29 | + sentence = "I'm going to the market." if tokenize else "I 'm going to the market ." |
| 30 | + correct_output = "I\tPRP\t1\tSUB\n'm\tVBP\t-1\tROOT\ngoing\tVBG\t1\tVC\nto\tTO\t2\tVMOD\nthe\tDT\t5\tNMOD\nmarket\tNN\t3\tPMOD\n.\t.\t1\tP\n" |
| 31 | + parsed_sentence = depparser.dep_parse_sentence(sentence, tokenize=tokenize) |
| 32 | + assert_equal(parsed_sentence, correct_output) |
| 33 | + |
| 34 | + |
| 35 | +def test_dep_parse_sentence(): |
| 36 | + yield check_dep_parse_sentence, False |
| 37 | + yield check_dep_parse_sentence, True |
| 38 | + |
| 39 | + |
| 40 | +def check_dep_parse_file(tokenize=False): |
| 41 | + """ |
| 42 | + Check parse_file method with and without tokenization |
| 43 | + """ |
| 44 | + |
| 45 | + from tests import depparser |
| 46 | + |
| 47 | + prefix = 'test' if tokenize else 'test_tokenized' |
| 48 | + |
| 49 | + correct_output = ['I\tPRP\t1\tSUB', 'am\tVBP\t-1\tROOT', |
| 50 | + 'going\tVBG\t1\tVC', 'to\tTO\t2\tVMOD', |
| 51 | + 'the\tDT\t5\tNMOD', 'market\tNN\t3\tPMOD', |
| 52 | + '.\t.\t1\tP', '', 'Are\tVBP\t-1\tROOT', |
| 53 | + 'you\tPRP\t0\tSUB', 'going\tVBG\t0\tVMOD', |
| 54 | + 'to\tTO\t4\tVMOD', 'come\tVB\t2\tVMOD', |
| 55 | + 'with\tIN\t4\tVMOD', 'me\tPRP\t5\tPMOD', |
| 56 | + '?\t.\t0\tP', ''] |
| 57 | + |
| 58 | + input_file = abspath(join(_my_dir, '..', 'examples', '{}.txt'.format(prefix))) |
| 59 | + output_file = abspath(join(_my_dir, '..', 'examples', '{}.dep'.format(prefix))) |
| 60 | + |
| 61 | + # dependency parse the file |
| 62 | + depparser.dep_parse_file(input_file, output_file, tokenize=tokenize) |
| 63 | + |
| 64 | + # read the output file and make sure we have the expected output |
| 65 | + with open(output_file, 'r') as outf: |
| 66 | + output = [l.strip() for l in outf.readlines()] |
| 67 | + |
| 68 | + assert_equal(output, correct_output) |
| 69 | + |
| 70 | + |
| 71 | +def test_dep_parse_file(): |
| 72 | + yield check_dep_parse_file, False |
| 73 | + yield check_dep_parse_file, True |
0 commit comments