快速计算双连词(使用或不使用多进程)- Python

8

给定norvig.com/big.txt中的big.txt,目标是快速计算二元组(想象一下我需要重复这个计数100,000次)。

根据Fast/Optimize N-gram implementations in python,像这样提取二元组将是最优的:

_bigrams = zip(*[text[i:] for i in range(2)])

如果我使用的是Python3,那么生成器不会被评估,直到我用list(_bigrams)或其他执行相同操作的函数将其实现。

import io
from collections import Counter

import time
with io.open('big.txt', 'r', encoding='utf8') as fin:
     text = fin.read().lower().replace(u' ', u"\uE000")

while True: 
    _bigrams = zip(*[text[i:] for i in range(2)])
    start = time.time()
    top100 = Counter(_bigrams).most_common(100)
    # Do some manipulation to text and repeat the counting.
    text = manipulate(text, top100)      

但是每次迭代需要大约1秒钟,而100,000次迭代太长了。

我也尝试过使用sklearn CountVectorizer,但是从提取、计数到获取前100个双词组的时间与原生Python相当。

然后我尝试了一些multiprocessing,使用了来自Python multiprocessing and a shared counterhttp://eli.thegreenplace.net/2012/01/04/shared-counter-with-pythons-multiprocessing的轻微修改:

from multiprocessing import Process, Manager, Lock

import time

class MultiProcCounter(object):
    def __init__(self):
        self.dictionary = Manager().dict()
        self.lock = Lock()

    def increment(self, item):
        with self.lock:
            self.dictionary[item] = self.dictionary.get(item, 0) + 1

def func(counter, item):
    counter.increment(item)

def multiproc_count(inputs):
    counter = MultiProcCounter()
    procs = [Process(target=func, args=(counter,_in)) for _in in inputs]
    for p in procs: p.start()
    for p in procs: p.join()
    return counter.dictionary

inputs = [1,1,1,1,2,2,3,4,4,5,2,2,3,1,2]

print (multiproc_count(inputs))

但是在bigram计数中使用MultiProcCounter甚至比每次迭代长1秒。我不知道为什么会这样,使用int的虚拟列表示例时,multiproc_count运行得很完美。
我尝试过:
import io
from collections import Counter

import time
with io.open('big.txt', 'r', encoding='utf8') as fin:
     text = fin.read().lower().replace(u' ', u"\uE000")

while True:
    _bigrams = zip(*[text[i:] for i in range(2)])
    start = time.time()
    top100 = Counter(multiproc_count(_bigrams)).most_common(100)

有没有一种快速计算Python中bigrams的方法?

听起来你应该研究分布式处理和Map/Reduce,如果你真的无法避免重复执行相同的操作100,000次。我想你的意思是你有更大的数据,而不是字面上重复计算100,000次;如果这确实是你的意思,那么这听起来就像你的基本计划有缺陷。 - tripleee
它重复相同的事情100,000次,但每次都会提取前100个二元组并操作文本,因此每次迭代时提取二元组的输入文本都不同。 - alvas
你认为 big.txt 的前20个bigram是['th', 'he', 'e ', ' p', 'pr', 'ro', 'oj', 'je', 'ec', 'ct', 't ', ' g', 'gu', 'ut', 'te', 'en', 'nb', 'be', 'er', 'rg'],就像你的代码生成的那样,还是一个基于单词的子集,例如['th', 'he', 'pr', 'ro', 'oj', 'je', 'ec', 'ct', 'gu', 'ut', 'te', 'en', 'nb', 'be', 'er', 'rg', 'eb', 'bo', 'oo', 'ok']?只是想了解游戏规则。 - cdlane
编辑是否如此复杂,以至于您无法在更改文本时更新二元计数?例如,当您将 o → a 替换为 dog 中的 o 时,可以将 doog 减少并将 daag 增加。如果大部分文本没有更改,则应该比重复计算更快。 - Palec
是的,更新二元组计数是可行的,但这意味着我需要一个 n^2 大小的哈希表,其中 n 是字符数,而在某些情况下,n=300,000 =(这个数量级)。 - alvas
2个回答

2
import os, thread

text = 'I really like cheese' #just load whatever you want here, this is just an example

CORE_NUMBER = os.cpu_count() # may not be available, just replace with how many cores you have if it crashes

ready = []
bigrams = []

def extract_bigrams(cores):
    global ready, bigrams
    bigrams = []
    ready = []
    for a in xrange(cores): #xrange is best for performance
        bigrams.append(0)
        ready.append(0)
    cpnt = 0#current point
    iterator = int(len(text)/cores)
    for a in xrange(cores-1):
        thread.start_new(extract_bigrams2, (cpnt, cpnt+iterator+1, a)) #overlap is intentional
        cpnt += iterator
    thread.start_new(extract_bigrams2, (cpnt, len(text), a+1))
    while 0 in ready:
        pass

def extract_bigrams2(startpoint, endpoint, threadnum):
    global ready, bigrams
    ready[threadnum] = 0
    bigrams[threadnum] = zip(*[text[startpoint+i:endpoint] for i in xrange(2)])
    ready[threadnum] = 1

extract_bigrams(CORE_NUMBER)
thebigrams = []
for a in bigrams:
    thebigrams+=a

print thebigrams

这个程序存在一些问题,例如它不能过滤空格或标点符号,但我制作这个程序是为了展示你应该追求什么。你可以轻松地编辑它以适应你的需求。
这个程序自动检测你的电脑有多少个内核,并创建相应数量的线程,试图均匀分配它查找双字母组的区域。我只能在学校拥有的计算机上的在线浏览器中测试此代码,因此我无法确定它是否完全有效。如果有任何问题或疑问,请在评论中留言。

我欣赏你的线程方法——例如数据过滤和大小写转换在线程之前发生,因此不会对性能提升产生显著影响。然而,你的解决方案实际上并没有计算二元组——一旦线程完成,主程序将不得不合并所有计数,这是单线程解决方案所面临的一个复杂问题。如果没有更完整的示例,很难知道这种额外开销是否会抵消潜在的收益。 - cdlane
你尝试过使用big.txt与本地Python非线程/非多进程方法进行基准测试吗? - alvas

0

我的建议:

Text= "The Project Gutenberg EBook of The Adventures of Sherlock Holmes"
"by Sir Arthur Conan Doyle"

# Counters
Counts= [[0 for x in range(128)] for y in range(128)]

# Perform the counting
R= ord(Text[0])
for i in range(1, len(Text)):
    L= R; R= ord(Text[i])
    Counts[L][R]+= 1

# Output the results
for i in range(ord('A'), ord('{')):
    if i < ord('[') or i >= ord('a'):
        for j in range(ord('A'), ord('{')):
            if (j < ord('[') or j >= ord('a')) and Counts[i][j] > 0:
                print chr(i) + chr(j), Counts[i][j]


Ad 1
Bo 1
EB 1
Gu 1
Ho 1
Pr 1
Sh 1
Th 2
be 1
ck 1
ct 1
dv 1
ec 1
en 2
er 2
es 2
he 3
je 1
lm 1
lo 1
me 1
nb 1
nt 1
oc 1
of 2
oj 1
ok 1
ol 1
oo 1
re 1
rg 1
rl 1
ro 1
te 1
tu 1
ur 1
ut 1
ve 1

这个版本是区分大小写的;最好先将整个文本转换为小写。


这是假设字符数量是固定的,对吗? - alvas
@alvas:多少个字符? - user1196549
我的意思是固定数组列表 Counts= [[0 for x in range(128)] for y in range(128)] - alvas
1
@alvas:没错,数组比字典更快。 - user1196549
@cdlane:我没有声称任何事情。你在什么文本长度上进行了基准测试? - user1196549
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接