def __init__(self, jieba=False, stanford=True): if jieba: self.json = json from seg import AllInfo from stanford import Stanford self.w = AllInfo() self.s = None if stanford: self.st = Stanford(False) else: self.dep = Dep() else: self.p = re.compile(u"\u25aa") self.json = json self.dep = Dep() from seg import Seg from stanford import Stanford self.w = Seg() self.s = Stanford(False)
def setup_example_fig5(): G = Dep('Fig5') for k in 'a','b','c','d','e': G.Nodes.append(k) G.Latency = {} for k in G.Nodes: G.Latency[k] = 1 edges =[('a','b'),('b','e'),('a','c'),('c','e'),\ ('b','e'),('a','d'),('d','e')] dist = [] for k in edges: dist.append(0) G.do_init_work(edges,dist) print '-------------------------------------' print G print '-------------------------------------' with open('digraph_fig5.dot','w') as file: file.write(G.dot_graph())
class Merge: # @jieba True means it will use jieba to ner #'cause the result of ner using jieba is not good enough,so @jieba must be False till i got a new way to get a better result.that means self.s always is not None def __init__(self, jieba=False, stanford=True): if jieba: self.json = json from seg import AllInfo from stanford import Stanford self.w = AllInfo() self.s = None if stanford: self.st = Stanford(False) else: self.dep = Dep() else: self.p = re.compile(u"\u25aa") self.json = json self.dep = Dep() from seg import Seg from stanford import Stanford self.w = Seg() self.s = Stanford(False) # ner,pos must like nn,nr,vv,ww # dep must like word_id@@word dep+head\t # the input is a str(result in line) def _merge_with_str(self, line_ner, line_pos, line_dep, line_seg): ner = line_ner.split(",") pos = line_pos.split(",") if line_dep is not None: deps = line_dep.split("\t") line = "" lens = len(ner) - 1 for dep in deps: info = dep.split("@@") id = int(info[0]) if id > lens: continue line += info[1].decode("gbk") line += " " + pos[id] + "\t" # line += ' '+ner[id]+' '+pos[id]+'\t' line = line.strip("\t") return line else: seg = line_seg.split(" ") line = "" if len(seg) != len(pos): print line_seg.encode("utf-8") print line_pos for id in xrange(len(ner)): if ner[id] != "O": seg[id] = ner[id] # line += seg[id] + ' ' + ner[id]+' '+pos[id]+'\t' line += seg[id] + " " + pos[id] + "\t" line = line.strip("\t") return line # this method is for processing the json def _process(self, line_json): decoded = self.json.loads(line_json) line_ner = decoded["ner"] line_pos = decoded["pos"] line_seg = decoded["seg"] return (line_ner, line_pos, line_seg) # this method is for getting all info of a line,without merging them in a line(return tuple) def _process_line(self, line_json): (line_ner, line_pos, line_seg) = self._process(line_json) line_dep = self.dep.dep_from_line(line_seg.encode("gbk")) deps = line_dep.split("\t") line = "" for dep in deps: info = dep.split("@@") info = info[1].split(" ") line += info[1].decode("gbk") + " " line = line.strip(" ") return (line_ner, line_pos, line_seg, line) # this method will parse the line and merge all info # the method will be used when i just have the json(including seg ner and pos) form indri # it can get the dep from stanford can merge it into a line # so it should not be used right now def merge(self, line_json, dep=False): if dep: (line_ner, line_pos, line_seg) = self._process(line_json) line_seg = self.p.sub(".", line_seg) line_dep = self.dep.dep_from_line(line_seg.encode("gbk")) line = self._merge_with_str(line_ner, line_pos, line_dep, None) return line else: (line_ner, line_pos, line_seg) = self._process(line_json) line = self._merge_with_str(line_ner, line_pos, None, line_seg) return line def add_new_words(self, newwords): self.w.add_words(newwords) def ner_using_nlpc(self, line): (line_seg, pos, ner) = self.w.getInfo(line) line_ner = self.dep._dep_line(line_seg.encode("gbk", "ignore")) sner = line_ner.split("\t") # (line_seg,line_pos,line_ner) = self.dep._dep_all(line.encode('gbk','ignore')) if len(ner) != len(sner): return ("", "", "") for i in xrange(len(ner)): j = ner[i] if j != "other": sner[i] = j return ("\t".join(line_seg.split(" ")), "\t".join(pos), "\t".join(sner)) # return (line_seg,line_pos,line_ner) # this method is for get a json of a line # now it's for testing # i need to get the json(including seg ner and pos) form indri,and then use stanford to get the dep # BTW,I don't have to use dep anymore,so the method won't return dep # so,if stanford(True),the method must change def _get_line_json(self, line): if self.s is not None: dict = {"seg": "", "ner": "", "pos": ""} dict["seg"] = self.w.seg(line) (dict["ner"], dict["pos"]) = self.s.get_ner_pos(dict["seg"]) return self.json.dumps(dict) else: (line_seg, pos, ner) = self.w.getInfo(line) (line_ner, line_pos) = self.st.get_ner_pos(line_seg) sner = line_ner.split(",") if len(ner) != len(sner): return ("", "", "") # print line_seg.encode('utf-8') # print ','.join(ner) # print line_ner for i in xrange(len(ner)): j = ner[i] if j != "other": sner[i] = j # print ','.join(sner) return (line_seg, line_pos, ",".join(sner)) # @dep False means without dep def get_line_info(self, line_json, dep=False): if self.s is not None: if dep: return self._process_line(line_json) else: (line_ner, line_pos, line_seg) = self._process(line_json) return (line_ner, line_pos, line_seg, None) else: (line_seg, line_pos, line_ner) = self._get_line_json(line_json) print line_seg print line_pos return (",".join(line_ner), ",".join(line_pos), " ".join(line_seg), None) # this method is for testing # it will use the jieba and standford tool to get the ner and pos's results. Then, transform them into json and use the method 'merge' to test whether it print the correct pattern # correct pattern:word dep(use ',' to split all result of parsing) ner pos\tword dep ner pos def test(self): for line in sys.stdin: line = line.strip("\n") # print line line_json = self._get_line_json(line) line = self.merge(line_json) print line.encode("utf-8") # this method is for testing def test2(self): for line in sys.stdin: line = line.strip("\n") if self.s is not None: line_json = self._get_line_json(line) (line_ner, line_pos, line_seg, line_dep) = self.get_line_info(line_json, False) print line_ner print line_pos print line_seg.encode("utf-8") # print line_dep.encode('utf-8') else: (line_ner, line_pos, line_seg, line_dep) = self.get_line_info(line, False) print line_seg.encode("utf-8") print line_ner print line_pos
def setup_example_fig7(): G = Dep('Fig7') G.Latency = {} for k in 'ABCDEFGHIJKLM': G.Nodes.append(k) G.typeof[k] = 'Alu' for k in G.Nodes: G.Latency[k] = 2 edges = ('AC','CF','DF','FA','AD','BD', 'BE','DG','GJ','JM','FI','IL', 'IG','IM','EH','HK','HJ') dist = [] for k in edges: if k == 'FA': dist.append(1) elif k == 'IG' : dist.append(1) else: dist.append(0) roots = ['A'] G.do_init_work(edges,dist,roots) print '-------------------------------------' print G with open('digraph_fig7.dot','w') as file: file.write(G.dot_graph()) O = [] G.Order(['A','C','D','F'],O) G.Order(['G','J','M','I'],O) G.Order(['B','E','H','K','L'],O) G.MII = 6 while True: S = Scoreboard(4,4,4,G.MII) myO = [k for k in O] G.Early_Startu = {} G.Late_Startu = {} if G.schedule(myO,S) == True: print 'Order:', '<'+','.join(O)+'>' print S break G.MII += 1
def setup_example_fig1(): while True: G = Dep('Fig1') G.Latency = {} for k in range(1,13): G.Nodes.append('n'+str(k)) if k != 7: G.Latency['n'+str(k)] = 2 else: G.Latency['n7'] = 1 edges =[('n1','n3'),('n1','n5'),('n1','n6'),\ ('n3','n4'),('n4','n7'),('n5','n8'),\ ('n8','n10'),('n10','n11'),('n11','n12'),\ ('n2','n8'),('n9','n10'),('n6','n11'),\ ('n1','n4')] dist = [] for k in edges: dist.append(0) G.do_init_work(edges,dist) print '-------------------------------------' print G G.dot_graph() with open('digraph_fig1.dot','w') as file: file.write(G.dot_graph()) G.typeof['n1'] = 'LdSt' G.typeof['n2'] = 'LdSt' G.typeof['n9'] = 'LdSt' G.typeof['n7'] = 'LdSt' G.typeof['n12'] = 'LdSt' G.typeof['n4'] = 'Mpy' G.typeof['n6'] = 'Mpy' G.typeof['n8'] = 'Mpy' G.typeof['n11'] = 'Mpy' for k in G.Nodes: if k not in G.typeof: G.typeof[k] = 'Alu' O = [] G.Order([k for k in G.Nodes],O) G.MII = 4 S = Scoreboard(1,1,2,G.MII) myO = [k for k in O] G.Early_Startu = {} G.Late_Startu = {} if G.schedule(myO,S) == True: print 'Order:', '<'+','.join(O)+'>' print S print '-------------------------------------' break G.MII += 1