В настоящее время у меня есть код python, который я бы хотел портировать на C++, поскольку он в настоящее время медленнее, чем мне хотелось бы. Проблема в том, что я использую словарь в нем, где ключ представляет собой кортеж, состоящий из объекта и строки (например, (obj, «word»)). Как я могу написать что-то подобное на C++? Может быть, мой алгоритм ужасен, и есть способ сделать это быстрее, не прибегая к C++?Как реализовать словарь «с кортежем Python» в качестве ключа на C++?
Полный алгоритм для ясности. Словарь «post_score» является проблемой.
def get_best_match_best(search_text, posts):
"""
Find the best matches between a search query "search_text" and any of the
strings in "posts".
@param search_text: Query to find an appropriate match with in posts.
@type search_text: string
@param posts: List of candidates to match with target text.
@type posts: [cl_post.Post]
@return: Best matches of the candidates found in posts. The posts are ordered
according to their rank. First post in list has best match and so on.
@returntype: [cl_post.Post]
"""
from math import log
search_words = separate_words(search_text)
total_number_of_hits = {}
post_score = {}
post_size = {}
for search_word in search_words:
total_number_of_hits[search_word] = 0.0
for post in posts:
post_score[(post, search_word)] = 0.0
post_words = separate_words(post.text)
post_size[post] = len(post_words)
for post_word in post_words:
possible_match = abs(len(post_word) - len(search_word)) <= 2
if possible_match:
score = calculate_score(search_word, post_word)
post_score[(post, search_word)] += score
if score >= 1.0:
total_number_of_hits[search_word] += 1.0
log_of_number_of_posts = log(len(posts))
matches = []
for post in posts:
rank = 0.0
for search_word in search_words:
rank += post_score[(post, search_word)] * \
(log_of_number_of_posts - log(1.0 + total_number_of_hits[search_word]))
matches.append((rank/post_size[post], post))
matches.sort(reverse=True)
return [post[1] for post in matches]
Вы еще пробовали Cython? –
Серьезно чувак, если код уже без ошибок, почему бы не использовать существующие инструменты? Видите ли, Джо Польски не рекомендует переписывать. –
@ Хамиш Грубиджан: кто такой Джо Польски, и почему меня должно волновать то, что он рекомендует? – sbk