class WikipediaSentenceSource extends SentenceSource {

  private static final boolean ONLY_ARTICLES = false;
  private static final String ARTICLE_NAMESPACE = "0";

  private final SwebleWikipediaTextFilter textFilter = new SwebleWikipediaTextFilter();
  private final XMLEventReader reader;
  private final Tokenizer sentenceTokenizer;
  private final List<WikipediaSentence> sentences;
  private final Language language;

  private int articleCount = 0;
  private int namespaceSkipCount = 0;
  private int redirectSkipCount = 0;

  WikipediaSentenceSource(InputStream xmlInput, Language language) {
    this(xmlInput, language, null);
  }

  /** @since 3.0 */
  WikipediaSentenceSource(InputStream xmlInput, Language language, Pattern filter) {
    super(language, filter);
    textFilter.enableMapping(false);  // improves performance
    try {
      System.setProperty("jdk.xml.totalEntitySizeLimit", String.valueOf(Integer.MAX_VALUE));  // see https://github.com/dbpedia/extraction-framework/issues/487
      XMLInputFactory factory = XMLInputFactory.newInstance();
      reader = factory.createXMLEventReader(xmlInput);
      sentenceTokenizer = language.getSentenceTokenizer();
      sentences = new ArrayList<>();
      this.language = language;
    } catch (XMLStreamException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public boolean hasNext() {
    try {
      fillSentences();
    } catch (XMLStreamException e) {
      throw new RuntimeException(e);
    }
    return sentences.size() > 0;
  }

  @Override
  public Sentence next() {
    try {
      fillSentences();
      if (sentences.isEmpty()) {
        throw new NoSuchElementException();
      }
      WikipediaSentence wikiSentence = sentences.remove(0);
      String url = "http://" + language.getShortCode() + ".wikipedia.org/wiki/" + wikiSentence.title;
      return new Sentence(wikiSentence.sentence, getSource(), wikiSentence.title, url, wikiSentence.articleCount);
    } catch (XMLStreamException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public String getSource() {
    return "wikipedia";
  }

  private void fillSentences() throws XMLStreamException {
    String title = null;
    String namespace = null;
    while (sentences.isEmpty() && reader.hasNext()) {
      XMLEvent event = reader.nextEvent();
      if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
        String elementName = event.asStartElement().getName().getLocalPart();
        switch (elementName) {
          case "title":
            event = reader.nextEvent();
            title = event.asCharacters().getData();
            articleCount++;
            if (articleCount % 100 == 0) {
              System.out.println("Article: " + articleCount);
            }
            break;
          case "ns":
            event = reader.nextEvent();
            namespace = event.asCharacters().getData();
            break;
          case "text":
            handleTextElement(namespace, title, articleCount);
            break;
        }
      }
    }
  }

  private void handleTextElement(String namespace, String title, int articleCount) throws XMLStreamException {<FILL_FUNCTION_BODY>}

  private static class WikipediaSentence {
    final String sentence;
    final String title;
    final int articleCount;
    WikipediaSentence(String sentence, String title, int articleCount) {
      this.sentence = sentence;
      this.title = title;
      this.articleCount = articleCount;
    }
  }
}

    if (ONLY_ARTICLES && !ARTICLE_NAMESPACE.equals(namespace)) {
      namespaceSkipCount++;
      return;
    }
    //System.out.println(articleCount + " (nsSkip:" + namespaceSkipCount + ", redirectSkip:" + redirectSkipCount + "). " + title);
    XMLEvent event = reader.nextEvent();
    StringBuilder sb = new StringBuilder();
    while (event.isCharacters()) {
      sb.append(event.asCharacters().getData());
      event = reader.nextEvent();
    }
    try {
      if (sb.toString().trim().toLowerCase().startsWith("#redirect")) {
        redirectSkipCount++;
        return;
      }
      String textToCheck = textFilter.filter(sb.toString()).getPlainText();
      for (String sentence : sentenceTokenizer.tokenize(textToCheck)) {
        if (acceptSentence(sentence)) {
          // Create an artificial ID - as we treat each sentence as a single document
          // in e.g. the nightly checks, this helps with detection of whether a match
          // is new or a duplicate:
          String titleWithId = title + "/" + sentence.hashCode();
          sentences.add(new WikipediaSentence(sentence, titleWithId, articleCount));
        }
      }
    } catch (Exception e) {
      System.err.println("Could not extract text, skipping document: " + e + ", full stacktrace follows:");
      e.printStackTrace();
    }
  
<methods>public abstract java.lang.String getSource() ,public abstract boolean hasNext() ,public abstract org.languagetool.dev.dumpcheck.Sentence next() ,public void remove() ,public java.lang.String toString() <variables>static final int MAX_SENTENCE_LENGTH,static final int MIN_SENTENCE_LENGTH,static final int MIN_SENTENCE_TOKEN_COUNT,private final non-sealed java.util.regex.Pattern acceptPattern,private int ignoreCount,private final non-sealed org.languagetool.tokenizers.Tokenizer wordTokenizer