Tryag File Manager
Home
-
Turbo Force
Current Path :
/
home
/
cluster1
/
data
/
bu01
/
1121861
/
html
/
TMAT
/
tmat
/
plugins
/
Upload File :
New :
File
Dir
/home/cluster1/data/bu01/1121861/html/TMAT/tmat/plugins/nahuatlPlugin.py
import sys import os from subprocess import Popen, PIPE import re from twisted.plugin import IPlugin from tokenizer import TokenizerPlugin from myparser import ParserPlugin from zope.interface import Interface, Attribute, implements from xml.dom.minidom import getDOMImplementation, parseString class CommandLineTokenizer(object): implements(IPlugin, TokenizerPlugin) running = False displayName = "Command-Line (Local) Tokenizer" preferences = {"Initialization Command":"string", "Start Command":"string", "Stop Command":"string", "Tokenize Command":"string", "Get Version Command":"string", "Uninitialize Command":"string"} values = {} def setValue(self,key, val): self.values[key]=val def initialize(self): try: initCommand="%s\n" % self.values["Initialization Command"] except: initCommand="" self.pipe=Popen(initCommand,shell=True,bufsize=0,stdin=PIPE,stdout=PIPE,stderr=PIPE) def tokenize(self, text): if not(self.running): self.start() self.communicate("Tokenize Command") retText=[] ## get all of the junk off the front line="" for inLine in [x for x in text.split("\n") if x.strip()]: self.pipe.stdin.write("%s\n" % inLine.strip()) self.pipe.stdin.flush() while not(line): line = self.pipe.stdout.readline().strip() retText.append(line) while line: line = self.pipe.stdout.readline().strip() retText.append(line) retText=retText[:-1] retList=[] i=0 for token in retText: i=text.find(token,i) retList.append((i,i+len(token),True)) i += len(token) return retList def start(self): if not(self.pipe): self.initialize() self.communicate("Start Command") self.running=True def stop(self): self.communicate("Stop Command") self.running=False def communicate(self, key,readback=True): self.pipe.stdin.write("%s\n" % self.values[key]) self.pipe.stdin.flush() if readback: return self.pipe.stdout.readline() else: return 0 def version(self): return self.communicate("Get Version Command") def uninitialize(self): self.communicate("Uninitialize Command",False) if self.pipe.poll(): self.pipe.stdin.close() pid=self.pipe.pid try: os.kill(pid,9) except: pass class CommandLineParser(object): implements(IPlugin, ParserPlugin) running = False displayName = "Command Line Parser" preferences = {"Initialization Command":"string", "Start Command":"string", "Stop Command":"string", "Parse Command":"string", "Get Version Command":"string"} values = {} def setValue(self,key, val): self.values[key]=val def initialize(self): try: initCommand="%s\n" % self.values["Initialization Command"] except: initCommand="" self.pipe=Popen(initCommand,shell=True,bufsize=0,stdin=PIPE,stdout=PIPE,stderr=PIPE) def start(self): if not(self.pipe): self.initialize() self.communicate("Start Command") self.running=True def stop(self): self.communicate("Stop Command") self.running=False def communicate(self, key): self.pipe.stdin.write("%s\n" % self.values[key]) self.pipe.stdin.flush() return self.pipe.stdout.readline() def version(self): return self.communicate("Get Version Command") def uninitialize(self): self.communicate("Uninitialize Command") def parse(self, wordforms): impl=getDOMImplementation() myDom=impl.createDocument(None,"parserInput",None) topNode=myDom.documentElement for word in wordforms: wordNode=myDom.createElement("wordform") wordNode.setAttribute("form",word) topNode.appendChild(wordNode) self.input.write(myDom.toxml()) outDom=parseString(self.output.read()) topOut=outDom.getElementsByTagName("parserOutput") retDict={} for parseNode in topOut.getElementsByTagName("parses"): word=parseNode.getAttribute("form") retDict[word]=[] for parse in parseNode.getElementsByTagName("parse"): morphemes=parse.GetAttribute("morphemes") glosses=parse.GetAttribute("glosses") retDict[word].append({"morpheme":morphemes, "gloss":glosses}) return retDict def initialize(self): try: initCommand="%s\n" % self.values["Initialization Command"] except: initCommand="" self.pipe=Popen(initCommand,shell=True,bufsize=0,stdin=PIPE,stdout=PIPE,stderr=PIPE) def parseToken(self, token): return [(x, self.getGloss(x)) for x in self.getUnderlyingForms(token)] x = CommandLineTokenizer() y = CommandLineParser()