import yargy
import unittest


class FactParserTestCase(unittest.TestCase):

    def test_simple_rules(self):
        text = "газета «Коммерсантъ» сообщила ..."
        parser = yargy.FactParser((
            ("word", {}),
            ("quote", {}),
            ("word", {}),
            ("quote", {}),
            ("$", {}))
        )
        results = parser.parse(text)
        self.assertEqual(sum([[w[1] for w in n] for n in results], []), ['газета', '«', 'Коммерсантъ', '»'])

    def test_repeat_rules(self):
        text = "... ООО «Коммерсантъ КАРТОТЕКА» уполномочено ..."
        parser = yargy.FactParser((
            ("quote", {}),
            ("word", {"repeat": True}),
            ("quote", {}),
            ("$", {}))
        )
        results = parser.parse(text)
        self.assertEqual(sum([[w[1] for w in n] for n in results], []), ['«', 'Коммерсантъ', 'КАРТОТЕКА', '»'])


    def test_gram_label(self):
        text = "маленький принц красиво пел"
        parser = yargy.FactParser((
            ("word", {"labels": [("gram", "ADJS")]}),
            ("word", {"labels": [("gram", "VERB")]}),
            ("$", {}))
        )
        results = parser.parse(text)
        self.assertEqual(sum([[w[1] for w in n] for n in results], []), ['красиво', 'пел'])

    def test_optional_rules(self):
        text = "великий новгород, москва."
        parser = yargy.FactParser((
            ("word", {"labels": [("gram", "ADJF")], "optional": True}),
            ("word", {"labels": [("gram", "NOUN"), ("gram", "Geox")]}),
            ("$", {}))
        )
        results = parser.parse(text)
        self.assertEqual([[w[1] for w in n] for n in results], [['великий', 'новгород'], ['москва']])
        
        text = "иван иванович иванов, анна смирнова"
        parser = yargy.FactParser((
            ("word", {"labels": [("gram", "NOUN"), ("gram", "Name")]}),
            ("word", {"labels": [("gram", "NOUN"), ("gram", "Patr")], "optional": True}),
            ("word", {"labels": [("gram", "NOUN"), ("gram", "Surn")]}),
            ("$", {}))
        )
        results = parser.parse(text)
        self.assertEqual([[w[1] for w in n] for n in results], [['иван', 'иванович', 'иванов'], ['анна', 'смирнова']])


@@ -1268,24 +1268,428 @@
 ', '%D0%BF%D0%B5%D0%BB'%5D)%0A%0A
+    def test_gram_not_label(self):%0A        text = %22%D0%98%D0%B2%D0%B0%D0%BD %D0%B2%D1%8B%D0%BF%D0%B8%D0%BB %D1%87%D0%B0%D1%8E. %D0%98 %D1%83%D1%88%D0%B5%D0%BB.%22%0A        parser = yargy.FactParser((%0A            (%22word%22, %7B%22labels%22: %5B(%22gram%22, %22Name%22), (%22gram-not%22, %22Abbr%22)%5D%7D),%0A            (%22word%22, %7B%22labels%22: %5B(%22gram%22, %22VERB%22)%5D%7D),%0A            (%22$%22, %7B%7D))%0A        )%0A        results = parser.parse(text)%0A        self.assertEqual(sum(%5B%5Bw%5B1%5D for w in n%5D for n in results%5D, %5B%5D), %5B'%D0%98%D0%B2%D0%B0%D0%BD', '%D0%B2%D1%8B%D0%BF%D0%B8%D0%BB'%5D)%0A%0A
     def test
@@ -1743,17 +1743,16 @@
 %D0%BD%D0%BE%D0%B2%D0%B3%D0%BE%D1%80%D0%BE%D0%B4
-,
  %D0%BC%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.

Add gram-not label test