Example #1
0
    def test_extractnumber_pt(self):
        self.assertEqual(extractnumber("isto e o primeiro teste", lang="pt"),
                         1)
        self.assertEqual(extractnumber("isto e o 2 teste", lang="pt"), 2)
        self.assertEqual(extractnumber("isto e o segundo teste", lang="pt"),
                         2)
        self.assertEqual(extractnumber(u"isto e um ter�o de teste",
                                       lang="pt"), 1.0 / 3.0)
        self.assertEqual(extractnumber("isto e o teste numero quatro",
                                       lang="pt"), 4)
        self.assertEqual(extractnumber(u"um ter�o de chavena", lang="pt"),
                         1.0 / 3.0)
        self.assertEqual(extractnumber("3 canecos", lang="pt"), 3)
        self.assertEqual(extractnumber("1/3 canecos", lang="pt"), 1.0 / 3.0)
        self.assertEqual(extractnumber("quarto de hora", lang="pt"), 0.25)
        self.assertEqual(extractnumber("1/4 hora", lang="pt"), 0.25)
        self.assertEqual(extractnumber("um quarto hora", lang="pt"), 0.25)
        self.assertEqual(extractnumber("2/3 pinga", lang="pt"), 2.0 / 3.0)
        self.assertEqual(extractnumber("3/4 pinga", lang="pt"), 3.0 / 4.0)
        self.assertEqual(extractnumber("1 e 3/4 cafe", lang="pt"), 1.75)
        self.assertEqual(extractnumber("1 cafe e meio", lang="pt"), 1.5)
        self.assertEqual(extractnumber("um cafe e um meio", lang="pt"), 1.5)
        self.assertEqual(
            extractnumber("tres quartos de chocolate", lang="pt"),
            3.0 / 4.0)
        self.assertEqual(extractnumber(u"tr�s quarto de chocolate",
                                       lang="pt"), 3.0 / 4.0)
        self.assertEqual(extractnumber("sete ponto cinco", lang="pt"), 7.5)
        self.assertEqual(extractnumber("sete ponto 5", lang="pt"), 7.5)
        self.assertEqual(extractnumber("sete e meio", lang="pt"), 7.5)
        self.assertEqual(extractnumber("sete e oitenta", lang="pt"), 7.80)
        self.assertEqual(extractnumber("sete e oito", lang="pt"), 7.8)
        self.assertEqual(extractnumber("sete e zero oito",
                                       lang="pt"), 7.08)
        self.assertEqual(extractnumber("sete e zero zero oito",
                                       lang="pt"), 7.008)
        self.assertEqual(extractnumber("vinte treze avos", lang="pt"),
                         20.0 / 13.0)
        self.assertEqual(extractnumber("seis virgula seiscentos e sessenta",
                                       lang="pt"), 6.66)
        self.assertEqual(extractnumber("seiscentos e sessenta e seis",
                                       lang="pt"), 666)

        self.assertEqual(extractnumber("seiscentos ponto zero seis",
                                       lang="pt"), 600.06)
        self.assertEqual(extractnumber("seiscentos ponto zero zero seis",
                                       lang="pt"), 600.006)
        self.assertEqual(extractnumber("seiscentos ponto zero zero zero seis",
                                       lang="pt"), 600.0006)
Example #2
0
 def test_extractnumber(self):
     self.assertEqual(extractnumber("this is the first test"), 1)
     self.assertEqual(extractnumber("this is 2 test"), 2)
     self.assertEqual(extractnumber("this is second test"), 2)
     self.assertEqual(extractnumber("this is the third test"), 1.0 / 3.0)
     self.assertEqual(extractnumber("this is test number 4"), 4)
     self.assertEqual(extractnumber("one third of a cup"), 1.0 / 3.0)
     self.assertEqual(extractnumber("three cups"), 3)
     self.assertEqual(extractnumber("1/3 cups"), 1.0 / 3.0)
     self.assertEqual(extractnumber("quarter cup"), 0.25)
     self.assertEqual(extractnumber("1/4 cup"), 0.25)
     self.assertEqual(extractnumber("one fourth cup"), 0.25)
     self.assertEqual(extractnumber("2/3 cups"), 2.0 / 3.0)
     self.assertEqual(extractnumber("3/4 cups"), 3.0 / 4.0)
     self.assertEqual(extractnumber("1 and 3/4 cups"), 1.75)
     self.assertEqual(extractnumber("1 cup and a half"), 1.5)
     self.assertEqual(extractnumber("one cup and a half"), 1.5)
     self.assertEqual(extractnumber("one and a half cups"), 1.5)
     self.assertEqual(extractnumber("one and one half cups"), 1.5)
     self.assertEqual(extractnumber("three quarter cups"), 3.0 / 4.0)
     self.assertEqual(extractnumber("three quarters cups"), 3.0 / 4.0)
Example #3
0
    def test_extractnumber_pt(self):
        self.assertEqual(extractnumber("isto e o primeiro teste", lang="pt"),
                         1)
        self.assertEqual(extractnumber("isto e o 2 teste", lang="pt"), 2)
        self.assertEqual(extractnumber("isto e o segundo teste", lang="pt"), 2)
        self.assertEqual(
            extractnumber(u"isto e um terço de teste", lang="pt"), 1.0 / 3.0)
        self.assertEqual(
            extractnumber("isto e o teste numero quatro", lang="pt"), 4)
        self.assertEqual(extractnumber(u"um terço de chavena", lang="pt"),
                         1.0 / 3.0)
        self.assertEqual(extractnumber("3 canecos", lang="pt"), 3)
        self.assertEqual(extractnumber("1/3 canecos", lang="pt"), 1.0 / 3.0)
        self.assertEqual(extractnumber("quarto de hora", lang="pt"), 0.25)
        self.assertEqual(extractnumber("1/4 hora", lang="pt"), 0.25)
        self.assertEqual(extractnumber("um quarto hora", lang="pt"), 0.25)
        self.assertEqual(extractnumber("2/3 pinga", lang="pt"), 2.0 / 3.0)
        self.assertEqual(extractnumber("3/4 pinga", lang="pt"), 3.0 / 4.0)
        self.assertEqual(extractnumber("1 e 3/4 cafe", lang="pt"), 1.75)
        self.assertEqual(extractnumber("1 cafe e meio", lang="pt"), 1.5)
        self.assertEqual(extractnumber("um cafe e um meio", lang="pt"), 1.5)
        self.assertEqual(extractnumber("tres quartos de chocolate", lang="pt"),
                         3.0 / 4.0)
        self.assertEqual(
            extractnumber(u"tr�s quarto de chocolate", lang="pt"),
            3.0 / 4.0)
        self.assertEqual(extractnumber("sete ponto cinco", lang="pt"), 7.5)
        self.assertEqual(extractnumber("sete ponto 5", lang="pt"), 7.5)
        self.assertEqual(extractnumber("sete e meio", lang="pt"), 7.5)
        self.assertEqual(extractnumber("sete e oitenta", lang="pt"), 7.80)
        self.assertEqual(extractnumber("sete e oito", lang="pt"), 7.8)
        self.assertEqual(extractnumber("sete e zero oito", lang="pt"), 7.08)
        self.assertEqual(extractnumber("sete e zero zero oito", lang="pt"),
                         7.008)
        self.assertEqual(extractnumber("vinte treze avos", lang="pt"),
                         20.0 / 13.0)
        self.assertEqual(
            extractnumber("seis virgula seiscentos e sessenta", lang="pt"),
            6.66)
        self.assertEqual(
            extractnumber("seiscentos e sessenta e seis", lang="pt"), 666)

        self.assertEqual(
            extractnumber("seiscentos ponto zero seis", lang="pt"), 600.06)
        self.assertEqual(
            extractnumber("seiscentos ponto zero zero seis", lang="pt"),
            600.006)
        self.assertEqual(
            extractnumber("seiscentos ponto zero zero zero seis", lang="pt"),
            600.0006)
Example #4
0
 def test_extractnumber(self):
     self.assertEqual(extractnumber("this is the first test"), 1)
     self.assertEqual(extractnumber("this is 2 test"), 2)
     self.assertEqual(extractnumber("this is second test"), 2)
     self.assertEqual(extractnumber("this is the third test"), 1.0 / 3.0)
     self.assertEqual(extractnumber("this is test number 4"), 4)
     self.assertEqual(extractnumber("one third of a cup"), 1.0 / 3.0)
     self.assertEqual(extractnumber("three cups"), 3)
     self.assertEqual(extractnumber("1/3 cups"), 1.0 / 3.0)
     self.assertEqual(extractnumber("quarter cup"), 0.25)
     self.assertEqual(extractnumber("1/4 cup"), 0.25)
     self.assertEqual(extractnumber("one fourth cup"), 0.25)
     self.assertEqual(extractnumber("2/3 cups"), 2.0 / 3.0)
     self.assertEqual(extractnumber("3/4 cups"), 3.0 / 4.0)
     self.assertEqual(extractnumber("1 and 3/4 cups"), 1.75)
     self.assertEqual(extractnumber("1 cup and a half"), 1.5)
     self.assertEqual(extractnumber("one cup and a half"), 1.5)
     self.assertEqual(extractnumber("one and a half cups"), 1.5)
     self.assertEqual(extractnumber("one and one half cups"), 1.5)
     self.assertEqual(extractnumber("three quarter cups"), 3.0 / 4.0)
     self.assertEqual(extractnumber("three quarters cups"), 3.0 / 4.0)
Example #5
0
 def test_extractnumber(self):
     self.assertEqual(extractnumber("this is the first test"), 1)
     self.assertEqual(extractnumber("this is 2 test"), 2)
     self.assertEqual(extractnumber("this is second test"), 2)
     self.assertEqual(extractnumber("this is the third test"), 1.0 / 3.0)
     self.assertEqual(extractnumber("this is test number 4"), 4)
     self.assertEqual(extractnumber("one third of a cup"), 1.0 / 3.0)
     self.assertEqual(extractnumber("three cups"), 3)
     self.assertEqual(extractnumber("1/3 cups"), 1.0 / 3.0)
     self.assertEqual(extractnumber("quarter cup"), 0.25)
     self.assertEqual(extractnumber("1/4 cup"), 0.25)
     self.assertEqual(extractnumber("one fourth cup"), 0.25)
     self.assertEqual(extractnumber("2/3 cups"), 2.0 / 3.0)
     self.assertEqual(extractnumber("3/4 cups"), 3.0 / 4.0)
     self.assertEqual(extractnumber("1 and 3/4 cups"), 1.75)
     self.assertEqual(extractnumber("1 cup and a half"), 1.5)
     self.assertEqual(extractnumber("one cup and a half"), 1.5)
     self.assertEqual(extractnumber("one and a half cups"), 1.5)
     self.assertEqual(extractnumber("one and one half cups"), 1.5)
     self.assertEqual(extractnumber("three quarter cups"), 3.0 / 4.0)
     self.assertEqual(extractnumber("three quarters cups"), 3.0 / 4.0)
     self.assertEqual(extractnumber("twenty two"), 22)
     self.assertEqual(extractnumber("two hundred"), 200)
     self.assertEqual(extractnumber("nine thousand"), 9000)
     self.assertEqual(extractnumber("six hundred sixty six"), 666)
     self.assertEqual(extractnumber("two million"), 2000000)
     self.assertEqual(
         extractnumber("two million five hundred thousand "
                       "tons of spinning metal"), 2500000)
     self.assertEqual(extractnumber("six trillion"), 600000000000.0)
     self.assertEqual(extractnumber("six trillion", short_scale=False),
                      6e+19)
     self.assertEqual(extractnumber("one point five"), 1.5)
     self.assertEqual(extractnumber("three dot fourteen"), 3.14)
     self.assertEqual(extractnumber("zero point two"), 0.2)
     self.assertEqual(extractnumber("billions of years older"),
                      10000000000.0)
     self.assertEqual(
         extractnumber("billions of years older", short_scale=False),
         10000000000000.0)
     self.assertEqual(extractnumber("one hundred thousand"), 100000)
     self.assertEqual(extractnumber("minus 2"), -2)
     self.assertEqual(extractnumber("negative seventy"), -70)
     self.assertEqual(extractnumber("thousand million"), 1000000000)
Example #6
0
 def test_extractnumber_sv(self):
     self.assertEqual(
         extractnumber("1 och en halv deciliter", lang='sv-se'), 1.5)
     self.assertEqual(
         extractnumber("det här är det första testet", lang='sv-se'), 1)
     self.assertEqual(
         extractnumber("det här är test nummer 2", lang='sv-se'), 2)
     self.assertEqual(
         extractnumber("det här är det andra testet", lang='sv-se'), 2)
     self.assertEqual(
         extractnumber("det här är tredje testet", lang='sv-se'), 3)
     self.assertEqual(
         extractnumber("det här är test nummer 4", lang='sv-se'), 4)
     self.assertEqual(extractnumber("en tredjedels dl", lang='sv-se'),
                      1.0 / 3.0)
     self.assertEqual(extractnumber("tre deciliter", lang='sv-se'), 3)
     self.assertEqual(extractnumber("1/3 deciliter", lang='sv-se'),
                      1.0 / 3.0)
     self.assertEqual(extractnumber("en kvarts dl", lang='sv-se'), 0.25)
     self.assertEqual(extractnumber("1/4 dl", lang='sv-se'), 0.25)
     self.assertEqual(extractnumber("en kvarts dl", lang='sv-se'), 0.25)
     self.assertEqual(extractnumber("2/3 dl", lang='sv-se'), 2.0 / 3.0)
     self.assertEqual(extractnumber("3/4 dl", lang='sv-se'), 3.0 / 4.0)
     self.assertEqual(extractnumber("1 och 3/4 dl", lang='sv-se'), 1.75)
     self.assertEqual(extractnumber("tre fjärdedels dl", lang='sv-se'),
                      3.0 / 4.0)
     self.assertEqual(extractnumber("trekvarts kopp", lang='sv-se'),
                      3.0 / 4.0)
Example #7
0
 def test_extractnumber_fr(self):
     self.assertEqual(extractnumber("voici le premier test", lang="fr-fr"),
                      1)
     self.assertEqual(extractnumber("c'est 2 tests", lang="fr-fr"), 2)
     self.assertEqual(extractnumber("voici le second test", lang="fr-fr"),
                      2)
     self.assertEqual(extractnumber("voici trois tests", lang="fr-fr"), 3)
     self.assertEqual(extractnumber("voici le test numéro 4", lang="fr-fr"),
                      4)
     self.assertEqual(extractnumber("un tiers de litre", lang="fr-fr"),
                      1.0 / 3.0)
     self.assertEqual(extractnumber("3 cuillères", lang="fr-fr"), 3)
     self.assertEqual(extractnumber("1/3 de litre", lang="fr-fr"),
                      1.0 / 3.0)
     self.assertEqual(extractnumber("un quart de bol", lang="fr-fr"), 0.25)
     self.assertEqual(extractnumber("1/4 de verre", lang="fr-fr"), 0.25)
     self.assertEqual(extractnumber("2/3 de bol", lang="fr-fr"), 2.0 / 3.0)
     self.assertEqual(extractnumber("3/4 de bol", lang="fr-fr"), 3.0 / 4.0)
     self.assertEqual(extractnumber("1 et 3/4 de bol", lang="fr-fr"), 1.75)
     self.assertEqual(extractnumber("1 bol et demi", lang="fr-fr"), 1.5)
     self.assertEqual(extractnumber("un bol et demi", lang="fr-fr"), 1.5)
     self.assertEqual(extractnumber("un et demi bols", lang="fr-fr"), 1.5)
     self.assertEqual(extractnumber("un bol et un demi", lang="fr-fr"), 1.5)
     self.assertEqual(extractnumber("trois quarts de bol", lang="fr-fr"),
                      3.0 / 4.0)
     self.assertEqual(extractnumber("32.2 degrés", lang="fr-fr"), 32.2)
     self.assertEqual(extractnumber("2 virgule 2 cm", lang="fr-fr"), 2.2)
     self.assertEqual(extractnumber("2 virgule 0 2 cm", lang="fr-fr"), 2.02)
     self.assertEqual(extractnumber("ça fait virgule 2 cm", lang="fr-fr"),
                      0.2)
     self.assertEqual(extractnumber("point du tout", lang="fr-fr"),
                      "point tout")
     self.assertEqual(extractnumber("32.00 secondes", lang="fr-fr"), 32)
     self.assertEqual(
         extractnumber("mange trente-et-une bougies", lang="fr-fr"), 31)
     self.assertEqual(extractnumber("un trentième", lang="fr-fr"),
                      1.0 / 30.0)
     self.assertEqual(extractnumber("un centième", lang="fr-fr"), 0.01)
     self.assertEqual(extractnumber("un millième", lang="fr-fr"), 0.001)
     self.assertEqual(extractnumber("un 20e", lang="fr-fr"), 1.0 / 20.0)
 def test_extractnumber(self):
     self.assertEqual(
         extractnumber("dies ist der erste Test", lang="de-de"), 1)
     self.assertEqual(extractnumber("dies ist 2 Test", lang="de-de"), 2)
     self.assertEqual(extractnumber("dies ist zweiter Test", lang="de-de"),
                      2)
     self.assertEqual(
         extractnumber("dies ist der dritte Test", lang="de-de"), 3)
     self.assertEqual(
         extractnumber("dies ist der Test Nummer 4", lang="de-de"), 4)
     self.assertEqual(
         extractnumber("ein drittel einer Tasse", lang="de-de"), 1.0 / 3.0)
     self.assertEqual(extractnumber("drei Tassen", lang="de-de"), 3)
     self.assertEqual(extractnumber("1/3 Tasse", lang="de-de"), 1.0 / 3.0)
     self.assertEqual(extractnumber("eine viertel Tasse", lang="de-de"),
                      0.25)
     self.assertEqual(extractnumber("1/4 Tasse", lang="de-de"), 0.25)
     self.assertEqual(extractnumber("viertel Tasse", lang="de-de"), 0.25)
     self.assertEqual(extractnumber("2/3 Tasse", lang="de-de"), 2.0 / 3.0)
     self.assertEqual(extractnumber("3/4 Tasse", lang="de-de"), 3.0 / 4.0)
     self.assertEqual(extractnumber("1 und 3/4 Tassen", lang="de-de"), 1.75)
     self.assertEqual(extractnumber("1 Tasse und eine halbe", lang="de-de"),
                      1.5)
     self.assertEqual(
         extractnumber("eine Tasse und eine halbe", lang="de-de"), 1.5)
     self.assertEqual(
         extractnumber("eine und eine halbe Tasse", lang="de-de"), 1.5)
     self.assertEqual(
         extractnumber("ein und ein halb Tassen", lang="de-de"), 1.5)
     self.assertEqual(extractnumber("drei Viertel Tasse", lang="de-de"),
                      3.0 / 4.0)
     self.assertEqual(extractnumber("drei Viertel Tassen", lang="de-de"),
                      3.0 / 4.0)
Example #9
0
    def test_extractnumber(self):
        self.assertEqual(
            extractnumber("this is the first test", ordinals=True), 1)
        self.assertEqual(extractnumber("this is 2 test"), 2)
        self.assertEqual(extractnumber("this is second test", ordinals=True),
                         2)
        self.assertEqual(extractnumber("this is the third test"), 1.0 / 3.0)
        self.assertEqual(
            extractnumber("this is the third test", ordinals=True), 3.0)
        self.assertEqual(extractnumber("this is test number 4"), 4)
        self.assertEqual(extractnumber("one third of a cup"), 1.0 / 3.0)
        self.assertEqual(extractnumber("three cups"), 3)
        self.assertEqual(extractnumber("1/3 cups"), 1.0 / 3.0)
        self.assertEqual(extractnumber("quarter cup"), 0.25)
        self.assertEqual(extractnumber("1/4 cup"), 0.25)
        self.assertEqual(extractnumber("one fourth cup"), 0.25)
        self.assertEqual(extractnumber("2/3 cups"), 2.0 / 3.0)
        self.assertEqual(extractnumber("3/4 cups"), 3.0 / 4.0)
        self.assertEqual(extractnumber("1 and 3/4 cups"), 1.75)
        self.assertEqual(extractnumber("1 cup and a half"), 1.5)
        self.assertEqual(extractnumber("one cup and a half"), 1.5)
        self.assertEqual(extractnumber("one and a half cups"), 1.5)
        self.assertEqual(extractnumber("one and one half cups"), 1.5)
        self.assertEqual(extractnumber("three quarter cups"), 3.0 / 4.0)
        self.assertEqual(extractnumber("three quarters cups"), 3.0 / 4.0)
        self.assertEqual(extractnumber("twenty two"), 22)
        self.assertEqual(extractnumber("two hundred"), 200)
        self.assertEqual(extractnumber("nine thousand"), 9000)
        self.assertEqual(extractnumber("six hundred sixty six"), 666)
        self.assertEqual(extractnumber("two million"), 2000000)
        self.assertEqual(
            extractnumber("two million five hundred thousand "
                          "tons of spinning metal"), 2500000)
        self.assertEqual(extractnumber("six trillion"), 60000000000.0)
        self.assertEqual(extractnumber("six trillion", short_scale=False),
                         6e+18)
        self.assertEqual(extractnumber("one point five"), 1.5)
        self.assertEqual(extractnumber("three dot fourteen"), 3.14)
        self.assertEqual(extractnumber("zero point two"), 0.2)
        self.assertEqual(extractnumber("billions of years older"),
                         1000000000.0)
        self.assertEqual(
            extractnumber("billions of years older", short_scale=False),
            1000000000000.0)
        self.assertEqual(extractnumber("one hundred thousand"), 100000)
        self.assertEqual(extractnumber("minus 2"), -2)
        self.assertEqual(extractnumber("negative seventy"), -70)
        self.assertEqual(extractnumber("thousand million"), 1000000000)
        self.assertEqual(extractnumber("sixth third"), 1 / 6 / 3)
        self.assertEqual(extractnumber("sixth third", ordinals=True), 3)
        self.assertEqual(extractnumber("thirty second"), 30)
        self.assertEqual(extractnumber("thirty second", ordinals=True), 32)
        self.assertEqual(
            extractnumber("this is the billionth test", ordinals=True), 1e09)
        self.assertEqual(extractnumber("this is the billionth test"), 1e-9)
        self.assertEqual(
            extractnumber("this is the billionth test",
                          ordinals=True,
                          short_scale=False), 1e12)
        self.assertEqual(
            extractnumber("this is the billionth test", short_scale=False),
            1e-12)
        # TODO handle this case
        # self.assertEqual(
        #    extractnumber("6 dot six six six"),
        #    6.666)
        self.assertTrue(extractnumber("The tennis player is fast") is False)
        self.assertTrue(extractnumber("fraggle") is False)

        self.assertTrue(extractnumber("fraggle zero") is not False)
        self.assertEqual(extractnumber("fraggle zero"), 0)

        self.assertTrue(extractnumber("grobo 0") is not False)
        self.assertEqual(extractnumber("grobo 0"), 0)
Example #10
0
 def test_extractnumber_it(self):
     self.assertEqual(extractnumber(u"questo è il primo test",
                                    lang="it"), 1)
     self.assertEqual(extractnumber(u"questo è il 2 test",
                                    lang="it"), 2)
     self.assertEqual(extractnumber(u"questo è il secondo test",
                                    lang="it"), 2)
     self.assertEqual(extractnumber(u"questo è un terzo di test",
                                    lang="it"), 1.0 / 3.0)
     self.assertEqual(extractnumber(u"questo è test numero 4",
                                    lang="it"), 4)
     self.assertEqual(extractnumber("un terzo di tazza",
                                    lang="it"), 1.0 / 3.0)
     self.assertEqual(extractnumber("tre tazze",
                                    lang="it"), 3)
     self.assertEqual(extractnumber("1/3 tazze",
                                    lang="it"), 1.0 / 3.0)
     self.assertEqual(extractnumber("un quarto di tazza",
                                    lang="it"), 0.25)
     self.assertEqual(extractnumber("1/4 tazza",
                                    lang="it"), 0.25)
     self.assertEqual(extractnumber("2/3 tazza",
                                    lang="it"), 2.0 / 3.0)
     self.assertEqual(extractnumber("3/4 tazza",
                                    lang="it"), 3.0 / 4.0)
     self.assertEqual(extractnumber("1 e 1/4 tazza",
                                    lang="it"), 1.25)
     self.assertEqual(extractnumber("1 tazza e mezzo",
                                    lang="it"), 1.5)
     self.assertEqual(extractnumber("una tazza e mezzo",
                                    lang="it"), 1.5)
     self.assertEqual(extractnumber("una e mezza tazza",
                                    lang="it"), 1.5)
     self.assertEqual(extractnumber("una e una mezza tazza",
                                    lang="it"), 1.5)
     self.assertEqual(extractnumber("tre quarti tazza",
                                    lang="it"), 3.0 / 4.0)
     self.assertEqual(extractnumber("tre quarto tazza",
                                    lang="it"), 3.0 / 4.0)
     self.assertEqual(extractnumber("sette punto cinque",
                                    lang="it"), 7.5)
     self.assertEqual(extractnumber("sette punto 5",
                                    lang="it"), 7.5)
     self.assertEqual(extractnumber("sette e mezzo",
                                    lang="it"), 7.5)
     self.assertEqual(extractnumber("sette e ottanta",
                                    lang="it"), 7.80)
     self.assertEqual(extractnumber("sette e otto",
                                    lang="it"), 7.8)
     self.assertEqual(extractnumber("sette e zero otto",
                                    lang="it"), 7.08)
     self.assertEqual(extractnumber("sette e zero zero otto",
                                    lang="it"), 7.008)
     self.assertEqual(extractnumber("venti tredicesimi",
                                    lang="it"), 20.0 / 13.0)
     self.assertEqual(extractnumber("sei virgola sessanta sei",
                                    lang="it"), 6.66)
     self.assertEqual(extractnumber("sei virgola sessantasei",
                                    lang="it"), 6.66)
     self.assertEqual(extractnumber("seicento sessanta  sei",
                                    lang="it"), 666)
     self.assertEqual(extractnumber("seicento punto zero sei",
                                    lang="it"), 600.06)
     self.assertEqual(extractnumber("seicento punto zero zero sei",
                                    lang="it"), 600.006)
     self.assertEqual(extractnumber("seicento punto zero zero zero sei",
                                    lang="it"), 600.0006)
     self.assertEqual(extractnumber("tre decimi ",
                                    lang="it"), 0.30000000000000004)
     self.assertEqual(extractnumber("dodici centesimi",
                                    lang="it"), 0.12)
     self.assertEqual(extractnumber("cinque e quaranta due millesimi",
                                    lang="it"), 5.042)
     self.assertEqual(extractnumber("mille e uno",
                                    lang="it"), 1001)
     self.assertEqual(extractnumber("due mila venti due dollari ",
                                    lang="it"), 2022)
     self.assertEqual(extractnumber(
         "cento quattordici mila quattrocento undici dollari ",
         lang="it"), 114411)
     self.assertEqual(extractnumber("ventitre dollari ", lang="it"), 23)
     self.assertEqual(extractnumber("quarantacinque minuti ",
                                    lang="it"), 45)
     self.assertEqual(extractnumber("ventuno anni ",
                                    lang="it"), 21)
     self.assertEqual(extractnumber("ventotto euro ",
                                    lang="it"), 28)
     self.assertEqual(extractnumber("dodici e quarantacinque ",
                                    lang="it"), 12.45)
     self.assertEqual(extractnumber("quarantotto euro ",
                                    lang="it"), 48)
     self.assertEqual(extractnumber("novantanove euro ",
                                    lang="it"), 99)
     self.assertEqual(extractnumber("avvisa se qualcuno arriva ",
                                    lang="it"), False)
Example #11
0
def extract_expression_en(string):
    expressions = {
        "+": ["add", "adding", "plus", "added"],
        "-": ["subtract", "subtracting", "minus", "negative", "subtracted"],
        "/": ["divide", "dividing", "divided"],
        "*": ["multiply", "multiplying", "times", "multiplied"],
        "%": ["modulus"],
        "!": ["factorial"],
        "is": ["set"],  # TODO find better keyword for x = value
        # "=": ["equals"],
        "^": ["**", "^", "pow"
              "elevated", "power", "powered", "raised"],
        "sqr": ["squared"],
        "sqrt": ["square_root"],
        "qb": ["cubed", "qubed"],
        "exp": ["exponent", "exponentiate", "exponentiated"],
        "(": ["open"],
        ")": ["close"]
    }
    # clean string
    noise_words = [
        "by", "and", "the", "in", "at", "a", "for", "an", "to", "with", "off",
        "of"
    ]

    # replace natural language expression
    for op in expressions:
        string = string.replace(op, " " + op + " ")
    words = string.replace(",", "").replace("square root", "sqrt").split(" ")

    for idx, word in enumerate(words):
        if word in noise_words:
            words[idx] = ""
        else:
            for operation in expressions:
                if word in expressions[operation]:
                    words[idx] = operation
    words = [word for word in words if word]
    exps = []
    # convert all numbers
    for idx, word in enumerate(words):
        if not word:
            continue
        if extractnumber(word):
            words[idx] = str(extractnumber(word))
        # join unknown vars nums
        if idx + 1 < len(words) and words[idx + 1] not in operations:
            # 3 x = 3x
            if is_numeric(word) and not is_numeric(words[idx + 1]):
                words[idx] = word + words[idx + 1]
                words[idx + 1] = ""
        if idx - 1 >= 0 and word not in operations:
            # 1 2 x = 1 2x
            if not is_numeric(word) and is_numeric(words[idx - 1]):
                words[idx] = words[idx - 1] + word
                words[idx - 1] = ""

    words = [word for word in words if word]

    # extract operations
    for idx, word in enumerate(words):
        if not word:
            continue
        # is an operation
        if word in expressions:
            operation = word
            if idx > 0:
                woi = words[idx - 1:idx + 2]
                words[idx - 1] = ""
                if idx + 1 < len(words):
                    words[idx + 1] = ""
                words[idx] = ""
                x = woi[0]
                try:
                    y = woi[2]
                except:
                    y = "next"
                if x == "":
                    x = "prev"
                if operation == "sqrt":
                    x = y
                exps.append([x, operation, y])
            else:
                # operation at first, is a sign
                y = words[idx + 1]
                words[idx + 1] = ""
                words[idx] = ""
                if operation == "-":
                    x = "-" + y
                    y = 0
                    operation = "+"
                    exps.append([x, operation, y])
                elif operation == "+":
                    exps.append(["0", operation, y])
                # or square root
                elif operation == "sqrt":
                    x = y
                    y = "next"
                    exps.append([x, operation, y])
                    # TODO exponent, log

    if not exps and extractnumber(string):
        exps = [["0", "+", str(extractnumber(string))]]
    return exps