【问题标题】:python - increase efficiency of large-file search by readlines(size)python - 通过 readlines(size) 提高大文件搜索的效率
【发布时间】:2017-03-25 12:15:37
【问题描述】:

我是 Python 新手,目前正在使用 Python 2。我有一些源文件,每个文件都包含大量数据(大约 1900 万行)。如下所示:

apple   \t N   \t apple
n&apos
garden  \t N   \t garden
b\ta\md 
great   \t Adj \t great
nice    \t Adj \t (unknown)
etc

我的任务是在每个文件的第 3 列搜索一些目标词,每次在语料库中找到目标词时,都必须将这个词之前和之后的 10 个词添加到多维词典中。

编辑:应排除包含“&”、“\”或字符串“(未知)”的行。

我尝试使用 readlines() 和 enumerate() 来解决这个问题,如下面的代码所示。代码做了它应该做的事情,但对于源文件中提供的数据量来说,它显然不够高效。

我知道 readlines() 或 read() 不应该用于大型数据集,因为它将整个文件加载到内存中。然而,逐行阅读文件,我没有设法使用 enumerate 方法来获取目标单词前后的 10 个单词。 我也无法使用 mmap,因为我没有在该文件上使用它的权限。

所以,我想具有一定大小限制的 readlines 方法将是最有效的解决方案。但是,为此,我会不会犯一些错误,因为每次到达大小限制的末尾时,由于代码刚刚中断,目标单词之后的 10 个单词不会被捕获?

def get_target_to_dict(file):
targets_dict = {}
with open(file) as f:
    for line in f:
            targets_dict[line.strip()] = {}
return targets_dict

targets_dict = get_target_to_dict('targets_uniq.txt')
# browse directory and process each file 
# find the target words to include the 10 words before and after to the dictionary
# exclude lines starting with <,-,; to just have raw text

    def get_co_occurence(path_file_dir, targets, results):
        lines = []
        for file in os.listdir(path_file_dir):
            if file.startswith('corpus'):
            path_file = os.path.join(path_file_dir, file)
            with gzip.open(path_file) as corpusfile:
                # PROBLEMATIC CODE HERE
                # lines = corpusfile.readlines()
                for line in corpusfile:
                    if re.match('[A-Z]|[a-z]', line):
                        if '(unknown)' in line:
                            continue
                        elif '\\' in line:
                            continue
                        elif '&' in line:
                            continue
                        lines.append(line)
                for i, line in enumerate(lines):
                    line = line.strip()
                    if re.match('[A-Z][a-z]', line):
                        parts = line.split('\t')
                        lemma = parts[2]
                        if lemma in targets:
                            pos = parts[1]
                            if pos not in targets[lemma]:
                                targets[lemma][pos] = {}
                            counts = targets[lemma][pos]
                            context = []
                            # look at 10 previous lines
                            for j in range(max(0, i-10), i):
                                context.append(lines[j])
                            # look at the next 10 lines
                            for j in range(i+1, min(i+11, len(lines))):
                                context.append(lines[j])
                            # END OF PROBLEMATIC CODE
                            for context_line in context:
                                context_line = context_line.strip()
                                parts_context = context_line.split('\t')
                                context_lemma = parts_context[2]
                                if context_lemma not in counts:
                                    counts[context_lemma] = {}
                                context_pos = parts_context[1]
                                if context_pos not in counts[context_lemma]:
                                    counts[context_lemma][context_pos] = 0
                                counts[context_lemma][context_pos] += 1
                csvwriter = csv.writer(results, delimiter='\t')
                for k,v in targets.iteritems():
                    for k2,v2 in v.iteritems():
                        for k3,v3 in v2.iteritems():
                            for k4,v4 in v3.iteritems():
                                csvwriter.writerow([str(k), str(k2), str(k3), str(k4), str(v4)])
                                #print(str(k) + "\t" + str(k2) + "\t" + str(k3) + "\t" + str(k4) + "\t" + str(v4))

results = open('results_corpus.csv', 'wb')
word_occurrence = get_co_occurence(path_file_dir, targets_dict, results)

出于完整性的原因,我复制了整个代码部分,因为它是一个函数的一部分,该函数从提取的所有信息中创建一个多维字典,然后将其写入 csv 文件。

如果有任何提示或建议可以使这段代码更高效,我将不胜感激。

编辑我更正了代码,以便它考虑到目标单词前后的确切 10 个单词

【问题讨论】:

  • 您可以使用mapfiltergroupbyislice 高效地完成我的工作
  • 谢谢,我读过它,它似乎非常有效。您介意对上面的代码进行详细说明吗?要使用map,我肯定需要将语料库文件列出,对吧?
  • 您是在查找列中的前 10 个单词还是仅查找前 10 个单词?
  • 我正在寻找第 3 列中的前 10 个单词
  • 这可能更适合the code review stack exchange

标签: python dictionary multidimensional-array enumerate readlines


【解决方案1】:

我的想法是创建一个缓冲区在 10 行之前存储,另一个缓冲区在 10 行之后存储,当文件被读取时,它将被推入缓冲区之前,如果大小超过 10,缓冲区将被弹出

对于后缓冲区,我从文件迭代器 1st 克隆另一个迭代器。然后在循环内并行运行两个迭代器,克隆迭代器提前运行 10 次迭代以获得后面的 10 行。

这避免了使用 readlines() 并将整个文件加载到内存中。 希望它在实际情况下对你有用

已编辑: 如果第 3 列不包含“&”、“\”、“(未知)”中的任何一个,则仅填充前后缓冲区。还将 split('\t') 更改为 split() 以便处理所有空格或标签

import itertools
def get_co_occurence(path_file_dir, targets, results):
    excluded_words = ['&', '\\', '(unknown)'] # modify excluded words here 
    for file in os.listdir(path_file_dir): 
        if file.startswith('testset'): 
            path_file = os.path.join(path_file_dir, file) 
            with open(path_file) as corpusfile: 
                # CHANGED CODE HERE
                before_buf = [] # buffer to store before 10 lines 
                after_buf = []  # buffer to store after 10 lines 
                corpusfile, corpusfile_clone = itertools.tee(corpusfile) # clone file iterator to access next 10 lines 
                for line in corpusfile: 
                    line = line.strip() 
                    if re.match('[A-Z]|[a-z]', line): 
                        parts = line.split() 
                        lemma = parts[2]

                        # before buffer handling, fill buffer excluded line contains any of excluded words 
                        if not any(w in line for w in excluded_words): 
                            before_buf.append(line) # append to before buffer 
                        if len(before_buf)>11: 
                            before_buf.pop(0) # keep the buffer at size 10 
                        # next buffer handling
                        while len(after_buf)<=10: 
                            try: 
                                after = next(corpusfile_clone) # advance 1 iterator 
                                after_lemma = '' 
                                after_tmp = after.split()
                                if re.match('[A-Z]|[a-z]', after) and len(after_tmp)>2: 
                                    after_lemma = after_tmp[2]
                            except StopIteration: 
                                break # copy iterator will exhaust 1st coz its 10 iteration ahead 
                            if after_lemma and not any(w in after for w in excluded_words): 
                                after_buf.append(after) # append to buffer
                                # print 'after',z,after, ' - ',after_lemma
                        if (after_buf and line in after_buf[0]):
                            after_buf.pop(0) # pop off one ready for next

                        if lemma in targets: 
                            pos = parts[1] 
                            if pos not in targets[lemma]: 
                                targets[lemma][pos] = {} 
                            counts = targets[lemma][pos] 
                            # context = [] 
                            # look at 10 previous lines 
                            context= before_buf[:-1] # minus out current line 
                            # look at the next 10 lines 
                            context.extend(after_buf) 

                            # END OF CHANGED CODE
                            # CONTINUE YOUR STUFF HERE WITH CONTEXT

【讨论】:

  • 哇,好主意!非常感谢您的帮助和代码。我会在今天晚些时候试一试,并立即给你反馈。
  • 谢谢,这很有帮助。我没有考虑到在源文件(corpusfile)中,还有一些行在读入缓冲区之前应该被排除(包含'&'、'\'或'(未知)'的行,见编辑)。我一整天都在尝试将此添加到您的代码中,但没有得到任何结果。你有什么建议吗?它绝对应该在 for line in cowfile: line = line.strip() 之后。然而,整个缓冲区会变得混乱。
  • 看起来您的原始代码也没有按照您的描述进行操作,它只是前后 10 行,不管它是什么,然后只在处理上下文期间检查;如果前 10 行中有 2 行包含无效词,如未知,那么您将只剩下 8 行。所以你想要的应该是过滤并确保缓冲区之前和之后的所有 10 行在没有任何过滤词的情况下都是有效的,对吗?我稍后会尝试为此编辑我的代码。
  • 已编辑答案以解决您的 cmets,希望它适合您的需求 :)
【解决方案2】:

用 Python 3.5 编写的功能替代方案。我简化了你的例子,两边只取 5 个字。关于垃圾值过滤还有其他简化,但只需要稍作修改。我将使用 PyPI 中的包 fn 使这个功能代码更易于阅读。

from typing import List, Tuple
from itertools import groupby, filterfalse
from fn import F

首先我们需要提取列:

def getcol3(line: str) -> str:
    return line.split("\t")[2]

然后我们需要将行拆分为由谓词分隔的块:

TARGET_WORDS = {"target1", "target2"}

# this is out predicate
def istarget(word: str) -> bool:
    return word in TARGET_WORDS        

让我们过滤垃圾并编写一个函数来获取最后和前 5 个单词:

def isjunk(word: str) -> bool:
    return word == "(unknown)"

def first_and_last(words: List[str]) -> (List[str], List[str]):
    first = words[:5]
    last = words[-5:]
    return first, last

现在,让我们获取组:

words = (F() >> (map, str.strip) >> (filter, bool) >> (map, getcol3) >> (filterfalse, isjunk))(lines)
groups = groupby(words, istarget)

现在,处理组

def is_target_group(group: Tuple[str, List[str]]) -> bool:
    return istarget(group[0])

def unpack_word_group(group: Tuple[str, List[str]]) -> List[str]:
    return [*group[1]]

def unpack_target_group(group: Tuple[str, List[str]]) -> List[str]:
    return [group[0]]

def process_group(group: Tuple[str, List[str]]):
    return (unpack_target_group(group) if is_target_group(group) 
            else first_and_last(unpack_word_group(group)))

最后的步骤是:

words = list(map(process_group, groups))

附言

这是我的测试用例:

from io import StringIO

buffer = """
_\t_\tword
_\t_\tword
_\t_\tword
_\t_\t(unknown)
_\t_\tword
_\t_\tword
_\t_\ttarget1
_\t_\tword
_\t_\t(unknown)
_\t_\tword
_\t_\tword
_\t_\tword
_\t_\ttarget2
_\t_\tword
_\t_\t(unknown)
_\t_\tword
_\t_\tword
_\t_\tword
_\t_\t(unknown)
_\t_\tword
_\t_\tword
_\t_\ttarget1
_\t_\tword
_\t_\t(unknown)
_\t_\tword
_\t_\tword
_\t_\tword
"""

# this simulates an opened file
lines = StringIO(buffer)

给定这个文件,你会得到这个输出:

[(['word', 'word', 'word', 'word', 'word'],
  ['word', 'word', 'word', 'word', 'word']),
 (['target1'], ['target1']),
 (['word', 'word', 'word', 'word'], ['word', 'word', 'word', 'word']),
 (['target2'], ['target2']),
 (['word', 'word', 'word', 'word', 'word'],
  ['word', 'word', 'word', 'word', 'word']),
 (['target1'], ['target1']),
 (['word', 'word', 'word', 'word'], ['word', 'word', 'word', 'word'])]

您可以从这里删除前 5 个单词和最后 5 个单词。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多