Пример #1
0
def parseSentences(sent, HOME_DIR = BASE_PATH):
    orig_Stdin = sys.stdin
    strIn = StringIO(sent)
    sys.stdin = strIn
    strOut = StringIO()
    parseInput(parser, opts, outputFile=strOut)
    sys.stdin = orig_Stdin
    # Now we can retrieve the output as a string:
    result = strOut.getvalue()
    #print >>sys.stderr, result
    #print result    
    tmp_fn = "./tmp.tmp"
    fout = open(tmp_fn,'w')
    fout.write(result)
    fout.close()
    graphs = read_dep_graphs_file(tmp_fn,False,HOME_DIR)
    ret = []
    for graph in graphs:        
        g = convert(graph)
        ret.append((g,g.tree_str))

    if not graphs:#Berkley bug?
        ret.append((GraphWrapper("",HOME_DIR),""))

    strIn.close()
    strOut.close()
    return ret
Пример #2
0
def parseSentences(sent, HOME_DIR = BASE_PATH):
    orig_Stdin = sys.stdin
    strIn = StringIO(sent)
    sys.stdin = strIn
    strOut = StringIO()
    parseInput(parser, opts, outputFile=strOut)
    sys.stdin = orig_Stdin
    # Now we can retrieve the output as a string:
    result = strOut.getvalue()
#     print result
    tmp_fn = "./tmp.tmp"
    fout = open(tmp_fn,'w')
    fout.write(result)
    fout.close()
    graphs = read_dep_graphs_file(tmp_fn,False,HOME_DIR)
    ret = []
    for graph in graphs:        
        g = convert(graph)
        ret.append((g,g.tree_str))

    if not graphs:#Berkley bug?
        ret.append((GraphWrapper("",HOME_DIR),""))

    strIn.close()
    strOut.close()
    return ret
Пример #3
0
def parseSentences(sent, HOME_DIR=BASE_PATH, stanford_json_sent=None):

    if stanford_json_sent:
        # Use Stanford json notation
        result = sent['parse'].replace("\n", "") + "\n"
    else:
        # Use default berkeley parser
        from BerkeleyInterface import parseInput
        orig_Stdin = sys.stdin
        strIn = StringIO(sent)
        sys.stdin = strIn
        strOut = StringIO()
        parseInput(parser, opts, outputFile=strOut)
        sys.stdin = orig_Stdin
        result = strOut.getvalue()

    tmp_fn = "./tmp.parse"

    fout = open(tmp_fn, 'w')
    fout.write(result)
    fout.close()
    graphs = read_dep_graphs_file(tmp_fn,
                                  False,
                                  HOME_DIR,
                                  stanford_json_sent = sent \
                                  if stanford_json_sent \
                                  else False)
    ret = []
    for graph in graphs:
        g = convert(graph)
        ret.append((g, g.tree_str))

    if not graphs:  #Berkley bug?
        ret.append((GraphWrapper("", HOME_DIR), ""))

    if (not stanford_json_sent):
        strIn.close()
        strOut.close()
    return ret
Пример #4
0
def parseSentences(sent, HOME_DIR = BASE_PATH, stanford_json_sent = None):
    resetCounter()
    if stanford_json_sent:
        # Use Stanford json notation
        result = sent['parse'].replace("\n","") + "\n"
    else:
        # Use default berkeley parser
        from BerkeleyInterface import parseInput
        orig_Stdin = sys.stdin
        strIn = StringIO(sent)
        sys.stdin = strIn
        strOut = StringIO()
        parseInput(parser, opts, outputFile=strOut)
        sys.stdin = orig_Stdin
        result = strOut.getvalue()

    tmp_fn = "./tmp.parse"
    fout = open(tmp_fn,'w')
    fout.write(result)
    fout.close()
    graphs = read_dep_graphs_file(tmp_fn,
                                  False,
                                  HOME_DIR,
                                  stanford_json_sent = sent \
                                  if stanford_json_sent \
                                  else False)
    ret = []
    for graph in graphs:        
        g = convert(graph)
        ret.append((g,g.tree_str))

    if not graphs:#Berkley bug?
        ret.append((GraphWrapper("",HOME_DIR),""))

    if (not stanford_json_sent):
        strIn.close()
        strOut.close()
    return ret