当前位置:   article > 正文

Java自然语言处理实现基于酒店评价的词云可视化(前端与后端交互使用)_java词云分析

java词云分析

目录

一、数据源

二、后端API

三、后端测试

四、前端API

五、可视化展示

1.正面词汇好评

 2.负面词汇差评

 六、好了今天就到这里,下下次再更新哦!


一、数据源

 

二、后端API

  1. package com.example.springbootdemo.web;
  2. import com.hankcs.hanlp.HanLP;
  3. import com.hankcs.hanlp.seg.common.Term;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.io.*;
  7. import java.util.ArrayList;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. import java.util.function.Predicate;
  11. import java.util.stream.Collectors;
  12. @RestController
  13. public class WordCloud {
  14. @GetMapping("/wordcloud")
  15. /**
  16. * 获取词云
  17. * @param type 好评,坏评
  18. * @return 分词列表
  19. */
  20. public List<Word> wordcloud(String type) {
  21. String wordcloudPath = "F:\\Java-data\\ChnSentiCorp情感分析酒店评论\\wordcloud\\";
  22. File wordcloudPathFile = new File(wordcloudPath);
  23. if (!wordcloudPathFile.exists()) {
  24. wordcloudPathFile.mkdirs();
  25. }
  26. File wordcloudFile = new File(wordcloudPath, type);
  27. if (wordcloudFile.exists()) {
  28. ObjectInputStream objectInputStream = null;
  29. try {
  30. objectInputStream = new ObjectInputStream(new FileInputStream(wordcloudFile));
  31. LinkedList<Word> words = (LinkedList<Word>) objectInputStream.readObject();
  32. if (words != null) {
  33. return words;
  34. }
  35. } catch (IOException | ClassNotFoundException e) {
  36. e.printStackTrace();
  37. } finally {
  38. try {
  39. objectInputStream.close();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }
  45. if (type.equals("")) {
  46. return new ArrayList<>(0);
  47. }
  48. String path = "F:\\Java-data\\ChnSentiCorp情感分析酒店评论\\" + type;
  49. File dir = new File(path);
  50. File[] files = dir.listFiles(File::isFile);
  51. StringBuilder text = new StringBuilder();
  52. for (File file : files) {
  53. try {
  54. BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
  55. String line = bufferedReader.readLine();
  56. line = line.replaceAll("[~!@#$%^&*()_+\\-*/.,\\.?/|\":;\\=]", "");
  57. text.append(line);
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. List<Term> segment = HanLP.segment(text.toString());
  63. List<Term> segmentWord = segment.stream().filter(new Predicate<Term>() {
  64. @Override
  65. public boolean test(Term term) {
  66. return term.nature.toString().equalsIgnoreCase("a");
  67. }
  68. }).collect(Collectors.toList());
  69. List<String> segmentWordString = segmentWord.stream().map(term -> term.word).collect(Collectors.toList());
  70. try {
  71. List<String> stopWords = StopWorrUtil.loadStopWord("F:\\Java-data\\停留词.txt");
  72. // 去停留词
  73. segmentWordString.removeAll(stopWords);
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. // 统计分词次数
  78. LinkedList<Word> words = new LinkedList<>();
  79. for (String word : segmentWordString) {
  80. // 第一次循环
  81. if (words.size() == 0) {
  82. words.add(new Word(word, 1));
  83. }
  84. // 第2 - n 次循环
  85. boolean exists = false;
  86. Word word1 = null;
  87. for (Word value : words) {
  88. word1 = value;
  89. if (word1.getName().equals(word)) {
  90. exists = true;
  91. break;
  92. }
  93. }
  94. if (exists) {
  95. word1.setValue(word1.getValue() + 1);
  96. } else {
  97. words.add(new Word(word, 1));
  98. }
  99. }
  100. try {
  101. ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(wordcloudFile));
  102. objectOutputStream.writeObject(words);
  103. objectOutputStream.flush();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. return words;
  108. }
  109. }

三、后端测试

四、前端API

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>词云</title>
  6. </head>
  7. <body>
  8. <div class="btn-group">
  9. <button onclick="loadAndShowWordCloud('正面')" style="text-align: center; color: coral">正面</button>
  10. <button onclick="loadAndShowWordCloud('负面')" style="text-align: center; color: coral">负面</button>
  11. </div>
  12. <div id="main" style="width: 500px; height: 500px"></div>
  13. </body>
  14. </html>
  15. <script src="./js/echarts.min.js"></script>
  16. <script src="./js/echarts-wordcloud.min.js"></script>
  17. <script>
  18. function loadAndShowWordCloud(type = '正面') {
  19. var request = new XMLHttpRequest()
  20. request.open('GET', '/wordcloud?type=' + type)
  21. request.send()
  22. request.onreadystatechange = function () {
  23. if (request.readyState === 4) {
  24. var chart = echarts.init(document.getElementById('main'));
  25. chart.setOption({
  26. series:
  27. [{
  28. type: 'wordCloud',
  29. shape: 'star',
  30. /**
  31. * // 词云的形状,可选值有
  32. * cardioid心形,diamond菱形,square正方形,
  33. * triangle-forward指向右边的三角形,triangle-upright正三角形
  34. * triangle三角形,pentagon五角形,star五角星形,
  35. */
  36. keepAspect: false,
  37. left: 'center',
  38. top: 'center',
  39. width: '100%',
  40. height: '100%',
  41. rotationStep: 90,
  42. gridSize: 8,
  43. drawOutOfBound: false,
  44. shrinkToFit: false,
  45. layoutAnimation: true,
  46. textStyle: {
  47. fontFamily: 'sans-serif',
  48. fontWeight: 'bold',
  49. // Color can be a callback function or a color string
  50. color: function () {
  51. // Random color
  52. return 'rgb(' + [
  53. Math.round(Math.random() * 160),
  54. Math.round(Math.random() * 160),
  55. Math.round(Math.random() * 160)
  56. ].join(',') + ')';
  57. }
  58. },
  59. emphasis: {
  60. focus: 'self',
  61. textStyle: {
  62. textShadowBlur: 10,
  63. textShadowColor: '#333'
  64. }
  65. },
  66. data: JSON.parse(request.responseText)
  67. }]
  68. });
  69. }
  70. }
  71. }
  72. loadAndShowWordCloud()
  73. </script>

五、可视化展示

1.正面词汇好评

 2.负面词汇差评

 六、好了今天就到这里,下下次再更新哦!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/772594
推荐阅读
相关标签
  

闽ICP备14008679号