最近做一个项目,需要得到英文单词的原型,上网查资料发现了StanfordNLP这一款工具,可以处理很多NLP相关的任务,而且效果不错。于是乎用这款工具做了单词还原。

问题来了

这款工具使用java写的,而我的项目全部是用python写的,于是乎在github上面找python版本的,但是python版本的运行速度实在不敢恭维,那怎么办咧!!
想到python被称为胶水语言,理论上可以黏合各种语言才对,于是上网去查python中如何写java代码,没想到还真找到一个方法,那就是JPype包,导入这个包就可以在python中调用JVM,运行java代码了,可以在pycharm中python,java随意切换,再也不会有语法的混淆了,那叫一个酸爽!!

环境配置

俗话说,工欲善其事必先利其器!!在使用之前先办环境配好,废话不多说。
JPype用起来还有点小麻烦,py2和py3有点小区别。如果你使用的是py2,那么安装包时用:pip install JPype;如果用的是py3,请使用pip install JPype1。
即时在python环境下调用JVM,那也是调用JVM,所以我们的环境还需要安装JVM,而且要求jdk的位数和python的位数必须保持一致。所以要看好自己的python和jdk的位数(jdk安装时,如果默认路径是x86,那一般是32位的,反之为64位的)。
万事俱备,开始写代码!!

JPype的使用

首先贴上利用StanfordNLP做单词还原的java代码:

package part_of_speech_reduction;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.StringUtils;
public class get_result {public static void main(String[] args) throws IOException, InterruptedException{try {String sentence = "Many people who work in London prefer to live outside it, and to go in to their offices or schools every day by train, car or bus, even though this means they have to get up early in the morning and reach home late in the evening._ One advantage of living outside London is that houses are cheaper. Even a small flat in London without a garden costs quite a lot to rent. With the same money, one can get a little house in the country with a garden of one's own. Then, in the country one can really get away from the noise and hurry of busy working lives. Even though one has to get up earlier and spend more time in trains or buses, one can sleep better at night and during weekends and on summer evenings, one can enjoy the fresh, clean air of the country. If one likes gardens, one can spend one's free time digging, planting, watering and doing the hundred and one other jobs which are needed in a garden. Then, when the flowers and vegetables come up, one has got the reward together with those who have shared the secret of nature. Some people, however, take no interest in country things: for them, happiness lies in the town, with its cinemas and theatres, beautiful shops and busy streets, dance-halls and restaurants. Such people would feel that their life was not worth living if they had to live it outside London. An occasional (偶谆謩) walk in one of the parks and a fortnight's (two weeks) visit to the sea every summer is all the country they want: the rest of the country they are quite prepared to spend with those who are glad to get away from London every night.\r\n";String result = deal_file(sentence);
//          Runtime.getRuntime().exec("cls");new ProcessBuilder("cmd","/c","cls").inheritIO().start().waitFor();System.out.println(result);} catch (Exception e) {// TODO: handle exception}}public static String deal_file(String sentence){String text = "";List<String> word = getlema(sentence);text = StringUtils.join(word, " ");return text;}public static List<String> getlema(String text) {// TODO Auto-generated method stubList<String> wordslist = new ArrayList<>();Properties props = new Properties();props.put("annotators", "tokenize, ssplit, pos, lemma");StanfordCoreNLP pipeline = new StanfordCoreNLP(props);Annotation document = new Annotation(text);pipeline.annotate(document);List<CoreMap> words = document.get(CoreAnnotations.SentencesAnnotation.class);for (CoreMap word_temp:words ) {for(CoreLabel token: word_temp.get(CoreAnnotations.TokensAnnotation.class)) {String lema = token.get(CoreAnnotations.LemmaAnnotation.class);wordslist.add(lema);}}return wordslist;}
}

写好java程序后,我们将其打包成jar包,自己起个名字,如何打包成jar包请自行百度,打包时选择可运行的jar文件,可以免去后续的配置。

单词还原之后我们便可以直接在pycharm中通过调用JPype来使用我们的java程序了,话不多说,直接上代码:

import jpype
from jpype import *
import os
#路径为刚才导出的jar包的路径
jar_path = os.path.join(os.path.abspath('.'), r'H:\company\cpit_cixinghuanyuan_v2.5.jar')
#路径为java程序所依赖的jar包的集合
ext_jar_path = os.path.join(os.path.abspath('.'), r'H:\company\extend_jars')
ext_jar = "-Djava.ext.dirs="+ext_jar_path
Djava = "-Djava.class.path="+jar_path
jvm_path = get_default_jvm_path()
jpype.startJVM(jvm_path, Djava, ext_jar)
#java程序所在包名
JPackge = jpype.JPackage("part_of_speech_reduction")
texts = "Many people who work in London prefer to live outside it, and to go in to their offices or school."
#调用函数
difference = JPackge.get_result.deal_file(texts)
print(difference)
shutdownJVM()

注意!!jvm只能启动一次,关闭一次,否则会报错。

在网上查资料的过程中,还看到了另一种思路,利用python调用dos命令行,再用dos命令行来调用jvm。这种方法大致看了一下思路,还没有深入研究。

在python编译器中调用JAVA程序——JPype使用总结相关推荐

  1. JPype:实现在python中调用JAVA 和 错误处理

    JPype使用说明 1.安装jpype 安装的时候输入:  pip install jpype1  (后面要加一个1) 2.启动JVM JPype 提供的 startJVM() 函数的作用是启动 JA ...

  2. Python利用JPype调用Java对象方法【实现在Python中调用JAVA】

    一.JPype简述 1.JPype是什么? JPype是一个能够让 python 代码方便地调用 Java 代码的工具,从而克服了 python 在某些领域(如服务器端编程)中的不足. 2.JPype ...

  3. python调用java之Jpype实现java接口

    python实现java接口 概述 java程序 java接口 java测试函数 在python中实现java接口的两种方式 第一种:JImplements JImplements的使用示例 JImp ...

  4. 如何用pycharm调用Java_JPype实现在python中调用JAVA的实例

    一.JPype简述 1.JPype是什么? JPype是一个能够让 python 代码方便地调用 Java 代码的工具,从而克服了 python 在某些领域(如服务器端编程)中的不足. 2.JPype ...

  5. java中调用matlab程序及相关问题

    **在java中调用matlab程序,首先要将matlab程序打包成jar文件,才可以被java程序所调用. 打包时本人所用的各种工具: 1:myeclipse-bule 2:java-1.7 3:m ...

  6. java和matlab的交互实践--在java项目中调用matlab程序

    本文主要解决了java项目中调用matlab程序过程中遇到的问题,下文主要分matlab的jar包部署以及java项目配置两个方面来分析. 1.matlab项目的部署 -- 生成jar包 保存matl ...

  7. PHP中调用Java类的两个办法

    Java语言功能强大,因此在许多情况下在php中来调用Java的功能将十分有用.在php中调用Java语言有两种方法,一种是使用php中的Java扩展模块,另一种是使用minij2ee应用服务器提供的 ...

  8. 如何在C++中嵌入JAVA程序

    如何在C++中嵌入JAVA程序呢?现在的程序员,不再像以前一样,掌握一种编程语言就可以混得有模有样了,这里为大家讲的,就是C++与Java混合编程.局域网聊天软件举几个简单的例子,一个软件为了快速开发 ...

  9. matlab中调用java代码_Matlab中调用第三方Java代码

    在Java中采用Matlab JA Builder可以实现调用m文件,采用这样的方式,可在Matlab的M文件中,直接调用Java类.这种方式可以表示为Java--> Matlab( m, Ja ...

  10. 在R中调用Java代码

    我们都知道Java语言长期处于霸主地位,在所有编程语言排行榜中,Java也是常年位居第一.在近几年也是爆炸式发展,几乎覆盖到了应用开发的所有领域.而R语言则在统计圈和数据可视化处于佼佼者,如果将Jav ...

最新文章

  1. 在JAVA中线程到底起到什么作用
  2. android电视盒子解码很慢,电视盒子反应慢又卡原因及解决办法推荐!
  3. Ubuntu如何搭建Django与Flup和Nginx环境?
  4. 导出excel 后 页面按钮失效(页面假死)
  5. PCWorld:Google到底要干什么?
  6. 内存碎片是什么?关于内存碎片的解释
  7. 通信光缆故障检测如何进行检测?
  8. 计算机显卡驱动停止工作,win7出现显卡器驱动程序已停止响应怎么办
  9. OEM嵌入式通讯模块与西门子PLC S7-1200通讯测试指南
  10. 如何装计算机网络驱动,网卡驱动怎么安装,手把手教你电脑网卡驱动怎么安装...
  11. 随笔—醒悟篇之考研调剂
  12. php strpos 区分大小写么?,PHP strpos() 函数
  13. matlab中ref代表什么,化妆品ref什么意思
  14. OpenGL摄像机(Look At矩阵)
  15. 2022年全球市场高空作业平台总体规模、主要生产商、主要地区、产品和应用细分研究报告
  16. 【项目】用户可自定义简易宏键盘
  17. 手机录音文件如何转换成文本?具体如何操作?
  18. python测试框架untest_python自动化测试框架unittest
  19. 高中计算机专业班主任工作总结,中等专业学校计算机班主任年工作总结
  20. 首师大附中科创教育平台 我的刷题记录 0325 50212228海岛帝国:LYF的太空运输站...

热门文章

  1. 软件的行业适配性:进销存软件与五金行业
  2. iPhone X改11Pro在线/免越狱QQ空间任意iPhone机型虚拟定位
  3. journalctl命令详解,与如何查看系统日志
  4. 计算机系统字体推荐,10大最合适编程的字体推荐
  5. 苹果画画软件_数位板可以连手机画画?有哪些好用的绘画APP?
  6. java方法重载的好处_Java方法重载浅谈
  7. Python如何安装pandas库,简单3步解决,亲测有效。
  8. 动态规划(dp)的总结
  9. 中国省市区列表MySQLl数据库脚本
  10. java怎么实现直方图均衡化_直方图均衡化原理与实现