Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
HowToKB/src/kb/howtokb/tools/NormalizationText.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
96 lines (83 sloc)
2.55 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kb.howtokb.tools; | |
import java.io.IOException; | |
import java.sql.SQLException; | |
import java.text.DecimalFormat; | |
import java.util.ArrayList; | |
import java.util.List; | |
import kb.howtokb.taskframe.extractor.HelperForOpenIE4Activities; | |
public class NormalizationText { | |
public static String normString(String s){ | |
if (s == null) s = ""; | |
return s.replaceAll("[^0-9a-zA-Z ,-.\\_%/+']+", " ") | |
.replaceAll("'", "") | |
.replaceAll("\\s+", " ") | |
.replaceAll(" ,", ",") | |
.trim().toLowerCase(); | |
} | |
public static ArrayList<String> normString(ArrayList<String> s){ | |
ArrayList<String> result = new ArrayList<>(); | |
for (int i=0; i<s.size(); i++){ | |
result.add(normString(s.get(i))); | |
} | |
return result; | |
} | |
public static List<String> normList(List<String> l) throws SQLException, IOException{ | |
List<String> result = new ArrayList<>(); | |
for (int i=0; i<l.size(); i++){ | |
String headword = HelperForOpenIE4Activities.headWord(l.get(i)); | |
headword = headword.contains(" ")?headword.substring(headword.lastIndexOf(" ") + 1):headword; | |
headword = headword.replaceAll("[,.]", ""); | |
if (!HelperForOpenIE4Activities.STOPWORDS.contains(headword)) | |
result.add(headword); | |
} | |
return result; | |
} | |
public static List<String> removeStopwordInString(String s) throws SQLException, IOException{ | |
List<String> result = new ArrayList<>(); | |
String[] l = s.split(" "); | |
for (int i=0; i<l.length; i++){ | |
if (!HelperForOpenIE4Activities.STOPWORDS.contains(l[i])) | |
result.add(l[i]); | |
} | |
return result; | |
} | |
public static String listToPhrase(List<String> list){ | |
String res = ""; | |
if (list.size() > 0 && list.get(0).length() > 0){ | |
for (String t:list){ | |
res += t + "_"; | |
} | |
res = res.substring(0, res.lastIndexOf("_") - 1); | |
} | |
return res; | |
} | |
public static String arrayToString(double[] vector){ | |
String res = ""; | |
if (vector.length == 1){ | |
return Double.toString(vector[0]); | |
}else if (vector.length > 1){ | |
for (int i=0; i<vector.length - 1; i++) | |
res += Double.toString(vector[i]) + " "; | |
res += Double.toString(vector[vector.length - 1]); | |
} | |
return res; | |
} | |
public static List<Integer> stringToListInt(String s){ | |
List<Integer> res = new ArrayList<>(); | |
if (!s.contains(";")) | |
res.add(Integer.parseInt(s)); | |
else{ | |
String [] tmp = s.split(";"); | |
for (String ss: tmp) | |
res.add(Integer.parseInt(ss)); | |
} | |
return res; | |
} | |
private static DecimalFormat decim; | |
/** 32.535534534534; after formatting = 32.536 */ | |
public static String format(double x) { | |
if (decim == null) | |
decim = new DecimalFormat("#.###"); | |
return decim.format(x); | |
} | |
} |