Python функция векторизации и вызов проблемы save_dictionary - PullRequest
0 голосов
/ 02 мая 2020

Я работаю над созданием функции векторизации, которая выполняет следующие действия.

Взять строковый аргумент в качестве пути (папки) к месту расположения файлов текстовых данных; Обрабатывает все файлы данных в пути и производит статистику TF и ​​DF;

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

class Document: 
    def __init__(self, doc_id):
        # create a new document with its ID
        self.id = doc_id
        # create an empty dictionary 
        # that will hold the term frequency (TF) counts
        self.tfs = {}

    def tokenization(self, text):
        # split a title into words, 
        # using space " " as delimiter
        words = text.lower().split(" ")
        for word in words: 
           # for each word in the list
           if word in self.tfs: 
               # if it has been counted in the TF dictionary
               # add 1 to the count
               self.tfs[word] = self.tfs[word] + 1
           else:
               # if it has not been counted, 
               # initialize its TF with 1
               self.tfs[word] = 1

def save_dictionary(diction_data, file_path_name):
    # print the key-values pair in a dictionary
    f = open("./textfiles", "w+")
    for key in diction_data: 
        f.print(key, diction_data[key])
        f.close()

def vectorize(data_path):
    Document = []
    for i in range(1, 21):
        file_name = "./textfiles/"+ i + ".txt"
        # create a new document with an ID
    Document = Document(i+1)
        #Read the files
    f = open(Document)
    print(f.read())
        # compute the term frequencies
    Document.tokenization(file_name)
        # add the documents to the lists
    Documents.append(Document)

Ответы [ 2 ]

0 голосов
/ 02 мая 2020

Я думаю, у вас есть небольшие пробелы в python. Не входил в реализацию класса, но вот несколько замечаний: знайте, что при токенизации вы передаете только путь, но в методе, использующем его в качестве текста файла, вам сначала нужно открыть путь к файлу и прочитать его содержимое. .

def vectorize(data_path):
    documents = [] # No need to declare the type of the array
    for i in range(1, 21):
        file_name = "./textfiles/"+ i + ".txt"
        # create a new document with an ID
    doc= Document(i+1) # Initiation
        # compute the term frequencies
    doc.tokenization(file_name)
        # add the documents to the lists
    documents .append(doc) # appending a current document to documents array

Да, и, очевидно, измените имя класса на Document, как обычно, pep8 вы можете посмотреть здесь: pep8

О save_dictionary fun c: Я бы сделал это методом класса Document. и используйте json, чтобы сохранить его в файл:

import json
def save_dictionary(self):
    # print the key-values pair in a dictionary
    with open(f'somepath/tf_{self.id}.txt', 'w') as f:
        f.write(json.dumps(self.tfs))
0 голосов
/ 02 мая 2020

Проверьте функцию векторизации:
1) Документ не определен
2) Я предполагаю, что вы хотите создать пустой список: document = []

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...