Advertisement
Guest User

Comment

a guest
Apr 23rd, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. package Structures;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.sql.Types;
  11. import java.util.ArrayList;
  12.  
  13. public class Comment {
  14. public String type = "";
  15. public String content = "";
  16. public String className = "";
  17. public String methodName = "";
  18. public int idMethod = -1;
  19. public int idClass = -1;
  20.  
  21. public void insert(Connection connection) {
  22.  
  23. try {
  24. if (methodName != null) {
  25. String queryMethod = "SELECT idMethod FROM method WHERE (Nom='" + methodName + "') LIMIT 1";
  26. Statement Stmt;
  27. Stmt = connection.createStatement();
  28. ResultSet rs = Stmt.executeQuery(queryMethod);
  29. if (rs.next() != false)
  30. idMethod = rs.getInt("idMethod");
  31. }
  32. String queryClass = "SELECT idClass FROM method WHERE (Nom='" + className + "') LIMIT 1";
  33. Statement Stmt;
  34. Stmt = connection.createStatement();
  35. ResultSet rs = Stmt.executeQuery(queryClass);
  36. if (rs.next() == false)
  37. System.err.println("Class Not Found for Comment");
  38. else {
  39. idClass = rs.getInt("idClass");
  40. String query = "INSERT INTO comment (Type, Content, idClass, idMethod) VALUES (?,?,?,?)";
  41. PreparedStatement preparedStmt;
  42. preparedStmt = connection.prepareStatement(query);
  43. preparedStmt.setString(1, this.type);
  44. preparedStmt.setString(2, this.content);
  45. preparedStmt.setInt(3, idClass);
  46. if (idMethod == -1)
  47. preparedStmt.setNull(4, Types.INTEGER);
  48. else
  49. preparedStmt.setInt(4, idMethod);
  50. preparedStmt.execute();
  51. }
  52.  
  53. } catch (SQLException e) {
  54. System.err.println(e.getMessage());
  55. ;
  56. }
  57. }
  58.  
  59. public static String extractContent(Connection connection) {
  60. String result = "";
  61. String query = "Select Content FROM comment";
  62. Statement Stmt;
  63. try {
  64. Stmt = connection.createStatement();
  65. ResultSet rs = Stmt.executeQuery(query);
  66. while (rs.next()) {
  67. result += " " + rs.getString("Content");
  68. }
  69. } catch (SQLException e) {
  70. System.err.println(e.getMessage());
  71. }
  72. result = result.replace(" ", " ");
  73. result = result.replace("''", "'");
  74. //result = result.replace(".", "");
  75.  
  76. result = stopword(result);
  77.  
  78. return result;
  79.  
  80. }
  81.  
  82. private static String stopword(String s){
  83.  
  84. String result = "";
  85. int k=0;
  86. ArrayList<String> wordsList = new ArrayList<String>();
  87. String sCurrentLine;
  88. String[] stopwords = new String[2000];
  89. try{
  90. FileReader fr=new FileReader("stopwords.txt");
  91. BufferedReader br= new BufferedReader(fr);
  92. while ((sCurrentLine = br.readLine()) != null){
  93. stopwords[k]=sCurrentLine;
  94. k++;
  95. }
  96.  
  97. StringBuilder builder = new StringBuilder(s);
  98. String[] words = builder.toString().split("\\s");
  99.  
  100.  
  101. for (String word : words){
  102. wordsList.add(word);
  103. }
  104. for(int i = 0; i < wordsList.size(); i++){
  105. for(int j = 0; j < k; j++){
  106. if(stopwords[j].contains(wordsList.get(i).toLowerCase())){
  107. wordsList.remove(i);
  108. i = i- 1;
  109.  
  110. break;
  111. }
  112. }
  113. }
  114.  
  115. result = wordsList.get(0);
  116. for (int i = 1; i<wordsList.size(); i++){
  117. result = result + " " + wordsList.get(i);
  118. }
  119. }catch(Exception ex){
  120. System.out.println(ex);
  121. }
  122.  
  123. return result;
  124. }
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement