Beispiel #1
0
    def work(fQ, eQ):
        xmlp = XMLParser.XMLParser()
        datp = DATParser.DATParser()

        while not fQ.empty():
            fp = fQ.get()
            if fp[-4:] == '.xml':
                parser = xmlp
            else:
                parser = datp
            logging.info('Parsing %s', os.path.basename(fp))
            print(os.path.basename(fp) + ' parsing')
            try:
                dpatns, badpatns = parser.parseFile(fp)
                print(os.path.basename(fp) + " parsed")
                # The len fun below works for both dicts (badpatns) and arrays (dpatns)
                logging.info("%d (%d bad) found in %s", len(dpatns),
                             len(badpatns), os.path.basename(fp))

                # DB: This next line, I think, is Andy loading the parsed good patents into the
                # multicore queue. I just have each thread insert the patents straight into the db.
                # dQ.put(dpatns)

                # DB: the below line inserts all of the good patents into
                # the database collection 'patns'. Assumes dpatns is of type array of dicts.
                dbase['patns'].insert(dpatns)
                print(os.path.basename(fp) + " in DB")

                # parser.patns = dict()	# toss old patns
                parser.patns = []
                # Could deal with bad patns instead of tossing them, but probably not worth it.
                # DB: put them into a mongo instance?
                parser.badpatns = {}
            except:
                logging.error("Error parsing %s",
                              os.path.basename(fp),
                              exc_info=True)
                eQ.put(fp)
            fQ.task_done()
        logging.info("Worker finished.")
Beispiel #2
0
 def work(fQ, dQ, eQ):
     xmlp = XMLParser.XMLParser()
     datp = DATParser.DATParser()
     
     while not fQ.empty():
         fp = fQ.get()
         if fp[-4:] == '.xml':
             parser = xmlp
         else:
             parser = datp
         logging.info("Parsing %s", os.path.basename(fp))
         try:
             dpatns,badpatns = parser.parseFile(fp)
             logging.info("%d (%d bad) found in %s", len(dpatns), len(badpatns), os.path.basename(fp))
             dQ.put(dpatns)
             parser.patns = dict()   # toss old patns
             # BUGBUG tossing bad patns instead of dealing with them
             parser.badpatns = dict()
         except:
             logging.error("Error parsing %s", os.path.basename(fp), exc_info=True)
             eQ.put(fp)
         fQ.task_done()
     #dictQ.close()
     logging.info("Worker finished.")
Beispiel #3
0
def main():
    model_path = get_model_path()
    config = Decoder.default_config()
    # config.set_string('-hmm', os.path.join(model_path, 'en-us'))
    # config.set_string('-lm', os.path.join(model_path, 'en-us.lm.bin'))
    # config.set_string('-dict', os.path.join(model_path, 'cmudict-en-us.dict'))
    config.set_string('-hmm', os.path.join(model_path, 'en-us'))
    config.set_string('-lm', '2823.lm')
    config.set_string('-verbose', 'False')
    config.set_string('-dict', '2823.dic')
    config.set_string('-kws', 'keyphrase.list')
    config.set_string('-logfn', '/dev/null')

    graph = XMLParser(graph_file="basic.xml", debug=True).parse()
    arduino = serial.Serial('/dev/ttyACM0', 57600)
    arduino.timeout = 0.1

    # Check currnet state
    print("Current State: {}".format(graph.get_current_state().name))

    decoder = Decoder(config)

    # while True:
    #     simpletime.sleep(0.1)
    #     try:
    #         # arduino_says = arduino.readline()
    #         # if (len(arduino_says) > 0):
    #         #     print('\nRaw: ' + arduino_says)
    #         # arduino_says = arduino_says.replace('\r', '')
    #         # arduino_says = arduino_says.replace('\n', '')
    #         # if "m:done;" in arduino_says or "e:ready;" in arduino_says:
    #         #     print('Live!')
    #         #     local.arduino_busy = False
    #         # sys.stdout.write(".")
    #         # sys.stdout.flush()
    #     except KeyboardInterrupt:
    #         break

    #src.recursive_stop()

    def on_graph_state_change():
        print("onStateChange()")
        # Runs through state responses
        print("\tNew Current State: {}".format(graph.state))
        print("\tExecuting responses for nextState...")

        if len(graph.state.get_responses()) > 0:
            print('Responses: {}'.format(len(graph.state.get_responses())))
            for response in graph.state.get_responses():
                print('\tRunning Response {}'.format(response))
                # do response action whether it has to do with moving motors, turning led, etc

                if response.typ == ResponseType.GO_TO_STATE:
                    graph.set_current_state(response.value)
                elif response.typ == ResponseType.LED:
                    if pixels is not None:
                        if response.value == 'listening':
                            pixels.think()
                        elif response.value == 'off':
                            pixels.off()
                        elif response.value == 'hello':
                            pixels.speak()
                        elif response.value == 'following':
                            pixels.spin()
                        elif response.value == 'doa':
                            if mic is not None:
                                pixels.wakeup(mic.direction)
                        else:
                            print("Unknown LED value: {} was found.".format(
                                response.value))
                elif response.typ == ResponseType.MOTOR_MOVE:
                    if response.value == 'forward':
                        arduino.write("d:f;")
                    elif response.value == 'stop':
                        arduino.write("d:s;")
                elif response.typ == ResponseType.CAMERA_MOVE:
                    if response.value == 'doa':
                        if mic is not None:
                            voice_direction = mic.direction
                            print("voice from " + str(voice_direction))
                            arduino_command = "m:" + str(voice_direction) + ";"
                            if voice_direction < 180:
                                #voice is coming from behind
                                voice_direction = (voice_direction + 180) % 360
                            else:
                                #voice is coming from in front
                                voice_direction = 90

                        arduino_command = arduino_command + "c:" + str(
                            voice_direction) + ",120;"
                        arduino.write(arduino_command)
                        last_time_motor_moved = simpletime.time()
                        print("@done@")
                elif response.typ == ResponseType.VOICE_RESPONSE:
                    text = response.value.replace(' ', '_')

                    #Calls the Espeak TTS Engine to read aloud a Text
                    call([cmd_beg + cmd_out + text + cmd_end], shell=True)
                else:
                    print("Unused response type: {}.".format(response.typ))
        else:
            print('\tResponding with nothing')

    class local:
        # arduino_busy = True
        voices = {}
        position = None

    def on_detected(word):
        start = datetime.now()
        if simpletime.time() - last_time_motor_moved > 0.4:
            print("on_detected with word = ")
            graph.apply_action(ActionType.VOICE_COMMAND, word.hypstr)
        else:
            print("on_detected ignored - motor movement")
        print(datetime.now() - start)
        # if 'odd bot' in word.hypstr and 'follow me' in word.hypstr:
        #     pixels.think()
        # else:
        #     print(word.hypstr)
        #     #     print("Arduino is busy. Doing nothing")
        #     #     return
        #     local.position = doa.get_direction()
        #     pixels.wakeup(local.position)
        #     print(datetime.now() - start)
        #     # local.arduino_busy = True
        #     print('\nDirection {}'.format(local.position) + " Sent: " + str(local.position))

        # arduino.write("m:" + str(k) + ";c:" + str(randint(30, 150)) + "," + str(randint(30,150)) + ";")

    graph.set_on_state_change(on_graph_state_change)
    p = pyaudio.PyAudio()
    stream = p.open(format=pyaudio.paInt16,
                    channels=1,
                    rate=16000,
                    input=True,
                    frames_per_buffer=2048)
    stream.start_stream()

    in_speech_bf = False
    decoder.start_utt()
    while True:
        try:
            buf = stream.read(2048, exception_on_overflow=False)
            if buf:
                decoder.process_raw(buf, False, False)
                if decoder.get_in_speech() != in_speech_bf:
                    in_speech_bf = decoder.get_in_speech()
                    if not in_speech_bf:
                        decoder.end_utt()
                        # print 'Result:', decoder.hyp().hypstr
                        on_detected(decoder.hyp())
                        decoder.start_utt()
            else:
                break
        except KeyboardInterrupt:
            break
    decoder.end_utt()
Beispiel #4
0
def main():
    src = Source(rate=16000, channels=1, frames_size=21000)
    ch1 = ChannelPicker(channels=1, pick=1)
    # doa = DOA(rate=16000, chunks=3)

    model_path = get_model_path()

    config = Decoder.default_config()
    # config.set_string('-hmm', os.path.join(model_path, 'en-us'))
    # config.set_string('-lm', os.path.join(model_path, 'en-us.lm.bin'))

    config.set_string('-hmm', os.path.join(model_path, 'en-us'))
    config.set_string('-lm', '2823.lm')
    config.set_string('-verbose', 'False')
    config.set_string('-dict', '2823.dic')
    # config.set_string('-dict', os.path.join(model_path, 'cmudict-en-us.dict'))
    config.set_string('-kws', 'keyphrase.list')
    config.set_string('-logfn', '/dev/null')
    # config.set_string('-keyphrase', 'hey there')
    # config.set_float('-kws_threshold', 1e-30)
    sphinx = Sphinx(config)

    src.link(ch1)
    # src.link(doa)
    ch1.link(sphinx)

    graph = XMLParser(graph_file="basic.xml", debug=True).parse()
    arduino = serial.Serial('/dev/ttyACM0', 57600)
    arduino.timeout = 0.1


    # Check currnet state
    print("Current State: {}".format(graph.get_current_state().name))


    def on_graph_state_change():
        print("onStateChange()")
        # Runs through state responses
        print("\tNew Current State: {}".format(graph.state))
        print("\tExecuting responses for nextState...")

        if len(graph.state.get_responses()) > 0:
            print('Responses: {}'.format(len(graph.state.get_responses())))
            for response in graph.state.get_responses():
                print('\tRunning Response {}'.format(response))
                # do response action whether it has to do with moving motors, turning led, etc

                if response.typ == ResponseType.GO_TO_STATE:
                    graph.set_current_state(response.value)
                elif response.typ == ResponseType.LED:
                    pixels != null:
                        if response.value == 'listening':
                            pixels.think()
                        elif response.value == 'off':
                            pixels.off()
                        elif response.value == 'hello':
                            pixels.speak()
                        elif response.value == 'following':
                            pixels.following()
                        elif response.value == 'doa':
                            if mic != null:
                                pixels.wakeup(mic.direction)
                        else:
                            print("Unknown LED value: {} was found.".format(response.value))
                elif response.typ == ResponseType.MOTOR_MOVE:
                    if response.value == 'forward':
                        arduino.write("d:f;")
                    elif response.value == 'stop':
                        arduino.write("d:s;")
                elif response.typ == ResponseType.CAMERA_MOVE:
                    if response.value == 'doa':
                        if mic != null:
                            voice_direction = mic.direction
                            print "voice from " + str(voice_direction)
                            arduino_command = "m:" + str(voice_direction) + ";"
                            if voice_direction < 180:
                                #voice is coming from behind
                                voice_direction = (voice_direction + 180) % 360
                            else:
                                #voice is coming from in front
                                voice_direction = 90

                        arduino_command = arduino_command + "c:" + str(voice_direction) + ",120;"
                        arduino.write(arduino_command)
                        last_time_motor_moved = simpletime.time()
                        print("@done@")
                elif response.typ == ResponseType.VOICE_RESPONSE:
                    text = response.value.replace(' ', '_')

                    #Calls the Espeak TTS Engine to read aloud a Text
                    call([cmd_beg+cmd_out+text+cmd_end], shell=True)
                else:
                    print("Unused response type: {}.".format(response.typ))
Beispiel #5
0
import IPPInterpret
import XMLParser
import sys
import argparse

argparser = argparse.ArgumentParser()
argparser.add_argument('--source', '-s', type=str, dest="source")
args = argparser.parse_args()
if args.source != None:
    file = open(args.source, "r")
else:
    file = sys.stdin

parser = XMLParser.XMLParser(file)
program = parser.parse_document()
interpret = IPPInterpret.IPPInterpret(program)
interpret.interpret_program()
Beispiel #6
0
    def start(self, inputFilePath, outputPath, fileName, config):

        currentDir = readConfig.getCurrentScriptPath()

        apkFilesPath = os.path.join(currentDir, config["apkFilesPath"])
        tempPath = os.path.join(currentDir, "tmp_" + fileName)
        androidManifest = os.path.join(tempPath, "AndroidManifest.xml")
        utilsPath = os.path.join(currentDir, config["utilsPath"])
        filesPath = os.path.join(currentDir, config["filesPath"])
        apkPath = os.path.join(apkFilesPath, fileName)

        print("\n APKPath: %s\n TmpPath: %s\n " % (apkPath, tempPath))

        if not (os.path.exists(apkFilesPath)):
            os.mkdir(apkFilesPath)

        # 判断APK是否已经保护过
        zipFile = os.path.join(utilsPath, '7za.exe')

        if not os.path.exists(zipFile):
            zipFile = '7za'

        assertLibPath = os.path.join(apkFilesPath, fileName[:-4])
        self.delete(assertLibPath)
        if self.sysstr == "Linux":
            cmd = 'unzip "%s" "lib/*" -d "%s"' % (apkPath, assertLibPath)
        else:
            cmd = ' %s x -aoa %s "assets" "lib"  -o%s ' % (zipFile, apkPath,
                                                           assertLibPath)
        subprocess.call(cmd, shell=True)
        #print ("\n command is %s\n" %cmd)
        #print ("\n Search path is: %s \n" %assertLibPath)

        for root, dirs, files in os.walk(assertLibPath):
            for temp in files:
                #print ("\n Search path is: %s \n" %temp)
                if temp.find(globalValues.IsBangBang) >= 0 or temp.find(
                        globalValues.IsAjiami) >= 0 or temp.find(
                            globalValues.IsProtect) >= 0:
                    globalValues.returnValue = 1
                    delete(assertLibPath)
                    print("This APK has been reinforced!!!")
                    sys.exit(1)

        self.delete(assertLibPath)

        # 解压APK
        packunpack.unpackApk(
            os.path.join(utilsPath, globalValues.APKToolsName), apkPath,
            tempPath)

        #读取values/String.xml和AndroidManifest.xml
        xmlparser = XMLParser.XMLParser(androidManifest, config)

        if len(globalValues.args) >= 2:
            #将args[1]保存到字典中
            if sysstr == "Linux":
                path_index = globalValues.args[1].rfind('/') + 1
            else:
                path_index = globalValues.args[1].rfind('\\') + 1
            out_path = globalValues.args[1][0:path_index]
            if (out_path[0] == '.'):
                out_path = sys.path[0] + out_path[1:]
            print("out_path: %s" % out_path)
        else:
            out_path = outputPath

        #读string文件
        xmlparser.read_string_XML(tempPath)
        reader = xmlparser.get_XML_Result(out_path)

        #调用__init__函数
        file_opt = fileOpt.fileOpt(apkPath, filesPath, tempPath, outputPath)
        file_opt.set_reader(reader)
        file_opt.set_xmlparser(xmlparser)

        #file_opt.copySmaliFile()

        #file_opt.copyLib()
        #新添加
        #file_opt.copyAssets()
        #file_opt.changeLauncher(androidManifest)
        #主activity文件onCreate中添加代码
        #file_opt.changeActivity()
        file_opt.addDialog()

        #重打包
        outPath = os.path.join(outputPath, fileName)
        file_opt.finish(outPath)
        print('repackage App finish!')
Beispiel #7
0
from StateGraph import *
from XMLParser import *

graph = XMLParser(graph_file="sample1a.xml", debug=False).parse()

# Check currnet state
print("Current State: {}".format(graph.get_current_state().name))

# Simulate an action
print("Simulating Action of VoiceCommand hello")
graph.apply_action(ActionType.VOICE_COMMAND, 'hello')

# Check currnet state
print("Current State: {}".format(graph.get_current_state().name))


"""
Output:

Parsing...
State:
	Name: Root state
	StateActions: 1 actions
		StateAction: (Type: voice_command, Value: hello, To: State that says hello back)
	Responses: 0 responses
State:
	Name: State that says hello back
	StateActions: 0 actions
	Responses: 3 responses
		Response: (Name: Saying Hello Back with LED, Type: led, Value: Some Random LED Value)
		Response: (Name: Sleeping for 5 seconds, Type: sleep, Value: 5000)
Beispiel #8
0
from XMLParser import *
from DataBaseFacade import *
from ConfigParser import *

# Load input and database file information
parameters = sys.argv[1:]
if(len(parameters)<2):
	print "Error! You must pass two parameters: the first one should be the input file and second one should be a database info(host, user, password, port) json file"
	sys.exit(1)
for i in range(len(parameters)):
	if i == 0:	# input
		fileInput = parameters[i]
	if i == 1:	# database file information
		config = ConfigParser(parameters[i])

db = DataBaseFacade(name = DataBaseFacade.MYSQL,host = config.host(), user = config.user(), password = config.password(), port = config.port())

x = XMLParser(fileInput)
x.parse()
x.generate()
tablesInfo = x.getTablesInfo();
tablesData = x.getTablesData();

if db.createDatabase(x.getDatabaseName()):
	print "++++	Database Created Sucefully ++++"
if db.createTables(tablesInfo):
	print "++++ Tables Created Sucefully ++++"
if db.insertData(tablesData):
	print "++++ Data Inserted Sucefully ++++"
db.closeConnection()