Esempio n. 1
0
 def test_dyn_getitem(self):
     php = PHP()
     array_params = range(self.TEST_RANGE)
     array = php.array(*array_params)
     php.shuffle(array)
     php.echo(array[array[0]])
     self.assertIn(int(str(php)), array_params)
Esempio n. 2
0
 def test_dict(self):
     php = PHP()
     values = php.array_values(self.TEST_DICT)
     php.sort(values)
     php.echo(php.implode(',', values))
     expected = ','.join(sorted(self.TEST_DICT.itervalues()))
     self.assertEquals(str(php), expected)
Esempio n. 3
0
 def test_new(self):
     php = PHP()
     dt = php.new('DateTime')
     ts = dt.getTimestamp()
     php.echo(ts)
     output = str(php)
     self.assertAlmostEquals(int(output), time(), delta=10)
Esempio n. 4
0
def fuzzymatch(string1):
    #note:  fuzzymatch.php must be in php path, e.g.  /usr/lib/php/!!!
    #put in a cron job that runs every half hour for new entries?

    entities = Session.query(Entity)

    matches = []

    ##string1 = string1.decode('utf8')

    for entity in entities:
        php = PHP("require 'fuzzymatch.php';")
        #php = PHP()
        #print "testing " + entity.label.encode('utf8') + " against " + string1.encode('utf8') + "\n"

        code = '$string1 = utf8_decode("' + string1.encode('utf8') + '");'

        #code = code + "$string2 = '" + entity.label.encode('latin-1', 'replace') + "';"
        #code = code + "print $string1; print $string2;"
        #print code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");'
        code = code + '$string2 = utf8_decode("' + entity.label.encode(
            'utf8') + '");'
        code = code + """print fuzzy_match($string1, $string2, 2);"""

        verdict = php.get_raw(code)
        #print "verdict is " + verdict + "\n"

        if float(verdict) >= .5:
            #print entity.label + " is a match!\n"
            entity.matchvalue = verdict
            matches.append(entity)

    return matches
Esempio n. 5
0
def fuzzymatchtest(string1, string2):
    #note:  fuzzymatch.php must be in php path, e.g.  /usr/lib/php/!!!
    php = PHP("require 'fuzzymatch.php';")
    #php = PHP()

    code = "$string1 = '" + string1 + "';"
    code = code + "$string2 = '" + string2.encode('latin-1', 'replace') + "';"
    #code = code + "print $string1; print $string2;"
    code = code + """print fuzzy_match($string1, $string2, 2);"""

    return php.get_raw(code)
Esempio n. 6
0
 def test_add(self):
     php = PHP()
     array = php.range(1, self.TEST_RANGE)
     php.echo(php.count(array) + self.TEST_VALUE)
     self.assertEquals(str(php), str(self.TEST_VALUE + self.TEST_RANGE))
Esempio n. 7
0
 def test_getitem(self):
     php = PHP()
     array_params = range(self.TEST_RANGE)
     array = php.array(*array_params)
     php.echo(array[0])
     self.assertEquals(str(php), str(array_params[0]))
Esempio n. 8
0
 def test_list(self):
     php = PHP()
     php.echo(php.count(range(self.TEST_RANGE)))
     output = str(php)
     self.assertEquals(output, str(self.TEST_RANGE))
Esempio n. 9
0
 def test_repr(self):
     php = PHP()
     self.assertEquals(repr(php), '<PHP object with 0 statement(s)>')
Esempio n. 10
0
 def test_deep_compose(self):
     php = PHP()
     php.echo(php.strlen(php.strrev(self.TEST_STRING)))
     output = str(php)
     self.assertEquals(output, str(len(self.TEST_STRING)))
Esempio n. 11
0
 def test_number(self):
     php = PHP()
     php.var_dump(5)
     self.assertEquals(str(php), 'int(5)\n')
Esempio n. 12
0
 def test_null(self):
     php = PHP()
     php.var_dump(None)
     self.assertEquals(str(php), 'NULL\n')
Esempio n. 13
0
 def test_error(self):
     php = PHP()
     php.nonexistant_method()
     with self.assertRaises(PhpError):
         str(php)
Esempio n. 14
0
 def test_echo(self):
     php = PHP()
     php.echo(self.TEST_STRING)
     self.assertEquals(str(php), self.TEST_STRING)
Esempio n. 15
0
def fuzzymatchall(SEPEntrieslist):
    #takes outputs from addlist() and saves all fuzzy match IDs to SEPEntry.fuzzymatch with verdicts (percent of words matched)
    #now change so that it only updates ones that don't currently have a fuzzymatchlist

    #clear out fuzzymatch table--otherwise old fuzzies will accumulate, and nobody wants that
    delquery = Session.query(Fuzzymatch)
    delquery.delete()
    Session.flush()
    Session.commit()

    for SEPEntry in SEPEntrieslist:
        print "working on " + SEPEntry.title.encode('utf-8') + "\n"
        entities = Session.query(Entity)

        #exclude journals and nodes from fuzzy matching
        entities = entities.filter(Entity.typeID != 2)
        entities = entities.filter(Entity.typeID != 4)

        #reset fuzzymatches for that entry
        #SEPEntry.fuzzymatches = ""

        ##string1 = string1.decode('utf8')

        for entity in entities:
            php = PHP("set_include_path('/usr/lib/php/');")
            php = PHP("require 'fuzzymatch.php';")
            #php = PHP()
            #print "testing " + entity.label.encode('utf8') + " against " + string1.encode('utf8') + "\n"

            code = '$string1 = utf8_decode("' + SEPEntry.title.encode(
                'utf8') + '");'

            #code = code + "$string2 = '" + entity.label.encode('latin-1', 'replace') + "';"
            #code = code + "print $string1; print $string2;"
            #print code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");'
            code = code + '$string2 = utf8_decode("' + entity.label.encode(
                'utf8') + '");'
            code = code + """print fuzzy_match($string1, $string2, 2);"""

            verdict = php.get_raw(code)
            #print "verdict is " + verdict + "\n"
            verdict = verdict.split(',')

            if float(verdict[0]) >= .20:
                #print entity.label + " is a match!\n"
                #entity.matchvalue = verdict
                #string = SEPEntry.fuzzymatches + "|" + str(entity.ID) + "," + verdict

                #if len(string) < 400:
                #    SEPEntry.fuzzymatches = SEPEntry.fuzzymatches + "|" + str(entity.ID) + "," + verdict
                #else:
                #    print "sorry, too many matches!  Can't add " + str(entity.ID) + " to fuzzy matches; over 400 chars."
                fmatch = Fuzzymatch(entity.ID)
                fmatch.sep_dir = SEPEntry.sep_dir
                fmatch.strength = verdict[0]
                fmatch.edits = verdict[1]

                SEPEntry.fmatches.append(fmatch)

        Session.flush()
        Session.commit()