code: def test_text_explainer_position_dependent():
      text = "foo bar baz egg spam bar baz egg spam ham"
  
      @_apply_to_list
      def predict_proba(doc):
          tokens = doc.split()
          # 'bar' is only important in the beginning of the document,
          # not in the end
          return (
              [0, 1]
              if len(tokens) >= 2 and tokens[1] == "bar"
              else [1, 0]
          )
  
      # bag of words model is not powerful enough to explain predict_proba above
      te = TextExplainer(random_state=42, vec=CountVectorizer())
      te.fit(text, predict_proba)
      print(te.metrics_)
      assert te.metrics_["score"] < 0.9
      assert te.metrics_["mean_KL_divergence"] > 0.3
  
      # position_dependent=True can make it work
      te = TextExplainer(position_dependent=True, random_state=42)
      te.fit(text, predict_proba)
      print(te.metrics_)
      assert te.metrics_["score"] > 0.95
      assert te.metrics_["mean_KL_divergence"] < 0.3
  
      expl = te.explain_prediction()
      format_as_all(expl, te.clf_)
  
      # it is also possible to almost make it work using a custom vectorizer
      vec = CountVectorizer(ngram_range=(1, 2))
      te = TextExplainer(vec=vec, random_state=42)
      te.fit(text, predict_proba)
      print(te.metrics_)
      assert te.metrics_["score"] > 0.95
      assert te.metrics_["mean_KL_divergence"] < 0.3
  
      expl = te.explain_prediction()
      format_as_all(expl, te.clf_)
  
      # custom vectorizers are not supported when position_dependent is True
      with pytest.raises(ValueError):
          te = TextExplainer(
              position_dependent=True, vec=HashingVectorizer()
          )
  
file_path: eli5/tests/test_lime.py
imports: from __future__ import absolute_import
  import numpy
  from sklearn.feature_extraction.text import HashingVectorizer, CountVectorizer
  from sklearn.naive_bayes import MultinomialNB
  from sklearn.tree import DecisionTreeClassifier
  from sklearn.pipeline import make_pipeline
  import pytest
  from eli5.lime import TextExplainer
  from eli5.formatters import format_as_text
  from .utils import format_as_all, check_targets_scores
label_desc: Use image node_id 4 for calling a global function with example usage: test_text_explainer_position_dependent() and returns: unknown