当前位置:   article > 正文

Apache OpenNLP使用_apache opennlp sentencemodel

apache opennlp sentencemodel


  1. import java.io.*;
  2. import opennlp.tools.sentdetect.SentenceDetectorME;
  3. import opennlp.tools.sentdetect.SentenceModel;
  4. import opennlp.tools.tokenize.Tokenizer;
  5. import opennlp.tools.tokenize.TokenizerME;
  6. import opennlp.tools.tokenize.TokenizerModel;
  7. import opennlp.tools.util.Span;
  8. public class Testing_openNLP {
  9. /* http://opennlp.apache.org/documentation/1.5.3/manual/opennlp.html 官方教程Apache OpenNLP Developer Documentation
  10. * openNLP 中的各种模型可以在 http://opennlp.sourceforge.net/models-1.5/ 下载
  11. * http://www.programcreek.com/2012/05/opennlp-tutorial/ this is good tutorial about openNLP tools
  12. *
  13. * */
  14. public static void main(String[] args) {
  15. // String testString = "This isn't the greatest example sentence in the world because I've seen better. Neither is this one. This one's not bad, though.";
  16. String testString = "Hi. How are you? This is &3 $444 Mike." ;
  17. String tokens[] = Token(testString);
  18. String sentences[] = sentenceSegmentation(testString);
  19. String aa = "";
  20. }
  21. //分句
  22. public static String[] sentenceSegmentation(String str){
  23. try {
  24. InputStream modelIn = new FileInputStream("en-sent.bin");
  25. SentenceModel model = null;
  26. try {
  27. model = new SentenceModel(modelIn);
  28. }
  29. catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. finally {
  33. if (modelIn != null) {
  34. try {
  35. modelIn.close();
  36. }
  37. catch (IOException e) {
  38. }
  39. }
  40. }
  41. SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);
  42. String sentences[] = sentenceDetector.sentDetect(str);
  43. return sentences;
  44. } catch (FileNotFoundException e1) {
  45. e1.printStackTrace();
  46. return null;
  47. }
  48. }
  49. //分词
  50. public static String[] Token(String str){
  51. try{
  52. InputStream modelIn = new FileInputStream("en-token.bin");
  53. TokenizerModel model = null;
  54. try {
  55. model = new TokenizerModel(modelIn);
  56. }
  57. catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. finally {
  61. if (modelIn != null) {
  62. try {
  63. modelIn.close();
  64. }
  65. catch (IOException e) {
  66. }
  67. }
  68. }
  69. TokenizerME tokenizer = new TokenizerME(model);
  70. String tokens[] = tokenizer.tokenize(str);
  71. // double tokenProbs[] = tokenizer.getTokenProbabilities();//must be called directly after one of the tokenize methods was called.
  72. return tokens;
  73. }
  74. catch(FileNotFoundException e){return null;}
  75. }
  76. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号