Exemplo n.º 1
0
def getSpeech():
    try:
        if request.method == 'POST':
            data = request.json
            text = data["raw_text"]
            current_indent = firebase_api.get_indent(data["uid"],
                                                     data["file_id"],
                                                     data["line_no"])
            revcode, new_indent, just_cut, before_detext = grammar.grammar(
                text, current_indent)
            if (revcode.isspace()):
                revcode = ""
            firebase_api.set_indent(data["uid"], data["file_id"],
                                    data["line_no"], new_indent, revcode)
            print(
                json.dumps(
                    {
                        "status": "ok",
                        "before_text": text,
                        "cut_text": just_cut,
                        "before_detect": before_detext,
                        "code": revcode
                    },
                    sort_keys=True,
                    indent=4,
                    ensure_ascii=False,
                    separators=(',', ': ')))

            return jsonify({
                "status": "ok",
                "before_text": text,
                "cut_text": just_cut,
                "before_detect": before_detext,
                "code": revcode
            })

        else:
            text = request.args.get("text")
            revcode, new_indent, just_cut, before_detext = grammar.grammar(
                text, 0)
            return jsonify({
                "status": "ok",
                "before_text": text,
                "cut_text": just_cut,
                "before_detect": before_detext,
                "code": revcode
            })
    except Exception as e:
        print("Error:", e)
        traceback.print_exc()
        return jsonify({"status": "error"})
Exemplo n.º 2
0
def parse(vars_file, template_file):
    with open(vars_file, 'r') as f:
        vars_file = f.read()

    vars = grammar.grammar(vars_file).vars_file()
    vars = sorted(vars, key=lambda x: x['name'])

    THIS_DIR = os.getcwd()
    j2_env = Environment(loader=FileSystemLoader(THIS_DIR), trim_blocks=True)
    out = j2_env.get_template(template_file).render(variables=vars)

    with open(template_file[:-3], 'w') as f:
        f.write(out)
Exemplo n.º 3
0
def test():
	""""""

	g = grammar(G)
	for key, value in g.items():
		print '{}: {}'.format(key, value)
Exemplo n.º 4
0
    "declaration -> var-declaration | fun-declaration",
    "var-declaration -> type-specifier ID ; | type-specifier ID [ NUM ] ;",
    "type-specifier -> int | void",
    "fun-declaration -> type-specifier ID ( params ) compound-stmt",
    "params -> param-list | void",
    "param-list -> param-list , param | param",
    "param -> type-specifier ID | type-specifier ID [ ]",
    "compound-stmt -> { local-declarations statement-list }",
    "local-declarations -> local-declarations var-declaration | #",
    "statement-list -> statement-list statement | #",
    "statement -> expression-stmt | compound-stmt | selection-stmt | iteration-stmt | return-stmt",
    "expression-stmt -> expression ; | ;",
    "selection-stmt -> if ( expression ) statement | if ( expression ) statement else statement",
    "iteration-stmt -> while ( expression ) statement",
    "return-stmt -> return ; | return expression ;",
    "expression -> var = expression | simple-expression",
    "var -> ID | ID [ expression ]",
    "simple-expression -> additive-expression RELOP additive-expression | additive-expression",
    "additive-expression -> additive-expression ADDOP term | term",
    "term -> term MULOP factor | factor",
    "factor -> ( expression ) | var | call | NUM",
    "call -> ID ( args )",
    "args -> arg-list | #",
    "arg-list -> arg-list , expression | expression"
]

string = "\n".join(re.sub(r"//.*$", "", x) for x in re.sub(r"/\*.*?\*/", "", open(sys.argv[1], "r").read(), flags=re.MULTILINE | re.DOTALL).split("\n"))
gram = grammar(cfg)
x = gram.parse(gram.lex(string, patterns))
print("ACCEPT" if x else "REJECT")
from grammar import grammar, parse, Fail

JSON = grammar(
    r"""value    => object | array | string | number | true | false | null
string   => ["][^"\\]+["]
number   => int frac exp | int exp | int frac | int
int      => [-]?[\d]+
frac     => [.][\d]+
exp      => [eE][+-]?[\d]+
object   => [{] members [}] | {}
array    => [\[] elements [\]]
elements => value [,] elements | value
members  => pair [,] members | pair
pair     => string [:] value""",
    whitespace="\s*",
)


def json_parse(text):
    return parse("value", text, JSON)


def test():
    assert json_parse('["testing", 1, 2, 3]') == (
        [
            "value",
            [
                "array",
                "[",
                [
                    "elements",
Exemplo n.º 6
0
while inp != "e":

    inp = input()

    if inp == "1":

        translate.translate(dictionaries)  # enter Dictionary-Module

    elif inp == "2":

        dictionaries = vocab.trainer(dictionaries)  # enter Vocabulary-Module

    elif inp == "3":

        grammar.grammar(dictionaries)  # enter Grammar-Module

    elif inp == "4":

        mad.madlib(dictionaries)  # enter MadLib-Module

    elif inp == "5":

        load.update(dictionaries)
        vocab.statistics(dictionaries)  #update statistics in statistics.csv

    elif inp == "i":

        print("Info"
              )  # print information about the program and underlying processes
Exemplo n.º 7
0
for line in lines:
    if line == "":
        groups.append([])
    else:
        groups[-1].append(line)


# for part 1, just return s
def rpl(s):
    if s.startswith("8:"):
        return "8: 42 | 42 8"
    if s.startswith("11:"):
        return "11: 42 31 | 42 11 31"
    return s


rules = [
    x.replace(":", " ->").replace('"', "")
    for x in map(rpl, sorted(groups[0]))
]
gram = grammar(rules)

ct = 0
for ip in groups[1]:
    tokens = [(x, x) for x in ip]
    ast = gram.parse(tokens)
    print(ip, ast)
    if ast is not None:
        ct += 1
print(ct)
Exemplo n.º 8
0
 URL = grammar("""
 url => httpaddress | ftpaddress | mailtoaddress
 httpaddress => http:// hostport /path? ?search?
 ftpaddress => ftp:// login / path ; ftptype | ftp:// login / path
 /path? => / path | ()
 ?search? => [?] search | ()
 mailtoaddress => mailto: xalphas @ hostname
 hostport => host : port | host
 host => hostname | hostnumber
 hostname => ialpha . hostname | ialpha
 hostnumber => digits . digits . digits . digits
 ftptype => A formcode | E formcode | I | L digits
 formcode => [NTC]
 port => digits | path
 path => void | segment / path | segment
 segment => xalphas
 search => xalphas + search | xalphas
 login => userpassword hostport | hostport
 userpassword => user : password @ | user @
 user => alphanum2 user | alphanum2
 password => alphanum2 password | password
 path => void | segment / path | segment
 void => ()
 digits => digit digits | digit
 digit => [0-9]
 alpha => [a-zA-Z]
 safe => [-$_@.&+]
 extra => [()!*''""]
 escape => % hex hex
 hex => [0-9a-fA-F]
 alphanum => alpha | digit
 alphanums => alphanum alphanums | alphanum
 alphanum2 => alpha | digit | [-_.+]
 ialpha => alpha xalphas | alpha
 xalphas => xalpha xalphas | xalpha
 xalpha => alpha | digit | safe | extra | escape
 """, whitespace = '()')
Exemplo n.º 9
0
from grammar import grammar, parse

# Sets regular expression grammar
REGRAMMAR = grammar("""
RE => ALT | RE2
RE2 => SEQ | EXP
EXP => MOD | VAL
MOD => STAR | PLUS | OPT
ALT => RE2 \| RE
GRP => \( RE \)
SEQ => EXP RE2
VAL => GRP | LIT | DOT | EOL
LIT => [^()|?+*.$]
DOT => \.
EOL => \$
STAR => VAL \*
PLUS => VAL \+
OPT => VAL \?
""", whitespace='')


def parse_re(pattern):
    """Parses a regular expression pattern and returns a string in a format
    readable by a custom regex compiler.

    Parameters:
    pattern : a string regex pattern

    Returns:
    string : a string containing regex pattern converted into string of
        functions readable by regex compiler
Exemplo n.º 10
0
'''
JSON parse.
'''

from grammar import grammar, parse, verify

JSON = grammar(r"""
value       => array | object | string | number | true | false | null
array       => [[] []] | [[] elements []]
elements    => value , elements | value
string      => "[^"]*"
number      => int frac exp | int frac | int exp | int
int         => -?[1-9]\d*
frac        => [.]\d+
exp         => [eE][+-]?\d+
object      => { } | { members }
members     => pair , members | pair
pair        => string : value
""",
               whitespace='\s*')


def json_parse(text):
    "JSON parse."
    return parse('value', text, JSON)


def test():
    "tests"
    assert json_parse('["testing", 1, 2, 3]') == ([
        'value',
Exemplo n.º 11
0
from grammar import grammar, parse, Fail

JSON = grammar(
    r"""value    => object | array | string | number | true | false | null
string   => ["][^"\\]+["]
number   => int frac exp | int exp | int frac | int
int      => [-]?[\d]+
frac     => [.][\d]+
exp      => [eE][+-]?[\d]+
object   => [{] members [}] | {}
array    => [\[] elements [\]]
elements => value [,] elements | value
members  => pair [,] members | pair
pair     => string [:] value""",
    whitespace='\s*')


def json_parse(text):
    return parse('value', text, JSON)


def test():
    assert json_parse('["testing", 1, 2, 3]') == ([
        'value',
        [
            'array', '[',
            [
                'elements', ['value', ['string', '"testing"']], ',',
                [
                    'elements', ['value', ['number', ['int', '1']]], ',',
                    [
Exemplo n.º 12
0
                    alaki2 = []
                    s = '\t'
                    old_c = '\t'
                    cnt = 0
                else:
                    if (s.__eq__('\t')):
                        s = ''
                    s = s + c
                    old_c = c

action.remove([])
goto.remove([])
#print(action)
#print(goto)

grammar = grammar()
#print(grammar)

inp = scanner()

#print(inp)

s_input = Stack()
s_input.push(('z', ''))
for i in range(0, inp.__len__()):
    #print(inp[inp.__len__()-1-i])
    s_input.push(inp[inp.__len__() - 1 - i])

s_parsser = Stack()
s_parsser.push((0, ''))
Exemplo n.º 13
0
def main():
    g = grammar.grammar(grammar.rules_from_file(sys.argv[1]))
    l = lexicon.lexicon(lexicon.lexemes_from_file(sys.argv[2]))
    
    for i in range(10):
        print g.gen_random_sentence(l)