Java LinkedHashSet无法从ArrayList中删除重复的句子
我正在构建一个android/Java程序,它从文本文件中读取并将文本文件中的每个句子存储在数组列表中。然后检查句子中每个单词的出现情况,打印出所有包含重复单词的句子 这是我用来打印最终结果的代码:Java LinkedHashSet无法从ArrayList中删除重复的句子,java,android,regex,linkedhashset,Java,Android,Regex,Linkedhashset,我正在构建一个android/Java程序,它从文本文件中读取并将文本文件中的每个句子存储在数组列表中。然后检查句子中每个单词的出现情况,打印出所有包含重复单词的句子 这是我用来打印最终结果的代码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text4); text = (TextV
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text4);
text = (TextView)findViewById(R.id.info2);
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("input3.txt")));
String line;
List<String> sentences = new ArrayList<String>();
for ( String line2; (line2 = reader.readLine()) != null;) {
for (String sentence : line2.split("(?<=[.?!\t])")) {
sentence = sentence.trim();
if (! sentence.isEmpty()) {
sentences.add(sentence);
}
}
String[] keys = line2.split(" ");
String[] uniqueKeys;
int count = 0;
uniqueKeys = getUniqueKeys(keys);
for(String key: uniqueKeys)
{
if(null == key)
{
break;
}
for(String s : keys)
{
if(key.equals(s))
{
count++;
}
}
if(key.equals("a") || key.equals("the")|| key.equals("is")|| key.equals("of")|| key.equals("and")|| key.equals("The") || key.equals("some") || key.equals("on") || key.equals("during") || key.equals("to") || key.equals("since") || key.equals("in") || key.equals("by") || key.equals("for") || key.equals("were") ||key.equals("--") || key.equals("in") || key.equals("as") || key.equals("that") || key.equals("may") || key.equals("can") || key.equals("without") || key.equals("You")){
count = 0;
}
if(count >1 ){
MyKey = key;
Pattern word = Pattern.compile("\\b"+key+"\\b", Pattern.CASE_INSENSITIVE);
//sentences is the arrayList of sentences in this program
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
for (String sentence : sentences) {
//checks the occurance of keyword within each sentence
if (word.matcher(sentence).find()) {
lhs.add(sentence);
}
}
for (String sentence2 : lhs) {
text.append(sentence2);
}
}
count=0;
}
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.text4);
text=(TextView)findViewById(R.id.info2);
BufferedReader reader=null;
试一试{
读卡器=新的BufferedReader(
新的InputStreamReader(getAssets().open(“input3.txt”);
弦线;
列出句子=新建ArrayList();
对于(字符串line2;(line2=reader.readLine())!=null;){
对于(字符串句子:line2.split((?),每次该单词与您正在创建新LinkedHashSet实例的句子匹配时
试试这个:
//sentences is the arrayList of sentences in this program
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
for (String sentence : sentences) {
//checks the occurance of keyword within each sentence
if (word.matcher(sentence).find()) {
lhs.add(sentence);
}
}
//displays the final result on the output window
String text = "";
for (String sentence2 : lhs) {
text.append(sentence2);
}
//句子是此程序中句子的数组列表
LinkedHashSet lhs=新LinkedHashSet();
for(字符串句子:句子){
//检查每个句子中关键字的出现情况
if(word.matcher(句子).find()){
lhs.添加(句子);
}
}
//在输出窗口上显示最终结果
字符串文本=”;
for(字符串语句2:lhs){
文本。追加(第2句);
}
为什么要添加字符串text=“”;text是我的TextView的名称,以避免出现空指针异常。如果您之前已经声明了,请删除我的一个。如果它对您有效,请向上投票!(:您现在可以检查一下吗,我已经添加了我程序中的所有代码。这是一个生成错误吗?运行时错误?发布您的疑问。当我运行上面的程序时,它会显示包含“key”的句子,正如我在问题中解释的那样。但是,上面的示例中的一些句子包含2“key”“单词。因此,程序将特定的句子识别为两个独立的句子。这就是为什么我尝试使用LinkedHashSet来防止重复的句子显示。