Exemple #1
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    from swap import sparql2cwm, myStore, notation3
    _outSink = notation3.ToN3(sys.stdout.write, quiet=1, flags='')
    sink = sparql2cwm.FromSparql(myStore._checkStore(), ve=1)
    if len(argv) == 3:
        sink = nullProductionHandler()
        p = N3Parser(file(argv[1], 'r'), branches, sink, ve=1)
        p.parse(start)
    if len(argv) == 2:
        p = N3Parser(file(argv[1], 'r'), branches, sink, ve=1)
        print "About to parse"
        f = p.parse(start).close()
        myStore._checkStore().dumpNested(f, _outSink)
def main(argv=None):
   if argv is None: 
      argv = sys.argv
   from swap import sparql2cwm, myStore, notation3
   _outSink = notation3.ToN3(sys.stdout.write,
                                      quiet=1, flags='')
   sink = sparql2cwm.FromSparql(myStore._checkStore(), ve=1)
   if len(argv) == 3:
       sink = nullProductionHandler()
       p = N3Parser(file(argv[1], 'r'), branches, sink, ve=1)
       p.parse(start)
   if len(argv) == 2: 
      p = N3Parser(file(argv[1], 'r'), branches, sink, ve=1)
      print "About to parse"
      f = p.parse(start).close()
      myStore._checkStore().dumpNested(f, _outSink)
Exemple #3
0
def importTokens():
    global tokens
    if tokens is None:
        try:
            t0 = time.time()
            from sparql_tokens_table import tokens as ts, regexps as rs
            t1 = time.time()
            print >> sys.stderr, 'loaded from file ', t1 - t0
            tokens = ts
            for k, v in rs.iteritems():
                setattr(Tokens, k, v)
        except ImportError:
            from swap import myStore
            store = myStore._checkStore()
            F = myStore.load('http://www.w3.org/2000/10/swap/grammar/sparql')
            BNF = myStore.Namespace("http://www.w3.org/2000/10/swap/grammar/bnf#")
            regexps = {}
            k = F.statementsMatching(pred=BNF.tokens)
            if len(k) != 1:
                raise RuntimeError("Expected 1 occurrence of bnf:tokens, got %i: %s" % (len(k), `k`))
            for triple in k:
                tokens = [x.uriref() for x in triple.object()]
            tokens.append(BNF.PASSED_TOKENS.uriref())
            for triple in F.statementsMatching(pred=BNF.matches):
                s, p, o = triple.spo()
                key = s.uriref()
                val = o.value()
                if key in tokens:
                    setattr(Tokens, 't_' + key, val)
                    regexps['t_' + key] = val
                    setattr(Tokens, 'c_' + key, smartCompile(val, re.I))
                    regexps['c_' + key] = smartCompile(val, re.I)
            pklVal = {'tokens': tokens, 'regexps': regexps}
            try:
                import imp, os.path
                try:
                    path = imp.find_module('sparql')[1]
                except ImportError:
                    path = ''
#                path = ''
                f = file(os.path.join(path, 'sparql_tokens_table.py'), 'w')
                mkmodule(pklVal, f)
                f.close()
            except:
                raise
Exemple #4
0
        return null, predict
    except ImportError:
        p = get_productions(uri)
        f = find_nulls(p)

        predict = find_null_connected(p, f)

        out = file('earley_tables.py', 'w')
        make_table(out, f, predict)
        return f, predict


if __name__ == '__main__':
    f, predict = cache_get(sys.argv[1])

    ##    for m in sorted(predict.items()):
    ##        print '\t%s: %s\n' % m
    ##    sys.exit(0)
    print 'ready to lex'
    results, t1, k2 = parse(sys.argv[3], sys.argv[2], predict, f)
    t2 = time.time() - k2

    #print '\n\n\t'.join([`a` for a in results])
    import sparql2cwm, myStore, notation3
    _outSink = notation3.ToN3(sys.stdout.write, quiet=1, flags='')
    for a in results:
        sink = sparql2cwm.FromSparql(myStore._checkStore(), ve=1)
        f = AST((sys.argv[2], ) + a[3], sink).run().close()
        myStore._checkStore().dumpNested(f, _outSink)
    print t1, t2, t1 + t2
Exemple #5
0
        p = get_productions(uri)
        f = find_nulls(p)
        
        predict = find_null_connected(p, f)

        out = file('earley_tables.py', 'w')
        make_table(out, f, predict)
        return f, predict
        

if __name__ == '__main__':
    f, predict = cache_get(sys.argv[1])

##    for m in sorted(predict.items()):
##        print '\t%s: %s\n' % m
##    sys.exit(0)
    print 'ready to lex'
    results, t1, k2 = parse(sys.argv[3], sys.argv[2], predict, f)
    t2 = time.time() - k2
    
    #print '\n\n\t'.join([`a` for a in results])
    import sparql2cwm, myStore, notation3
    _outSink = notation3.ToN3(sys.stdout.write,
                                      quiet=1, flags='')
    for a in results:
        sink = sparql2cwm.FromSparql(myStore._checkStore(), ve=1)
        f = AST((sys.argv[2],) + a[3], sink).run().close()
        myStore._checkStore().dumpNested(f, _outSink)
    print t1,t2,t1+t2