首页 > 解决方案 > 从存在于第二个字符串列表中的字符串列表中删除所有项目

问题描述

我将对一系列 PDF 执行一些基本的 NLP,我想在将单词列表输出到 CSV 之前从 PDF 中删除所谓的“停用词”。我创建了一个停用词以及 PDF 中的单词的 ArrayList,并尝试使用 removeAll。我收到一个无用的错误 -Exception in thread main在 removeAll 行上。

public class ReadingText {

public static void main(String args[]) throws IOException {
  String stopList = "i,me,my,...";
  
  String[] stopList1 = stopList.split(",");
  List<String> stopList2 = Arrays.asList(stopList1);
  
  File file = new File("C:/Users/Documents/Walmart_2020_Annual_Report.pdf");
  PDDocument document = PDDocument.load(file);
  PDFTextStripper pdfStripper = new PDFTextStripper();
  String text = pdfStripper.getText(document);
  text = text.replace(",", "");
  text = text.trim().replaceAll(" +", " ");
  text = text.replace(" ", ",");
  
  String[] text2 = text.split(",");
  
  List<String> wordList = Arrays.asList(text2);
  
  wordList.removeAll(stopList2);
  
  System.out.println(wordList.size());

} }

标签: java

解决方案


Arrays.asList()返回一个不可变列表,如果您尝试更改其大小,则会引发异常。

要从中创建一个可变列表:

wordList = new ArrayList<>(wordList);

推荐阅读