15
Фев
2017

Объясните пожалуйста что происходит после строчки matrix[]

import scipy.spatial
import numpy as np
import re

with open("sentences", "r") as file:
    sentences = file.readlines()

i = 0
for sentence in sentences:
    sentence = re.split('[^a-z]', sentence.lower())

    # Rewrite sentence with removed empty strings '' after splitting
    sentences[i] = filter(None, sentence)
    i += 1

word_index = dict()
i = 0
for sentence in sentences:
    for word in sentence:
        if word not in word_index:
            word_index[word] = i
            i += 1

matrix = []
for sent_i in xrange(0, len(sentences)):
    matrix.append([0 for x in word_index])

    for word in sentences[sent_i]:
        word_i = word_index[word]
        matrix[sent_i][word_i] += 1

np_matrix = np.array(matrix)

distances = list()
for i in range(len(sentences)):
    distance = scipy.spatial.distance.cosine(np_matrix[0], np_matrix[i])
    distances.append((i, distance))

sorted_list = sorted(distances, key=lambda tup: tup[1])

print sorted_list[1][0], sorted_list[2][0]

Объясните пожалуйста что происходит после строчки matrix[].

Источник: https://ru.stackoverflow.com/questions/627907/%D0%9E%D0%B1%D1%8A%D1%8F%D1%81%D0%BD%D0%B8%D1%82%D0%B5-%D0%BF%D0%BE%D0%B6%D0%B0%D0%BB%D1%83%D0%B9%D1%81%D1%82%D0%B0-%D1%87%D1%82%D0%BE-%D0%BF%D1%80%D0%BE%D0%B8%D1%81%D1%85%D0%BE%D0%B4%D0%B8%D1%82-%D0%BF%D0%BE%D1%81%D0%BB%D0%B5-%D1%81%D1%82%D1%80%D0%BE%D1%87%D0%BA%D0%B8-matrix

Тебе может это понравится...

Добавить комментарий