示例#1
0
def run(args):
    if args.agent:
        run_agent(args.agent,
                  lang=args.lang,
                  hg=hgraph(args.hg),
                  infile=args.infile,
                  url=args.url,
                  sequence=args.sequence)
    elif args.system:
        run_system(args.system,
                   lang=args.lang,
                   hg=hgraph(args.hg),
                   infile=args.infile,
                   url=args.url,
                   sequence=args.sequence)
    else:
        raise RuntimeError('Either agent or system must be specified.')
示例#2
0
def run(args):
    if args.agent:
        run_agent(args.agent,
                  lang=args.lang,
                  parser_class=args.parser,
                  hg=hgraph(args.hg),
                  infile=args.infile,
                  indir=args.indir,
                  url=args.url,
                  sequence=args.sequence,
                  corefs=args.corefs)
    elif args.system:
        run_system(args.system,
                   lang=args.lang,
                   parser_class=args.parser,
                   hg=hgraph(args.hg),
                   infile=args.infile,
                   indir=args.indir,
                   url=args.url,
                   sequence=args.sequence,
                   corefs=args.corefs)
    else:
        raise RuntimeError('Either agent or system must be specified.')
示例#3
0
    def setUp(self):
        self.hg = hgraph('test.hg')
        self.hg.destroy()

        concepts = []
        concepts.append(hedge('paris/C'))
        concepts.append(hedge('(of/B city/C paris/C)'))
        concepts.append(hedge('(of/B capital/C france/C)'))
        concepts.append(hedge('berlin/C'))
        concepts.append(hedge('(of/B city/C berlin/C)'))
        concepts.append(hedge('(of/B capital/C germany/C)'))
        self.concepts = concepts

        self.hg.add('(love/P i/C (of/B city/C paris/C))')
        self.hg.add('(hate/P i/C (of/B city/C paris/C))')
示例#4
0
 def setUp(self):
     self.hg = hgraph('test.hg')
示例#5
0
def cli():
    _show_logo()

    parser = argparse.ArgumentParser()

    parser.add_argument('command', type=str, help='command to execute')
    parser.add_argument('--agent', type=str, help='agent name', default=None)
    parser.add_argument('--col', type=str, help='table column', default=None)
    parser.add_argument('--corefs',
                        help='perform coreference resolution',
                        action='store_true')
    parser.add_argument('--fields', type=str, help='field names', default=None)
    parser.add_argument('--hg',
                        type=str,
                        help='hypergraph db',
                        default='gb.db')
    parser.add_argument('--indir',
                        type=str,
                        help='input directory',
                        default=None)
    parser.add_argument('--infile', type=str, help='input file', default=None)
    parser.add_argument('--lang', type=str, help='language', default=None)
    parser.add_argument('--outdir',
                        type=str,
                        help='output directory',
                        default=None)
    parser.add_argument('--outfile',
                        type=str,
                        help='output file',
                        default=None)
    parser.add_argument('--parser', type=str, help='parser', default=None)
    parser.add_argument('--pattern',
                        type=str,
                        help='edge pattern',
                        default='*')
    parser.add_argument('--sequence',
                        type=str,
                        help='sequence name',
                        default=None)
    parser.add_argument('--show_namespaces',
                        help='show namespaces',
                        action='store_true')
    parser.add_argument('--system',
                        type=str,
                        help='agent system file',
                        default=None)
    parser.add_argument('--text',
                        type=str,
                        help='text identifier',
                        default='title')
    parser.add_argument('--training_data',
                        type=str,
                        help='training data for ML tasks',
                        default=None)
    parser.add_argument('--url', type=str, help='url', default=None)

    args = parser.parse_args()

    # determine language
    if args.parser:
        plang = parser_lang(args.parser)
        if args.lang:
            if args.lang != plang:
                msg = 'specified language ({}) and parser language ({}) do '\
                      'not match'.format(args.lang, plang)
                error_msg()
                sys.exit(-1)
        else:
            args.lang = plang
    # if not lang or parser is specified, default to 'en'
    elif not args.lang:
        args.lang = 'en'

    print(colored('{}\n'.format('command: {}'.format(args.command)), 'white'))

    if args.agent:
        print('agent: {}'.format(args.agent))
    if args.col:
        print('column: {}'.format(args.col))
    if args.corefs:
        print('coreferences: {}'.format(args.corefs))
    if args.hg:
        print('hypergraph: {}'.format(args.hg))
    if args.infile:
        print('input file: {}'.format(args.infile))
    if args.lang:
        print('language: {}'.format(args.lang))
    if args.parser:
        print('parser: {}'.format(args.parser))
    if args.outfile:
        print('output file: {}'.format(args.outfile))
    if args.sequence:
        print('sequence: {}'.format(args.sequence))
    if args.system:
        print('system: {}'.format(args.system))
    if args.training_data:
        print('training data: {}'.format(args.training_data))
    if args.url:
        print('url: {}'.format(args.url))

    print()

    if args.command == 'create':
        hgraph(args.hg)
        print('Hypergraph database created.')
    elif args.command == 'export':
        print('exporting hypergraph...')
        hg = hgraph(args.hg)
        n = 0
        with open(args.outfile, 'w') as f:
            for edge, attributes in hg.all_attributes():
                row = [str(edge), attributes]
                f.write('{}\n'.format(json.dumps(row, ensure_ascii=False)))
                n += 1
        print('{} edges exported.'.format(n))
    elif args.command == 'import':
        print('importing hypergraph...')
        hg = hgraph(args.hg)
        n = 0
        with open(args.infile, 'r') as f:
            for line in f:
                edge_str, attributes = json.loads(line)
                hg.add_with_attributes(hedge(edge_str), attributes)
                n += 1
        print('{} edges imported.'.format(n))
    elif args.command == 'txt':
        TxtReader(args.infile,
                  hg=hgraph(args.hg),
                  sequence=args.sequence,
                  lang=args.lang,
                  corefs=args.corefs).read()
    elif args.command == 'dir':
        DirReader(args.indir,
                  hg=hgraph(args.hg),
                  sequence=args.sequence,
                  lang=args.lang,
                  corefs=args.corefs).read()
    elif args.command == 'csv':
        CsvReader(args.infile,
                  args.col,
                  hg=hgraph(args.hg),
                  sequence=args.sequence,
                  lang=args.lang,
                  corefs=args.corefs).read()
    elif args.command == 'wikipedia':
        WikipediaReader(args.url,
                        hg=hgraph(args.hg),
                        sequence=args.sequence,
                        lang=args.lang,
                        corefs=args.corefs).read()
    elif args.command == 'reddit':
        RedditReader(args.infile,
                     hg=hgraph(args.hg),
                     sequence=args.sequence,
                     lang=args.lang,
                     corefs=args.corefs).read()
    elif args.command == 'actors':
        Actors(hg=hgraph(args.hg), sequence=args.sequence).run()
    elif args.command == 'claims':
        Claims(hg=hgraph(args.hg), sequence=args.sequence).run()
    elif args.command == 'conflicts':
        Conflicts(hg=hgraph(args.hg), sequence=args.sequence).run()
    elif args.command == 'names':
        CorefsNames(hg=hgraph(args.hg), sequence=args.sequence).run()
    elif args.command == 'number':
        Number(hg=hgraph(args.hg), sequence=args.sequence).run()
    elif args.command == 'onto':
        CorefsOnto(hg=hgraph(args.hg), sequence=args.sequence).run()
    elif args.command == 'taxonomy':
        Taxonomy(hg=hgraph(args.hg), sequence=args.sequence).run()
    else:
        raise RuntimeError('Unknown command: {}'.format(args.command))

    print()