[关闭]
@zhou-si 2016-08-29T01:38:05.000000Z 字数 3909 阅读 1882

java对指定文件中汉字数字字母进行切分

java切词


初衷

最近项目组有个小需求:编辑组那边收集了一些电商产品信息,需要对其进行用指定分隔符连接,比如:"三星GALAXY A4 移动4G" --> "三星@GALAXYA@4@移动@4@G"


代码实现(工具类)

  1. package utils_split;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. /**
  5. * 创建时间:20160828
  6. * 工具类
  7. * @author 圣斗士宙斯
  8. *
  9. */
  10. public class Utils {
  11. // 判断字符串是否仅为数字
  12. public static boolean isNumeric(char cha) {
  13. if (Character.isDigit(cha)) {
  14. return true;
  15. }else {
  16. return false;
  17. }
  18. }
  19. // 判断字符是否是字母
  20. public static boolean word(char cha) {
  21. int i = (int) cha;
  22. if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122)) {
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. }
  28. //判断字符是否是中文
  29. public static boolean chinese(char str){
  30. String regEx = "[\\u4e00-\\u9fa5]";
  31. Pattern p = Pattern.compile(regEx);
  32. Matcher m = p.matcher(String.valueOf(str));
  33. if (m.find()) {
  34. return true;
  35. }
  36. else {
  37. return false;
  38. }
  39. }
  40. }

代码实现(实现类)

  1. package split;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.UnsupportedEncodingException;
  10. import utils_split.Utils;
  11. /**
  12. * 创建时间:20160828
  13. * 指定文件绝对路径 把文件中字母和字母链接的不替换,
  14. * 汉字和数字连接的中间要添加指定符号
  15. * 字母和数字连接的中间要添加指定符号
  16. * 汉字和字母连接的中间要添加指定符号
  17. * 数字和数字,字母和字母,汉字和汉字连接的中间不需要加指定符号
  18. * 需求源于挖掘组切词
  19. * @author 圣斗士宙斯
  20. *
  21. */
  22. public class JavaSplitFile {
  23. static String filePath = "";
  24. static String splitStr = "@";//默认为@
  25. public static void main(String[] args) {
  26. filePath = args[0];
  27. splitStr = args[1];
  28. long startTime = System.currentTimeMillis();
  29. File file = new File(filePath);
  30. File newFile = new File(filePath + "_new");
  31. if (newFile.exists()) {
  32. newFile.delete();
  33. } else {
  34. try {
  35. newFile.createNewFile();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. FileInputStream fis = null;
  41. InputStreamReader read = null;
  42. String lineText = null;
  43. // 判断文件是存在
  44. if (file.exists() && file.isFile()) {
  45. try {
  46. fis = new FileInputStream(file);
  47. } catch (FileNotFoundException e) {
  48. e.printStackTrace();
  49. }
  50. try {
  51. read = new InputStreamReader(fis, "gbk");
  52. } catch (UnsupportedEncodingException e1) {
  53. e1.printStackTrace();
  54. }
  55. BufferedReader br = new BufferedReader(read);
  56. FileWriter fw = null;
  57. String outPut = "";
  58. StringBuffer sBuffer = new StringBuffer();
  59. try {
  60. fw = new FileWriter(newFile, true);
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. // 逐行读取,判断,替换
  65. try {
  66. while ((lineText = br.readLine()) != null) {
  67. // 过滤掉中文括号后面的 (全网通) 和url
  68. lineText = lineText.split("(")[0].split("http://")[0]
  69. .replaceAll("\\s*", "");
  70. int i = lineText.toCharArray().length;
  71. String firstChar = String.valueOf(lineText.charAt(0));// 当前行的第一个字符
  72. sBuffer.append(firstChar);// 刚开始遍历一行时就追加第一个字符
  73. for (int n = 1; n <= i - 1; n++) {
  74. char nowChar = lineText.charAt(n);// 当前字符
  75. char suffStr = lineText.charAt(n - 1);// 前一个字符
  76. // 如果当前字符是数字========================
  77. if (Utils.isNumeric(nowChar)) {
  78. // 如果前一个字符是字母
  79. if (Utils.word(suffStr)) {
  80. sBuffer.append(splitStr + String.valueOf(nowChar));
  81. }
  82. // 如果前一个字符是汉字
  83. else if (Utils.chinese(suffStr)) {
  84. sBuffer.append(splitStr + String.valueOf(nowChar));
  85. }
  86. // 如果前一字符是数字
  87. else if (Utils.isNumeric(nowChar)) {
  88. sBuffer.append(String.valueOf(nowChar));
  89. }
  90. // 如果当前字符是字母=========================
  91. } else if (Utils.word(nowChar)) {
  92. // 如果前一个字符是字母
  93. if (Utils.word(suffStr)) {
  94. sBuffer.append(String.valueOf(nowChar));
  95. }
  96. // 如果前一个字符是汉字
  97. else if (Utils.chinese(suffStr)) {
  98. sBuffer.append(splitStr + String.valueOf(nowChar));
  99. }
  100. // 如果前一字符是数字
  101. else if (Utils.isNumeric(suffStr)) {
  102. sBuffer.append(splitStr + String.valueOf(nowChar));
  103. }
  104. // 如果当前字符是汉字=========================
  105. } else if (Utils.chinese(nowChar)) {
  106. // 如果前一个字符是字母
  107. if (Utils.word(suffStr)) {
  108. sBuffer.append(splitStr + String.valueOf(nowChar));
  109. }
  110. // 如果前一个字符是汉字
  111. else if (Utils.chinese(suffStr)) {
  112. sBuffer.append(String.valueOf(nowChar));
  113. }
  114. // 如果前一字符是数字
  115. else if (Utils.isNumeric(suffStr)) {
  116. sBuffer.append(splitStr + String.valueOf(nowChar));
  117. }
  118. }
  119. }
  120. sBuffer.append("\n");
  121. outPut = sBuffer.toString();
  122. }
  123. fw.write(outPut);
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. }
  127. try {
  128. read.close();
  129. fw.close();
  130. } catch (IOException e) {
  131. e.printStackTrace();
  132. }
  133. } else {
  134. System.out.println("第一个参数不是一个文件的绝对路径!!!");
  135. }
  136. long endTime = System.currentTimeMillis();
  137. System.out.println("输入文件:" + filePath);
  138. System.out.println("输出文件:"+ newFile);
  139. System.out.println("耗时:" + (endTime - startTime) / 1000.00 + "秒");
  140. }
  141. }

结果

输入文件:C:/Users/zhousi/Desktop/mobile_param_url.txt
输出文件:C:\Users\zhousi\Desktop\mobile_param_url.txt_new
耗时:0.261秒
(事例:)
三星@F@308
三星@SGH@208
三星@SCH@609
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注