/* Visitor for `WordNode`s.
197209
function visitor(node, position, parent) {
    var word
    var phrases
    var length
    var index
    var result

    if (!literals && isLiteral(parent, position)) {
      return
    }

    word = normalize(node, config)
    phrases = byWord['*'].concat(own.call(byWord, word) ? byWord[word] : [])
    length = phrases.length
    index = -1

    while (++index < length) {
      result = test(phrases[index], position, parent)

      if (result) {
        handler(result, position, parent, phrases[index])
      }
    }
  }
1143231
private final static void traverse(Element element, ElementVisitor visitor) {

		visitor.visit(element);

		NodeList children = element.getChildNodes();

		for (int i = 0; i < children.getLength(); i++) {
			Node node = children.item(i);

			if (node.getNodeType() != Node.ELEMENT_NODE)
				continue;

			traverse((Element) node, visitor);
		}
	}
210085
@Override
    public void visit(NodeVisitor v) {
        if (v.visit(this)) {
            iterator.visit(v);
            iteratedObject.visit(v);
        }
    }
1202314
function traverseNodes(nodes, visitor) {
    const ne = new NodeEventGenerator(visitor)
    traverse(nodes, {
        enter(child) {
            ne.enterNode(child)
        },

        leave(child) {
            ne.leaveNode(child)
        },
    })
}
113107
function reindexNodes () {
  var child = this.node.firstChild;
  this._length = 0;
  
  while (child) {
    this[this._length++] = child;
    child = child.nextSibling;
  }
}
673615
function findFirstTextNode(node) {
          var walker;

          if (node) {
            walker = new TreeWalker(node, node);

            for (node = walker.current(); node; node = walker.next()) {
              if (node.nodeType === 3) {
                return node;
              }
            }
          }
        }
458150
public void processNode(INode node) throws ContextPreprocessorException {
        try {
            // reset old preprocessing
            node.getNodeData().setcLabFormula("");
            node.getNodeData().setcNodeFormula("");
            while (0 < node.getNodeData().getACoLCount()) {
                node.getNodeData().removeACoL(0);
            }

            boolean isEmpty = true;
            String labelOfNode = node.getNodeData().getName().trim();

            if (debugLabels) {
                log.debug("preprocessing: " + labelOfNode);
            }

            labelOfNode = labelOfNode.toLowerCase();
            List<ISense> wnSense = new ArrayList<ISense>();
            if (!(("top".equals(labelOfNode) || "thing".equals(labelOfNode)) && !node.hasParent()) && (!meaninglessWords.contains(labelOfNode + " ")) && (isTokenMeaningful(labelOfNode))) {
                wnSense = linguisticOracle.getSenses(labelOfNode);
            }
            if ((0 == wnSense.size()) && !(("top".equals(labelOfNode) || "thing".equals(labelOfNode)) && !node.hasParent()) && (!meaninglessWords.contains(labelOfNode + " ")) && (isTokenMeaningful(labelOfNode))) {
                labelOfNode = replacePunctuation(labelOfNode);
                wnSense = linguisticOracle.getSenses(labelOfNode);
            }

            // identifiers of meaningful tokens in
            String meaningfulTokens = " ";
            // tokens of the label of node
            List<String> tokensOfNodeLabel = new ArrayList<String>();

            // is the label a WordNet entry?
            int id_tok = 0;
            if (0 < wnSense.size()) {
                // add to list of processed labels
                tokensOfNodeLabel.add(labelOfNode);
                List<String> lemmas = linguisticOracle.getBaseForms(labelOfNode);
                String lemma = labelOfNode;
                if (0 < lemmas.size()) {
                    lemma = lemmas.get(0);
                }

                // create atomic node of label
                IAtomicConceptOfLabel ACoL = createACoL(node, id_tok, labelOfNode, lemma);
                // to token ids
                meaningfulTokens = meaningfulTokens + id_tok + " ";
                // add senses to ACoL
                for (ISense sense : wnSense) {
                    ACoL.addSense(sense);
                }
                isEmpty = false;
                id_tok++;
            } else {
                // The label of node is not in WN
                // Split the label by words
                StringTokenizer lemmaTokenizer = new StringTokenizer(labelOfNode, " _()[]/'\\#1234567890");
                ArrayList<String> tokens = new ArrayList<String>();
                while (lemmaTokenizer.hasMoreElements()) {
                    tokens.add(lemmaTokenizer.nextToken());
                }

                // perform multiword recognition
                tokens = multiwordRecognition(tokens);
                // for all tokens in label
                for (int i = 0; i < tokens.size(); i++) {
                    String token = tokens.get(i).trim();
                    // if the token is not meaningless
                    if ((!meaninglessWords.contains(token + " ")) && (isTokenMeaningful(token))) {
                        // add to list of processed tokens
                        tokensOfNodeLabel.add(token);
                        // if not logical connective
                        if ((!andWords.contains(token)) && ((orWords.indexOf(token)) == -1)
                                && ((notWords.indexOf(token)) == -1) && (!isNumber(token))) {
                            // get WN senses for token
                            if (!(("top".equals(token) || "thing".equals(token)) && !node.hasParent())) {
                                wnSense = linguisticOracle.getSenses(token);
                            } else {
                                wnSense = new ArrayList<ISense>();
                            }
                            if (0 == wnSense.size()) {
                                List<String> newTokens = complexWordsRecognition(token);
                                if (0 < newTokens.size()) {
                                    tokensOfNodeLabel.remove(tokensOfNodeLabel.size() - 1);
                                    tokensOfNodeLabel.add(newTokens.get(0));
                                    wnSense = linguisticOracle.getSenses(newTokens.get(0));
                                    tokens.remove(i);
                                    tokens.add(i, newTokens.get(0));
                                    for (int j = 1; j < newTokens.size(); j++) {
                                        String s = newTokens.get(j);
                                        tokens.add(i + j, s);
                                    }
                                }
                            }
                            List<String> lemmas = linguisticOracle.getBaseForms(token);
                            String lemma = token;
                            if (0 < lemmas.size()) {
                                lemma = lemmas.get(0);
                            }

                            // create atomic node of label
                            IAtomicConceptOfLabel ACoL = createACoL(node, id_tok, token, lemma);
                            // mark id as meaningful
                            meaningfulTokens = meaningfulTokens + id_tok + " ";
                            // if there no WN senses
                            if (0 == wnSense.size() && !(("top".equals(labelOfNode) || "thing".equals(labelOfNode)) && !node.hasParent())) {
                                unrecognizedWords.add(token);
                            }
                            // add senses to ACoL
                            for (ISense sense : wnSense) {
                                ACoL.addSense(sense);
                            }
                            isEmpty = false;
                        }
                        id_tok++;
                    }
                }
            }

            if (isEmpty) {
                String token = labelOfNode.replaceAll(" ", "_");
                // add to list of processed labels
                tokensOfNodeLabel.add(token);
                // create atomic node of label
                createACoL(node, id_tok, token, token);
                // to token ids
                meaningfulTokens = meaningfulTokens + id_tok + " ";
            }
            // build complex formula of a node
            buildComplexConcept(node, tokensOfNodeLabel, meaningfulTokens);
            node.getNodeData().setIsPreprocessed(true);
        } catch (LinguisticOracleException e) {
            final String errMessage = e.getClass().getSimpleName() + ": " + e.getMessage();
            log.error(errMessage, e);
            throw new ContextPreprocessorException(errMessage, e);
        }
    }
1049294
async function lookup(words) {
  const index = new Index();
  const hits = await index.lookup(words);
  index.quit();
  words.forEach((word, i) => {
    console.log(`hits for "${word}":`, hits[i].join(', '));
  });
  return hits;
}
309882
function currentWord(x) {
    if ((x.b.data.wordWindow["0"] === token) &&
        (x.a === tag)) {
        return 1;
    }
    return 0;
  }
642264
private void getCollocationsList(Tree t, boolean threadSafe) {
    int leftMostLeaf = Trees.leftEdge(t,qTree);
    if (t.isPreTerminal()) return;
    List<Tree> children = t.getChildrenAsList();
    if (children.isEmpty()) return;
    //TODO: fix determineHead
    // - in phrases like "World Trade Organization 's" the head of the parent NP is "POS".
    // - this is problematic for the collocationFinder which assigns this head
    // as the POS for the collocation "World_Trade_Organization"!
    Label headLabel= hf.determineHead(t).label();
    StringBuffer testString = null;
    Integer leftSistersBuffer=0;//measures the length of sisters in words when reading
    for (int i = 0; i < children.size();i++){
      ArrayList<Integer> childConstituents = new ArrayList<Integer>();
      childConstituents.add(i);
      Tree subtree = children.get(i);
      Integer currWindowLength=0; //measures the length in words of the current collocation.
      getCollocationsList(subtree, threadSafe); //recursive call to get colls in subtrees.
      testString = new StringBuffer(160);
      testString.append(treeAsStemmedCollocation(subtree, threadSafe));
      testString.append("_");
      Integer thisSubtreeLength = subtree.yield().size();
      currWindowLength+=thisSubtreeLength;
      StringBuffer testStringNonStemmed = new StringBuffer(160);
      testStringNonStemmed.append(treeAsNonStemmedCollocation(subtree));
      testStringNonStemmed.append("_");

      //for each subtree i, we iteratively append word yields of succeeding sister
      //subtrees j and check their wordnet entries.  if they exist we write them to
      //the global collocationCollector pair by the indices of the leftmost and
      //rightmost words in the collocation.

      for (int j = i+1; j < children.size(); j++) {
        Tree sisterNode = children.get(j);
        childConstituents.add(j);
        testString.append(treeAsStemmedCollocation(sisterNode, threadSafe));
        testStringNonStemmed.append(treeAsNonStemmedCollocation(sisterNode));
        currWindowLength+=sisterNode.yield().size();
        if (DEBUG) {
       //   err.println("Testing string w/ reported indices:" + testString.toString()
         //             + " (" +(leftMostLeaf+leftSistersBuffer)+","+(leftMostLeaf+leftSistersBuffer+currWindowLength-1)+")");
        }
        //ignore collocations beginning with "the" or "a"
        if (StringUtils.lookingAt(testString.toString(), "(?:[Tt]he|THE|[Aa][Nn]?)[ _]")) {
          if (false) {
            err.println("CollocationFinder: Not collapsing the/a word: " +
                testString);
          }
        } else if (wordNetContains(testString.toString())) {
          Pair <Integer, Integer> c = new Pair<Integer,Integer>(leftMostLeaf+leftSistersBuffer,leftMostLeaf+leftSistersBuffer+currWindowLength-1);

          Collocation col = new Collocation(c,t,(ArrayList<Integer>)childConstituents.clone(),testString.toString(),headLabel);
          collocationCollector.add(col);
          if (DEBUG) {
            err.println("Found collocation in wordnet: "+ testString.toString());
            err.println("  Span of collocation is: " + c +
                "; childConstituents is: " + c);
          }
        }
        testString.append("_");
        if (StringUtils.lookingAt(testStringNonStemmed.toString(), "(?:[Tt]he|THE|[Aa][Nn]?)[ _]")) {
          if (false) {
            err.println("CollocationFinder: Not collapsing the/a word: " +
                testStringNonStemmed);
          }
        } else if (wordNetContains(testStringNonStemmed.toString())) {
          Pair <Integer, Integer> c = new Pair<Integer,Integer>(leftMostLeaf+leftSistersBuffer,leftMostLeaf+leftSistersBuffer+currWindowLength-1);

          Collocation col = new Collocation(c,t,(ArrayList<Integer>)childConstituents.clone(),testStringNonStemmed.toString(),headLabel);
          collocationCollector.add(col);
          if (DEBUG) {
            err.println("Found collocation in wordnet: "+ testStringNonStemmed.toString());
            err.println("  Span of collocation is: " + c +
                "; childConstituents is: " + c);
          }
        }
        testStringNonStemmed.append("_");
      }
      leftSistersBuffer+=thisSubtreeLength;
    }
  }
510291
def handle_term(self, _, __, tokens: ParseResults) -> ParseResults:
        """"""
        self.ensure_node(tokens)
        return tokens
1045164
public Collection<String> wordsNearest(String word, int n) {
        return modelUtils.wordsNearest(word, n);
    }
939335
function ( nodes ) {
			var form = nodes[0],
				word = nodes[1];

			return word && form && this.language.convertGrammar( word, form );
		}
1143264
function(word) {
        var re,
            parsed = false,
            network = this.model.get('network');

        if (!network) {
            return;
        }

        re = new RegExp('(^|\\s)([' + _.escapeRegExp(network.get('channel_prefix')) + '][^ .,\\007]+)', 'g');

        if (!word.match(re)) {
            return parsed;
        }

        parsed = word.replace(re, function (m1, m2) {
            return m2 + '<a class="chan" data-channel="' + _.escape(m1.trim()) + '">' + _.escape(m1.trim()) + '</a>';
        });

        return parsed;
    }
1094274
@Override
    public Collection<String> wordsNearestSum(INDArray words, int top) {

        if (lookupTable instanceof InMemoryLookupTable) {
            InMemoryLookupTable l = (InMemoryLookupTable) lookupTable;
            INDArray syn0 = l.getSyn0();
            INDArray weights = syn0.norm2(0).rdivi(1).muli(words);
            INDArray distances = syn0.mulRowVector(weights).sum(1);
            INDArray[] sorted = Nd4j.sortWithIndices(distances, 0, false);
            INDArray sort = sorted[0];
            List<String> ret = new ArrayList<>();

            // FIXME: int cast
            if (top > sort.length())
                top = (int) sort.length();
            //there will be a redundant word
            int end = top;
            for (int i = 0; i < end; i++) {
                String add = vocabCache.wordAtIndex(sort.getInt(i));
                if (add == null || add.equals("UNK") || add.equals("STOP")) {
                    end++;
                    if (end >= sort.length())
                        break;
                    continue;
                }
                ret.add(vocabCache.wordAtIndex(sort.getInt(i)));
            }
            return ret;
        }

        Counter<String> distances = new Counter<>();

        for (String s : vocabCache.words()) {
            INDArray otherVec = lookupTable.vector(s);
            double sim = Transforms.cosineSim(words, otherVec);
            distances.incrementCount(s, (float) sim);
        }

        distances.keepTopNElements(top);
        return distances.keySet();
    }
1270361
private static void analyzeNode(TreeGraphNode t, TreeGraphNode root, Collection<GrammaticalRelation> relations) {
    // if (t.numChildren() > 0) {          // don't do leaves
    if (t.isPhrasal()) {    // don't do leaves or preterminals!
      TreeGraphNode tHigh = t.highestNodeWithSameHead();
      for (GrammaticalRelation egr : relations) {
        if (egr.isApplicable(t)) {
          for (Tree u : egr.getRelatedNodes(t, root)) {
            tHigh.addArc(GrammaticalRelation.getAnnotationClass(egr), (TreeGraphNode) u);
          }
        }
      }
      // now recurse into children
      for (TreeGraphNode kid : t.children()) {
        analyzeNode(kid, root, relations);
      }
    }
  }
348842
@Override
    public void visit(NodeVisitor v) {
        if (v.visit(this)) {
            for (AstNode e : getElements()) {
                e.visit(v);
            }
        }
    }