Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6542246
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Java

2022-03-09 09:35:22

环境:Spring Boot 2.0.6 + maven 3.5.3 + JDK1.8
关键配置
pom.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="" xmlns:xsi=""
  3.     xsi:schemaLocation=" ">
  4.     <modelVersion>4.0.0</modelVersion>

  5.     <groupId>com.xlc</groupId>
  6.     <artifactId>demohmm</artifactId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <packaging>jar</packaging>

  9.     <name>demohmm</name>
  10.     <description>Demo project for Spring Boot</description>

  11.     <parent>
  12.         <groupId>org.springframework.boot</groupId>
  13.         <artifactId>spring-boot-starter-parent</artifactId>
  14.         <version>2.0.6.RELEASE</version>
  15.         <relativePath/> <!-- lookup parent from repository -->
  16.     </parent>

  17.     <properties>
  18.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20.         <java.version>1.8</java.version>
  21.     </properties>

  22.     <dependencies>
  23.         <dependency>
  24.             <groupId>org.springframework.boot</groupId>
  25.             <artifactId>spring-boot-starter-web</artifactId>
  26.         </dependency>

  27.         <dependency>
  28.             <groupId>org.springframework.boot</groupId>
  29.             <artifactId>spring-boot-starter-test</artifactId>
  30.             <scope>test</scope>
  31.         </dependency>
  32.         
  33.         
  34.         <dependency>
  35.             <groupId>org.springframework.boot</groupId>
  36.             <artifactId>spring-boot-starter-data-jpa</artifactId>
  37.         </dependency>
  38.  
  39.         <dependency>
  40.             <groupId>mysql</groupId>
  41.             <artifactId>mysql-connector-java</artifactId>
  42.         </dependency>
  43.         
  44.     </dependencies>

  45.     <build>
  46.         <plugins>
  47.             <plugin>
  48.                 <groupId>org.springframework.boot</groupId>
  49.                 <artifactId>spring-boot-maven-plugin</artifactId>
  50.             </plugin>
  51.         </plugins>
  52.     </build>


  53. </project>
application.yml

点击(此处)折叠或打开

  1. spring:
  2.   datasource:
  3.      driver-class-name: com.mysql.jdbc.Driver
  4.      url: jdbc:mysql:///parameter?useSSL=false
  5.      username: root
  6.      password: 123456
  7.   jpa:
  8.     hibernate:
  9.       ddl-auto: update
  10.     show-sql: true
Table1.java

点击(此处)折叠或打开

  1. package com.xlc.hmm;

  2. import javax.persistence.Column;
  3. import javax.persistence.Entity;
  4. import javax.persistence.Id;

  5. import org.hibernate.annotations.Proxy;

  6. @Entity
  7. @Proxy(lazy = false)
  8. public class Table1 {
  9.     
  10.     @Id
  11.     @Column(name="id")
  12.     private int id;
  13.     @Column(name="wordlist")
  14.     private String wordList;
  15.     @Column(name="labellist")
  16.     private String labelList;
  17.     @Column(name="wordsize")
  18.     private int wordSize;
  19.     @Column(name="labelsize")
  20.     private int labelSize;
  21.     @Column(name="pi")
  22.     private String pi;
  23.     
  24.     public int getId() {
  25.         return id;
  26.     }
  27.     public void setId(int id) {
  28.         this.id = id;
  29.     }
  30.     public String getWordList() {
  31.         return wordList;
  32.     }
  33.     public void setWordList(String wordList) {
  34.         this.wordList = wordList;
  35.     }
  36.     public String getLabelList() {
  37.         return labelList;
  38.     }
  39.     public void setLabelList(String labelList) {
  40.         this.labelList = labelList;
  41.     }
  42.     public int getWordSize() {
  43.         return wordSize;
  44.     }
  45.     public void setWordSize(int wordSize) {
  46.         this.wordSize = wordSize;
  47.     }
  48.     public int getLabelSize() {
  49.         return labelSize;
  50.     }
  51.     public void setLabelSize(int labelSize) {
  52.         this.labelSize = labelSize;
  53.     }
  54.     public String getPi() {
  55.         return pi;
  56.     }
  57.     public void setPi(String pi) {
  58.         this.pi = pi;
  59.     }
  60.     @Override
  61.     public String toString() {
  62.         return "Table1 [id=" + id + ", wordList=" + wordList + ", labelList=" + labelList + ", wordSize=" + wordSize
  63.                 + ", labelSize=" + labelSize + ", pi=" + pi + "]";
  64.     }

  65. }

Table2、Table3同理基于数据库映射关系构建

Table1Respository.java

点击(此处)折叠或打开

  1. package com.xlc.hmm;

  2. import org.springframework.data.jpa.repository.JpaRepository;
  3.                                                                  //此处可以指定其他类型,因需而定
  4. public interface Table1Respository extends JpaRepository<Table1, Integer>{}

Table2Respository.java、Table2Respository.java同理

HmmController.java

点击(此处)折叠或打开

  1. package com.xlc.hmm;

  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;

  6. import javax.annotation.PostConstruct;

  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import org.springframework.web.bind.annotation.RestController;

  13. //@Component
  14. //@Order(value=1)
  15. //class StartUpRunner implements CommandLineRunner {
  16. // @Override
  17. // public void run(String... args) throws Exception {
  18. //// new ParamFromSql();
  19. // System.out.println("SUCCESS");
  20. // }
  21. //}

  22. @Component
  23. class ParamFromSql {
  24.     
  25.     @Autowired
  26.     private Table1Respository table1Respository;
  27.     
  28.     @Autowired
  29.     private Table2Respository table2Respository;
  30.     
  31.     @Autowired
  32.     private Table3Respository table3Respository;
  33.     
  34.     public static ParamFromSql paramFromSql;
  35.     
  36.     static List<String> wordlist;
  37.     static List<String> labellist;
  38.     static double[] pi;
  39.     static double[][] A;
  40.     static double[][] B;

  41.     @PostConstruct
  42.     public void init() {
  43.         
  44.         paramFromSql = this;
  45.         paramFromSql.table1Respository = this.table1Respository;
  46.         paramFromSql.table2Respository = this.table2Respository;
  47.         paramFromSql.table3Respository = this.table3Respository;
  48.         
  49.         wordlist = new ArrayList<String>();
  50.         labellist = new ArrayList<String>();
  51. // getParamFromMysql();
  52. // Table1 table1 = paramFromSql.table1Respository.getOne(1);
  53. // paramFromSql.table1Respository.getOne(1);
  54. // System.out.println(paramFromSql.table1Respository.getOne(1));
  55.         Table1 table1 = paramFromSql.table1Respository.getOne(1);
  56.         System.out.println(table1.getLabelList());
  57.         labellist = Arrays.asList(table1.getLabelList().split(" "));
  58.         wordlist = Arrays.asList(table1.getWordList().split(" "));
  59.         String[] piStr = table1.getPi().split(" ");
  60.         int labelSize= table1.getLabelSize();
  61.         int wordSize = table1.getWordSize();
  62.         pi = new double[labelSize];
  63.         A = new double[labelSize][labelSize];
  64.         B = new double[labelSize][wordSize];
  65.         
  66.         int j = 1;
  67.         for (int i = 0; i < labelSize; ++i) {
  68.             pi[i] = Double.valueOf(piStr[i]);
  69.             
  70.             String[] rowAStrs = paramFromSql.table2Respository.getOne(j).getRowA().split(" ");
  71.             for(int k = 0; k < labelSize; ++k) {
  72.                 A[i][k] = Double.valueOf(rowAStrs[k]);
  73.             }
  74.             
  75.             String[] rowBStrs = paramFromSql.table3Respository.getOne(j).getRowB().split(" ");
  76.             for(int k = 0; k < wordSize; ++k) {
  77.                 B[i][k] = Double.valueOf(rowBStrs[k]);
  78.             }
  79.             
  80.             ++j;
  81.             
  82.         }
  83.         
  84.         System.out.println("SUCCESS");
  85.     }
  86.     
  87. }

  88. class Test{
  89.     public void test() {
  90.         System.out.println("-------------------------");
  91.         System.out.println(ParamFromSql.A[0][0]);
  92.         System.out.println(ParamFromSql.B[0][0]);
  93.         System.out.println("-------------------------");
  94.     }
  95. }


  96. class SetLabel{

  97.     public String setLabel(String strInput) {
  98.         String result = "";
  99.         try {
  100.             int[] labelindex = viterbi(strInput, ParamFromSql.pi, ParamFromSql.A, ParamFromSql.B);
  101.             String[] strwords = strInput.split(" ");
  102.             for (int i = 0; i < labelindex.length; i++) {
  103.                 result += strwords[i] + "/" + ParamFromSql.labellist.get(labelindex[i]) + " ";
  104.                 
  105.             }
  106.         }catch(Exception e) {
  107.             e.printStackTrace();
  108.         }
  109.         return result;
  110.     }
  111.     
  112.     // viterbi
  113.     public int[] viterbi(String string, double[] pi, double[][] A, double[][] B) throws IOException{
  114.         
  115.         
  116.         String[] words = string.split(" ");
  117.         double[][] delta = new double[words.length][pi.length];
  118.         int[][] way = new int[words.length][pi.length];
  119.         int[] labelindex = new int[words.length];
  120.         //System.out.println(words[0]);
  121.         for (int i = 0; i < pi.length; i++) {
  122.             delta[0][i] = pi[i] * B[i][ParamFromSql.wordlist.indexOf(words[0])]; //////////////////////////////////////////////
  123.         }
  124.         for (int t = 1; t < words.length; t++) {
  125.             //System.out.println(words[t]);
  126.             for (int i = 0; i < pi.length; i++) {
  127.                 for (int j = 0; j < pi.length; j++) {
  128.                     ////////
  129.                     //System.out.println("t:" +t + "i:" + i + "j:" + j + "wordlist.indexOf(words[t]):"
  130.                         // + wordlist.indexOf(words[t]));
  131.                     if(delta[t][i] < delta[t-1][j] * A[j][i] * B[i][ParamFromSql.wordlist.indexOf(words[t])]) {
  132.                         delta[t][i] = delta[t-1][j] * A[j][i] * B[i][ParamFromSql.wordlist.indexOf(words[t])];
  133.                         way[t][i] = j;
  134.                     }
  135.                 }
  136.             }
  137.         }
  138.         double max = delta[words.length - 1][0];
  139.         labelindex[words.length - 1] = 0;
  140.         for (int i = 0; i < pi.length; i++) {
  141.             if (delta[words.length - 1][i] > max) {
  142.                 max = delta[words.length - 1][i];
  143.                 labelindex[words.length - 1] = i;
  144.             }
  145.         }
  146.         for (int t = words.length - 2; t >= 0; t--) {
  147.             labelindex[t] = way[t + 1][labelindex[t + 1]];
  148.         }
  149.         //System.out.println(Arrays.toString(labelindex));
  150.         return labelindex;
  151.     }
  152.     
  153. }

  154. @RestController
  155. public class HmmController {
  156.         
  157.     
  158.     public String justDoIt(String str) {
  159.         String resultStr = null;
  160.         try {
  161.             resultStr = new SetLabel().setLabel(str);
  162.         }catch(Exception e) {
  163.             e.printStackTrace();
  164.         }
  165.         
  166.         return resultStr;
  167.     }

  168.     @PostMapping("/hmm")
  169.     public String hmmDemo(@RequestParam(value = "str", required = false, defaultValue = "0") String testStr) {// return testStr;
  170.         if(testStr.equals("0")) {
  171.             return "are you kidding me?";
  172.         }else {
  173.             return justDoIt(testStr);
  174.         }
  175.         
  176.     }
  177.     
  178.     @GetMapping("/test")
  179.     public String test() {
  180. // new Test().test();
  181.         return "do you like me?";
  182.     }

  183. }
DemohmmApplication.java

点击(此处)折叠或打开

  1. package com.xlc;

  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. @SpringBootApplication
  5. public class DemohmmApplication {

  6.     public static void main(String... args) {
  7.                 
  8.         SpringApplication.run(DemohmmApplication.class, args);
  9.     }
  10. }
现在右键DemohmmApplication运行,启动项目
看见这个是不是很激动

点击(此处)折叠或打开

  1. . ____ _ __ _ _
  2.  /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  4.  \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  5.   

 可以使用Postman进行测试。

阅读(755) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~