之前有介绍过图形化生成的过程,但是需求变了,需要把数据预处理的现成东西写成一个完整的模块,我使用python方法调用java程序,来使用stanford parse。
这个是使用命令来执行:(就是官方打包好的一个例子)
然后就开始看我们的正儿八经的代码吧:
在数据预处理 中,使用cmd调用stanford parse的代码
def dependency_parse(filepath, cp='', tokenize=True):
print('\nDependency parsing ' + filepath)
dirpath = os.path.dirname(filepath)#得到文件的绝对路径
filepre = os.path.splitext(os.path.basename(filepath))[0]#得到路径中最后的文件名和后缀
tokpath = os.path.join(dirpath, filepre + '.toks')
parentpath = os.path.join(dirpath, filepre + '.parents')
relpath = os.path.join(dirpath, filepre + '.rels')
tokenize_flag = '-tokenize - ' if tokenize else ''
cmd = ('java -cp %s DependencyParse -tokpath %s -parentpath %s -relpath %s %s < %s'
% (cp, tokpath, parentpath, relpath, tokenize_flag, filepath))
os.system(cmd)
- 先编译javac -cp 依赖.jar xxx.java
- 然后把class复制到lib下
- 修改.py文件
- 运行
把这个java文件复制到stanford-parser文件夹里面编译的
在路径下执行
会出来一个警告,不要在意:
得到class文件
将这个class文件复制到上层目录下,因为这里路径指定死了,所以丢到上层目录下
修改.py文件:把:改成;
然后回去执行python