def test_law_node_finder(self): finder = LawNodeFinder(self.build_sample_law_tree()) nodes = finder.find(Query.from_text('第一条第二項')) self.assertEqual(1, len(nodes)) node = nodes[0] self.assertTrue(isinstance(node, Paragraph)) self.assertEqual('第一条第二項', node.sentence)
def test_apply_replace(self): node = Paragraph(title='第一項', sentence='私はネコです') node_finder = LawNodeFinder([node]) action = parse_action_text('第一項中「ネコ」を「イヌ」に改める') apply_replace(action, node_finder) self.assertEqual('私はイヌです', node.sentence)
def test_apply_delete(self): node = Paragraph(title='第一項', sentence='私はネコです') node_finder = LawNodeFinder([node]) action = parse_action_text('第一項中「私は」及び「です」を削る') apply_delete(action, node_finder) self.assertEqual('ネコ', node.sentence)
def test_apply_add_word_fail(self): node = Paragraph(title='第一項', sentence='私はネコです') node_finder = LawNodeFinder([node]) action = parse_action_text('第一項中「サル」の下に「ザメ」を加える') with self.assertRaises(TextNotFoundError): apply_add_word(action, node_finder)
def test_apply_add_word(self): node = Paragraph(title='第一項', sentence='私はネコです') node_finder = LawNodeFinder([node]) action = parse_action_text('第一項中「ネコ」の下に「ザメ」を加える') apply_add_word(action, node_finder) self.assertEqual('私はネコザメです', node.sentence)
def test_apply_replace_multiple_fail(self): node = Paragraph(title='第一項', sentence='私はネコネコです') node_finder = LawNodeFinder([node]) action = parse_action_text('第一項中「ネコ」を「イヌ」に改める') with self.assertRaises(MultipleTextFoundError): apply_replace(action, node_finder)
def test_apply_delete_fail(self): node = Paragraph(title='第一項', sentence='私はネコです') node_finder = LawNodeFinder([node]) action = parse_action_text('第一項中「私は」及び「でした」を削る') with self.assertRaises(TextNotFoundError): apply_delete(action, node_finder) self.assertEqual('私はネコです', node.sentence) # not changed
def main(law_fp, gian_fp, out_fp, stat_fp, applied_fp, failed_fp, skipped_fp): LOGGER.info(f'Start to parse {law_fp}') try: meta = extract_law_meta(law_fp) nodes = parse_xml_fp(law_fp) except Exception as e: msg = f'failed to parse {law_fp}: {e}' LOGGER.error(msg) sys.exit(1) node_finder = LawNodeFinder(nodes) if gian_fp: LOGGER.info(f'Start to apply {gian_fp}') stats_factory = StatsFactory(['file', 'process', 'success']) applied_actions, failed_actions, skipped_actions = apply_gian(gian_fp, node_finder) process_count = len(applied_actions) + len(failed_actions) success_count = len(applied_actions) stats_factory.add({'file': gian_fp, 'process': process_count, 'success': success_count}) LOGGER.info('Applied {} / {} actions'.format(success_count, process_count)) if stat_fp: stats_factory.commit(stat_fp) LOGGER.info(f'Appended stats to {stat_fp}') if applied_fp: with open(applied_fp, 'w') as f: for action in applied_actions: f.write(json.dumps(action.to_dict(), ensure_ascii=False) + '\n') LOGGER.info(f'Saved applied actions to {applied_fp}') if failed_fp: with open(failed_fp, 'w') as f: for action in failed_actions: f.write(json.dumps(action.to_dict(), ensure_ascii=False) + '\n') LOGGER.info(f'Saved failed actions to {failed_fp}') if skipped_fp: with open(skipped_fp, 'w') as f: for action in skipped_actions: f.write(json.dumps(action.to_dict(), ensure_ascii=False) + '\n') LOGGER.info(f'Saved failed actions to {skipped_fp}') save_law_tree(meta['LawTitle'], nodes, out_fp) LOGGER.info(f'Saved result to {out_fp}')
def test_law_node_finder_fail(self): finder = LawNodeFinder(self.build_sample_law_tree()) self.assertEqual(0, len(finder.find(Query.from_text('第一条第一号イ')))) self.assertEqual(0, len(finder.find(Query.from_text('第三条'))))