首页 > 解决方案 > 从一串文本中获取三元组

问题描述

定义将get_triples_dict()文本字符串作为参数传递的函数。该函数首先将参数字符串转换为小写,然后返回一个字典,其键是文本中唯一的连续三个字母字符,对应的值是三个连续字母字符在文本中出现的次数。使用该isalpha()方法检查字符是否为字母。字典应该只包含多次出现的条目。创建并填充字典后,您需要删除任何对应值为 1 的键值对。

有人可以帮我修复我的代码,以便我可以获得正确的输出吗?我已经尝试过了,但它并不完全有效。我只需要该功能的一些帮助get_triples_dict,我不需要触摸该test_get_triples_dict功能。以下是我到目前为止的预期输出:

def get_triples_dict(text):

       triples_dict = {}

       word = ""

       mod_text = ""

       # convert the text to contain only letters and convert all letters to lowercase

       for i in range(0,len(text)):

                      if text[i].isalpha():

                                     mod_text = mod_text + text[i].lower()

       # if length of text >= 3 letters                     

       if(len(mod_text) >= 3):

                      # loop over the text

                      for i in range(0,len(mod_text)-2):

                                     word = mod_text[i:i+3] # extract 3 letter word from starting with the letter at i

                                     if word in triples_dict.keys(): # if word is in dictionary then increment its frequency

                                                    triples_dict[word] = triples_dict[word] + 1

                                     else: # else insert the word in dictionary with frequency 1

                                                    triples_dict[word] = 1



       keys = triples_dict.keys() # get the list of keys in the dictionary

       # loop over the keys list

       for i in range(0,len(keys)):

                      if triples_dict[keys] == 1: #if frequency of the word is 1, then remove the entry from dictionary

                                     del triples_dict[keys]
                                     i = i - 1 # decrement i

       return triples_dict # return the dictionary


def test_get_triples_dict():
    print("1.")
    print_dict_in_key_order(get_triples_dict('super, duper'))
    print("\n2.")
    print_dict_in_key_order(get_triples_dict("ABC ABC ABC"))
    print("\n3.")
    print_dict_in_key_order(get_triples_dict("Sometimes the smallest things make more room in your heart"))
    print("\n4.")
    print_dict_in_key_order(get_triples_dict("My favourite painting is the painting i did of my dog in that painting in my den"))

预期输出:

标签: pythonpython-3.x

解决方案


推荐阅读