github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/nlp/wordnet/parser_test.go (about)

     1  package wordnet
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestDataParser(t *testing.T) {
    11  	expected := map[string]*Synset{
    12  		"v111": {
    13  			"111",
    14  			"v",
    15  			[]string{
    16  				"foo",
    17  				"bar",
    18  				"baz",
    19  			},
    20  			[]*Pointer{
    21  				{"!", "n123", -1, -1},
    22  				{"@", "a321", 0, 1},
    23  			},
    24  			[]*Frame{
    25  				{4, 4},
    26  				{6, 6},
    27  			},
    28  			"hello world",
    29  			nil,
    30  		},
    31  	}
    32  
    33  	actual := map[string]*Synset{}
    34  	err := parseDataFile(strings.NewReader(testData), "v", map[string][]int{},
    35  		actual)
    36  	if err != nil {
    37  		t.Fatal("Parsing error:", err)
    38  	}
    39  	if !reflect.DeepEqual(expected, actual) {
    40  		t.Error("Non-equal values:")
    41  		t.Error(stringify(expected))
    42  		t.Error(stringify(actual))
    43  	}
    44  	for key, ss := range actual {
    45  		if ss.Id() != key {
    46  			t.Errorf("ss.Id()=%v, want %v", ss.Id(), key)
    47  		}
    48  	}
    49  }
    50  
    51  func TestExceptionParser(t *testing.T) {
    52  	expected := map[string][]string{
    53  		"n.foo": {"n.bar"},
    54  		"n.baz": {"n.bla", "n.blu"},
    55  	}
    56  
    57  	actual := map[string][]string{}
    58  	err := parseExceptionFile(strings.NewReader(testException), "n", actual)
    59  	if err != nil {
    60  		t.Fatal("Parsing error:", err)
    61  	}
    62  	if !reflect.DeepEqual(expected, actual) {
    63  		t.Error("Non-equal values:")
    64  		t.Error(stringify(expected))
    65  		t.Error(stringify(actual))
    66  	}
    67  }
    68  
    69  func TestExampleIndexParser(t *testing.T) {
    70  	expected := map[string][]int{
    71  		"abash.37.0": {126, 127},
    72  		"abhor.37.0": {138, 139, 15},
    73  	}
    74  
    75  	actual, err := parseExampleIndex(strings.NewReader(testExampleIndex))
    76  	if err != nil {
    77  		t.Fatal("Parsing error:", err)
    78  	}
    79  	if !reflect.DeepEqual(expected, actual) {
    80  		t.Error("Non-equal values:")
    81  		t.Error(expected)
    82  		t.Error(actual)
    83  	}
    84  }
    85  
    86  func TestExampleParser(t *testing.T) {
    87  	expected := map[string]string{
    88  		"111": "hello world",
    89  		"222": "goodbye universe",
    90  	}
    91  
    92  	actual, err := parseExamples(strings.NewReader(testExamples))
    93  	if err != nil {
    94  		t.Fatal("Parsing error:", err)
    95  	}
    96  	if !reflect.DeepEqual(expected, actual) {
    97  		t.Error("Non-equal values:")
    98  		t.Error(expected)
    99  		t.Error(actual)
   100  	}
   101  }
   102  
   103  func TestIndexParser(t *testing.T) {
   104  	expected := map[string][]string{
   105  		"n.thing":  {"na", "nb"},
   106  		"v.thing2": {"vc", "vd"},
   107  	}
   108  
   109  	actual, err := parseIndex(strings.NewReader(testIndex))
   110  	if err != nil {
   111  		t.Fatal("Parsing error:", err)
   112  	}
   113  	if !reflect.DeepEqual(expected, actual) {
   114  		t.Error("Non-equal values:")
   115  		t.Error(expected)
   116  		t.Error(actual)
   117  	}
   118  }
   119  
   120  func stringify(a interface{}) string {
   121  	j, _ := json.Marshal(a)
   122  	return string(j)
   123  }
   124  
   125  var testData = `  copyright line
   126  111 1 v 3 foo 1 bar 3 baz 5 2 ! 123 n 0000 @ 321 a 0102 2 + 4 5 + 6 7 | hello world`
   127  
   128  var testException = `foo bar
   129  baz bla blu`
   130  
   131  var testExampleIndex = `abash%2:37:00:: 126,127
   132  abhor%2:37:00:: 138,139,15`
   133  
   134  var testExamples = `111 hello world
   135  222 goodbye universe`
   136  
   137  var testIndex = `  copyright line
   138  thing n 2 3 x y z 2 2 a b
   139  thing2 v 4 1 x 4 2 c d e f`