diff --git a/python/examples/lac/lac_reader.py b/python/examples/lac/lac_reader.py index 3895277dbbf98a9b4ff2d6592d82fe02fe9a4c12..c9f31c148123e1975c0102903abf2f2b3b15d3f6 100644 --- a/python/examples/lac/lac_reader.py +++ b/python/examples/lac/lac_reader.py @@ -108,15 +108,15 @@ class LACReader(object): partial_word = "" for ind, tag in enumerate(tags): if partial_word == "": - partial_word = words[ind] + partial_word = self.id2word_dict[str(words[ind])] tags_out.append(tag.split('-')[0]) continue if tag.endswith("-B") or (tag == "O" and tag[ind - 1] != "O"): sent_out.append(partial_word) tags_out.append(tag.split('-')[0]) - partial_word = words[ind] + partial_word = self.id2word_dict[str(words[ind])] continue - partial_word += words[ind] + partial_word += self.id2word_dict[str(words[ind])] if len(sent_out) < len(tags_out): sent_out.append(partial_word) diff --git a/python/examples/lac/lac_web_service.py b/python/examples/lac/lac_web_service.py index bd9ace1230766deb5adf35a65ca9e01c7cd0b617..c9bd00986c62abde3ee24ddddbf08dda45bbed05 100644 --- a/python/examples/lac/lac_web_service.py +++ b/python/examples/lac/lac_web_service.py @@ -22,22 +22,24 @@ class LACService(WebService): self.reader = LACReader("lac_dict") def preprocess(self, feed={}, fetch=[]): - batch_feed = [] + feed_batch = [] for ins in feed: if "words" not in ins: raise ("feed data error!") feed_data = self.reader.process(ins["words"]) - batch_feed.append({"words": feed_data}) + feed_batch.append({"words": feed_data}) fetch = ["crf_decode"] - return batch_feed, fetch + return feed_batch, fetch def postprocess(self, feed={}, fetch=[], fetch_map={}): - ret = [] + batch_ret = [] for idx, ins in enumerate(feed): + begin = fetch_map['crf_decode.lod'][idx] + end = fetch_map['crf_decode.lod'][idx + 1] segs = self.reader.parse_result(ins["words"], - fetch_map[idx]["crf_decode"]) - ret.append({"word_seg": "|".join(segs)}) - return ret + fetch_map["crf_decode"][begin:end]) + batch_ret.append({"word_seg": "|".join(segs)}) + return batch_ret lac_service = LACService(name="lac") @@ -46,3 +48,4 @@ lac_service.load_reader() lac_service.prepare_server( workdir=sys.argv[2], port=int(sys.argv[3]), device="cpu") lac_service.run_server() +lac_service.run_flask()