<commit_before>package org.deeplearning4j.models.word2vec.actor;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.deeplearning4j.models.word2vec.StreamWork;
import org.deeplearning4j.models.word2vec.VocabWord;
import org.deeplearning4j.models.word2vec.VocabWork;
import org.deeplearning4j.text.invertedindex.InvertedIndex;
import org.deeplearning4j.text.tokenization.tokenizer.Tokenizer;
import org.deeplearning4j.text.tokenization.tokenizerfactory.TokenizerFactory;
import org.deeplearning4j.text.movingwindow.Util;
import org.deeplearning4j.models.word2vec.wordstore.VocabCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import akka.actor.UntypedActor;

/**
 * Individual actor for updating the vocab cache
 *
 * @author <NAME>
 */
public class VocabActor extends UntypedActor {

    private transient TokenizerFactory tokenizer;
    private List<String> stopWords;
    private AtomicLong lastUpdate;
    private VocabCache cache;
    private int minWordFrequency;
    private AtomicInteger numWordsEncountered;
    private InvertedIndex index;



    public VocabActor(
            TokenizerFactory tokenizer,
            VocabCache cache,
            List<String> stopWords,
            AtomicLong lastUpdate,
            int minWordFrequency,
            AtomicInteger numWordsEncountered,
            InvertedIndex index) {
        super();
        this.tokenizer = tokenizer;
        this.stopWords = stopWords;
        this.lastUpdate = lastUpdate;
        this.cache = cache;
        this.minWordFrequency = minWordFrequency;
        this.numWordsEncountered = numWordsEncountered;
        this.index = index;
    }




    @Override
    public void onReceive(Object message) throws Exception {
        Set<String> encountered = new HashSet<>();

        if(message  instanceof VocabWork) {
            List<VocabWord> document = new ArrayList<>();
            VocabWork work = (VocabWork) message;
            if(work.getWork() == null || work.getWork().isEmpty())
                return;
            //work.getCount().incrementAndGet();
            String sentence = work.getWork();
            if(sentence.isEmpty() || sentence.length() <= 2) {
                work.countDown();
                return;
            }
            Tokenizer t = tokenizer.create(sentence);
            while(t.hasMoreTokens())  {
                String token = t.nextToken();
                processToken(token,encountered,document);
            }
            index.addWordsToDoc(index.numDocuments(),document);
            numWordsEncountered.set(numWordsEncountered.get() + document.size());
            work.countDown();

            lastUpdate.getAndSet(System.currentTimeMillis());

        }


        else if(message instanceof StreamWork) {
            StreamWork work = (StreamWork) message;
            List<VocabWord> document = new ArrayList<>();

            InputStream is = work.getIs();
            if(is == null)
                return;
            boolean tryRead = false;
            try {
                if(is.available() > 0) {
                    tryRead = true;
                }
            }catch(Exception e) {
                tryRead = false;
            }

            if(!tryRead)
                return;

            Tokenizer t = tokenizer.create(is);

            while(t.hasMoreTokens())  {
                String token = t.nextToken();
                processToken(token,encountered,document);

            }

            //adds the words to the document after all of them have bene processed
            index.addWordsToDoc(index.numDocuments(),document);
            numWordsEncountered.set(numWordsEncountered.get() + document.size());

            IOUtils.closeQuietly(is);
            work.countDown();

            lastUpdate.getAndSet(System.currentTimeMillis());
<commit_msg>check for null or empty on word<commit_after>package org.deeplearning4j.models.word2vec.actor;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.deeplearning4j.models.word2vec.StreamWork;
import org.deeplearning4j.models.word2vec.VocabWord;
import org.deeplearning4j.models.word2vec.VocabWork;
import org.deeplearning4j.text.invertedindex.InvertedIndex;
import org.deeplearning4j.text.tokenization.tokenizer.Tokenizer;
import org.deeplearning4j.text.tokenization.tokenizerfactory.TokenizerFactory;
import org.deeplearning4j.text.movingwindow.Util;
import org.deeplearning4j.models.word2vec.wordstore.VocabCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import akka.actor.UntypedActor;

/**
 * Individual actor for updating the vocab cache
 *
 * @author <NAME>
 */
public class VocabActor extends UntypedActor {

    private transient TokenizerFactory tokenizer;
    private List<String> stopWords;
    private AtomicLong lastUpdate;
    private VocabCache cache;
    private int minWordFrequency;
    private AtomicInteger numWordsEncountered;
    private InvertedIndex index;



    public VocabActor(
            TokenizerFactory tokenizer,
            VocabCache cache,
            List<String> stopWords,
            AtomicLong lastUpdate,
            int minWordFrequency,
            AtomicInteger numWordsEncountered,
            InvertedIndex index) {
        super();
        this.tokenizer = tokenizer;
        this.stopWords = stopWords;
        this.lastUpdate = lastUpdate;
        this.cache = cache;
        this.minWordFrequency = minWordFrequency;
        this.numWordsEncountered = numWordsEncountered;
        this.index = index;
    }




    @Override
    public void onReceive(Object message) throws Exception {
        Set<String> encountered = new HashSet<>();

        if(message  instanceof VocabWork) {
            List<VocabWord> document = new ArrayList<>();
            VocabWork work = (VocabWork) message;
            if(work.getWork() == null || work.getWork().isEmpty())
                return;
            //work.getCount().incrementAndGet();
            String sentence = work.getWork();
            if(sentence.isEmpty() || sentence.length() <= 2) {
                work.countDown();
                return;
            }
            Tokenizer t = tokenizer.create(sentence);
            while(t.hasMoreTokens())  {
                String token = t.nextToken();
                processToken(token,encountered,document);
            }
            index.addWordsToDoc(index.numDocuments(),document);
            numWordsEncountered.set(numWordsEncountered.get() + document.size());
            work.countDown();

            lastUpdate.getAndSet(System.currentTimeMillis());

        }


        else if(message instanceof StreamWork) {
            StreamWork work = (StreamWork) message;
            List<VocabWord> document = new ArrayList<>();

            InputStream is = work.getIs();
            if(is == null)
                return;
            boolean tryRead = false;
            try {
                if(is.available() > 0) {
                    tryRead = true;
                }
            }catch(Exception e) {
                tryRead = false;
            }

            if(!tryRead)
                return;

            Tokenizer t = tokenizer.create(is);

            while(t.hasMoreTokens())  {
                String token = t.nextToken();
                if(token == null || token.isEmpty())
                    break;
                processToken(token,encountered,document);

            }

            //adds the words to the document after all of them have bene processed
            index.addWordsToDoc(index.numDocuments(),document);
            numWordsEncountered.set(numWordsEncountered.get() + document.size());

            IOUtils.closeQuietly(is);
            work.countDown();

            lastUpdate.getAndSet(System.currentTimeMillis());

{'fraction_non_alphanumeric': 0.09490629505045652, 'fraction_numerical': 0.004565112926477655, 'mean_word_length': 2.2905138339920947, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5533715', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2238, 'n_words': 442}