Python анализирует текст из нескольких файлов txt

Поиск советов о том, как создавать объекты из нескольких текстовых файлов для создания словаря.

Этот текстовый файл: https://pastebin.com/Npcp3HCM

Был преобразован вручную в эту требуемую структуру данных: https://drive.google.com/file/d/0B2AJ7rliSQubV0J2Z0d0eXF3bW8/view

Существуют тысячи таких текстовых файлов, и они могут иметь разные заголовки разделов, как показано в этих примерах:

Я начал с чтения файлов

from glob import glob

txtPth = '../tr-txt/*.txt'
txtFiles = glob(txtPth)

with open(txtFiles[0],'r') as tf:
    allLines = [line.rstrip() for line in tf]

sectionHeading = ['Corporate Participants',
                  'Conference Call Participiants',
                  'Presentation',
                  'Questions and Answers']

for lineNum, line in enumerate(allLines):
    if line in sectionHeading:
        print(lineNum,allLines[lineNum])

Моя идея состояла в том, чтобы искать номера строк, в которых существовали заголовки разделов, и пытаться извлечь содержимое между этими номерами строк, а затем выделять разделители как тире. Это не сработало, и я застрял в попытке создать такой словарь, чтобы впоследствии я мог запускать различные алгоритмы обработки естественного языка на добытых элементах.

{file-name1:{
    {date-time:[string]},
    {corporate-name:[string]},
    {corporate-participants:[name1,name2,name3]},
    {call-participants:[name4,name5]},
    {section-headings:{
        {heading1:[
            {name1:[speechOrderNum, text-content]},
            {name2:[speechOrderNum, text-content]},
            {name3:[speechOrderNum, text-content]}],
        {heading2:[
            {name1:[speechOrderNum, text-content]},
            {name2:[speechOrderNum, text-content]},
            {name3:[speechOrderNum, text-content]},
            {name2:[speechOrderNum, text-content]},
            {name1:[speechOrderNum, text-content]},
            {name4:[speechOrderNum, text-content]}],
        {heading3:[text-content]},
        {heading4:[text-content]}
        }
    }
}

Проблема состоит в том, что разные файлы могут иметь разные заголовки и количество заголовков. Но всегда будет раздел под названием "Презентация" и, скорее всего, будет иметь раздел "Вопрос и ответ" . Заголовки этих разделов всегда разделяются строкой равных знакам. А содержание разных динамиков всегда разделяется строкой тире. "Речевой порядок" для раздела Q & A обозначается цифрой в квадратных скобках. Участники всегда указываются в начале документа со звездочками перед их именем, и их плитка всегда находится на следующей строке.

Приветствуется любое предложение о том, как разбирать текстовые файлы. Идеальной помощью было бы дать рекомендации о том, как создать такой словарь (или другую подходящую структуру данных) для каждого файла, который затем может быть записан в базу данных.

Спасибо

- EDIT -

Один из файлов выглядит следующим образом: https://pastebin.com/MSvmHb2e

В котором раздел "Вопрос и ответ" помечен как "Презентация", и нет другого раздела "Вопрос и ответ" .

И окончательный текст примера: https://pastebin.com/jr9WfpV8

Ответы

Ответ 1

Комментарии в коде должны объяснять все. Дайте мне знать, если что-то указано, и вам нужно больше комментариев.

Короче говоря, я использую регулярное выражение, чтобы найти строки разделителя '=', чтобы разделить весь текст на подразделы, а затем обрабатывать каждый тип разделов отдельно для ясности (так что вы можете рассказать, как я обрабатываю каждый случай).

Боковое примечание: я использую слово "посетитель" и "автор" взаимозаменяемо.

EDIT: Обновлен код для сортировки на основе шаблона [x], который находится рядом с участником/автором в разделе презентации /QA. Также изменилась красивая печатная часть, так как pprint не очень хорошо обрабатывает OrderedDict.

Чтобы удалить любые дополнительные пробелы, включая \n в любом месте строки, просто выполните str.strip(). если вам нужно снять только \n, просто сделайте str.strip('\n').

Я изменил код, чтобы удалить пробелы в разговорах.

import json
import re
from collections import OrderedDict
from pprint import pprint


# Subdivides a collection of lines based on the delimiting regular expression.
# >>> example_string =' =============================
#                       asdfasdfasdf
#                       sdfasdfdfsdfsdf
#                       =============================
#                       asdfsdfasdfasd
#                       =============================
# >>> subdivide(example_string, "^=+")
# >>> ['asdfasdfasdf\nsdfasdfdfsdfsdf\n', 'asdfsdfasdfasd\n']
def subdivide(lines, regex):
    equ_pattern = re.compile(regex, re.MULTILINE)
    sections = equ_pattern.split(lines)
    sections = [section.strip('\n') for section in sections]
    return sections


# for processing sections with dashes in them, returns the heading of the section along with
# a dictionary where each key is the subsection header, and each value is the text in the subsection.
def process_dashed_sections(section):

    subsections = subdivide(section, "^-+")
    heading = subsections[0]  # header of the section.
    d = {key: value for key, value in zip(subsections[1::2], subsections[2::2])}
    index_pattern = re.compile("\[(.+)\]", re.MULTILINE)

    # sort the dictionary by first capturing the pattern '[x]' and extracting 'x' number.
    # Then this is passed as a compare function to 'sorted' to sort based on 'x'.
    def cmp(d):
        mat = index_pattern.findall(d[0])
        if mat:
            print(mat[0])
            return int(mat[0])
        # There are issues when dealing with subsections containing '- but not containing '[x]' pattern.
        # This is just to deal with that small issue.
        else:
            return 0

    o_d = OrderedDict(sorted(d.items(), key=cmp))
    return heading, o_d


# this is to rename the keys of 'd' dictionary to the proper names present in the attendees.
# it searches for the best match for the key in the 'attendees' list, and replaces the corresponding key.
# >>> d = {'mr. man   ceo of company   [1]' : ' This is talk a' ,
#  ...     'ms. woman  ceo of company    [2]' : ' This is talk b'}
# >>> l = ['mr. man', 'ms. woman']
# >>> new_d = assign_attendee(d, l)
# new_d = {'mr. man': 'This is talk a', 'ms. woman': 'This is talk b'}
def assign_attendee(d, attendees):
    new_d = OrderedDict()
    for key, value in d.items():
        a = [a for a in attendees if a in key]
        if len(a) == 1:
            # to strip out any additional whitespace anywhere in the text including '\n'.
            new_d[a[0]] = value.strip()
        elif len(a) == 0:
            # to strip out any additional whitespace anywhere in the text including '\n'.
            new_d[key] = value.strip()
    return new_d


if __name__ == '__main__':
    with open('input.txt', 'r') as input:
        lines = input.read()

        # regex pattern for matching headers of each section
        header_pattern = re.compile("^.*[^\n]", re.MULTILINE)

        # regex pattern for matching the sections that contains
        # the list of attendee (those that start with asterisks )
        ppl_pattern = re.compile("^(\s+\*)(.+)(\s.*)", re.MULTILINE)

        # regex pattern for matching sections with subsections in them.
        dash_pattern = re.compile("^-+", re.MULTILINE)

        ppl_d = dict()
        talks_d = dict()

        # Step1. Divide the the entire document into sections using the '=' divider
        sections = subdivide(lines, "^=+")
        header = []
        print(sections)
        # Step2. Handle each section like a switch case
        for section in sections:

            # Handle headers
            if len(section.split('\n')) == 1:  # likely to match only a header (assuming )
                header = header_pattern.match(section).string

            # Handle attendees/authors
            elif ppl_pattern.match(section):
                ppls = ppl_pattern.findall(section)
                d = {key.strip(): value.strip() for (_, key, value) in ppls}
                ppl_d.update(d)

                # assuming that if the previous section was detected as a header, then this section will relate
                # to that header
                if header:
                    talks_d.update({header: ppl_d})

            # Handle subsections
            elif dash_pattern.findall(section):
                heading, d = process_dashed_sections(section)

                talks_d.update({heading: d})

            # Else its just some random text.
            else:

                # assuming that if the previous section was detected as a header, then this section will relate
                # to that header
                if header:
                    talks_d.update({header: section})

        #pprint(talks_d)
        # To assign the talks material to the appropriate attendee/author. Still works if no match found.
        for key, value in talks_d.items():
            talks_d[key] = assign_attendee(value, ppl_d.keys())

        # ordered dict does not pretty print using 'pprint'. So a small hack to make use of json output to pretty print.
        print(json.dumps(talks_d, indent=4))

Ответ 2

Не могли бы вы подтвердить, что вам нужны только разделы "Презентация" и "Вопрос и ответ"? Кроме того, что касается вывода, это нормально, чтобы сбрасывать формат CSV, подобный тому, что вы "вручную преобразовали".

Обновлено решение для работы с каждым предоставленным вами образцом.

Вывод осуществляется из ячейки "D: H" в соответствии с общим файлом "Parsed-transcript".

#state = ["other", "head", "present", "qa", "speaker", "data"]
# codes : 0, 1, 2, 3, 4, 5
def writecell(out, data):
    out.write(data)
    out.write(",")

def readfile(fname, outname):
    initstate = 0
    f = open(fname, "r")
    out = open(outname, "w")
    head = ""
    head_written = 0
    quotes = 0
    had_speaker = 0
    for line in f:
        line = line.strip()
        if not line: continue
        if initstate in [0,5] and not any([s for s in line if "=" != s]):
            if initstate == 5:
                out.write('"')
                quotes = 0
                out.write("\n")
            initstate = 1
        elif initstate in [0,5] and not any([s for s in line if "-" != s]):
            if initstate == 5:
                out.write('"')
                quotes = 0
                out.write("\n")
                initstate = 4
        elif initstate == 1 and line == "Presentation":
            initstate = 2
            head = "Presentation"
            head_written = 0
        elif initstate == 1 and line == "Questions and Answers":
            initstate = 3
            head = "Questions and Answers"
            head_written = 0
        elif initstate == 1 and not any([s for s in line if "=" != s]):
            initstate = 0
        elif initstate in [2, 3] and not any([s for s in line if ("=" != s and "-" != s)]):
            initstate = 4
        elif initstate == 4 and '[' in line and ']' in line:
            comma = line.find(',')
            speech_st = line.find('[')
            speech_end = line.find(']')
            if speech_st == -1:
                initstate = 0
                continue
            if comma == -1:
                firm = ""
                speaker = line[:speech_st].strip()
            else:
                speaker = line[:comma].strip()
                firm = line[comma+1:speech_st].strip()
            head_written = 1
            if head_written:
                writecell(out, head)
                head_written = 0
            order = line[speech_st+1:speech_end]
            writecell(out, speaker)
            writecell(out, firm)
            writecell(out, order)
            had_speaker = 1
        elif initstate == 4 and not any([s for s in line if ("=" != s and "-" != s)]):
            if had_speaker:
                initstate = 5
                out.write('"')
                quotes = 1
            had_speaker = 0
        elif initstate == 5:
            line = line.replace('"', '""')
            out.write(line)
        elif initstate == 0:
            continue
        else:
            continue
    f.close()
    if quotes:
        out.write('"')
    out.close()

readfile("Sample1.txt", "out1.csv")
readfile("Sample2.txt", "out2.csv")
readfile("Sample3.txt", "out3.csv")

Подробнее

в этом решении есть машина состояний, которая работает следующим образом: 1. определяет, присутствует ли заголовок, если да, напишите его 2. обнаруживает динамики после записи заголовка 3. пишет записи для этого докладчика 4. переключается на следующий громкоговоритель и т.д.

После этого вы можете обрабатывать файлы csv по своему усмотрению. Вы также можете заполнить данные в любом формате, который вам нужен, когда будет выполнена основная обработка.

Edit:

Пожалуйста, замените функцию "writecell"

def writecell(out, data):
    data = data.replace('"', '""')
    out.write('"')
    out.write(data)
    out.write('"')
    out.write(",")