Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在mongodb中,如何在大量(约100万)文档上加速计算bigram/trigram?_Python_Mongodb_Apache Spark_Pyspark_Pymongo - Fatal编程技术网

Python 在mongodb中,如何在大量(约100万)文档上加速计算bigram/trigram?

Python 在mongodb中,如何在大量(约100万)文档上加速计算bigram/trigram?,python,mongodb,apache-spark,pyspark,pymongo,Python,Mongodb,Apache Spark,Pyspark,Pymongo,我在mongodb中有大约一百万个文档,其中包含大量文本字段。我想提取最有意义的术语。我当前的逻辑是使用类似于下面逻辑的逻辑在Python中计算每周的bigram。问题是这种逻辑是缓慢的。有没有更快的方法来做这件事 from nltk.tokenize import sent_tokenize for week_start,week_end in zip(weeks[:-1],weeks[1:]): all_top_words = Counter() for post in c

我在mongodb中有大约一百万个文档,其中包含大量文本字段。我想提取最有意义的术语。我当前的逻辑是使用类似于下面逻辑的逻辑在Python中计算每周的bigram。问题是这种逻辑是缓慢的。有没有更快的方法来做这件事

from nltk.tokenize import sent_tokenize

for week_start,week_end in zip(weeks[:-1],weeks[1:]):
    all_top_words = Counter()
    for post in collection.find( {'date': {'$lt': week_end, '$gte': week_start},'text':{"$exists":True}}):
        text = strip_tags(post['text'])
        text = remove_brackets(text)
        sentences = sent_tokenize(text)
        for sentence in sentences:
            sentence = sentence.lower()
            sentence = remove_punctuation(sentence)
            top_words=Counter(ngrams(sentence.split(" "),2))
            all_top_words += top_words

代码的哪一部分删除了句子末尾的最后一个点“.”?嗨。我简化了StackOverflow的代码,更新后的代码更接近我的codeFor speed,您可以编写一个标记器,它只读取每个字符一次,并将文本拆分为标记。然后将word类型的令牌流添加到ngram计数器。不确定这样做是否值得,特别是因为处理标记会使标记器更加复杂。如何在不知道什么是慢的情况下加快速度?也就是说,您是否分析了代码,或者至少分析了其内部循环?代码的哪一部分删除了句子末尾的最后一个点“.”?您好。我简化了StackOverflow的代码,更新后的代码更接近我的codeFor speed,您可以编写一个标记器,它只读取每个字符一次,并将文本拆分为标记。然后将word类型的令牌流添加到ngram计数器。不确定这样做是否值得,特别是因为处理标记会使标记器更加复杂。如何在不知道什么是慢的情况下加快速度?也就是说,您是否分析了代码,或者至少分析了其内部循环?