Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从文本字符串中获取三元组_Python_Python 3.x - Fatal编程技术网

Python 从文本字符串中获取三元组

Python 从文本字符串中获取三元组,python,python-3.x,Python,Python 3.x,定义传递文本字符串的get\u triples\u dict()函数 作为一个参数。函数首先将参数字符串转换为 小写,然后返回一个字典,其中包含所有 文本中唯一的连续三个字母字符,以及 相应的值是连续三次测量的次数 字母字符出现在文本中。使用isalpha()方法 检查字符是否按字母顺序排列。这本词典只应 包含多次出现的条目。在你的字典出版之后 如果已创建并填充,则需要删除任何 相应的值为1 有人能帮我修改代码,这样我就能得到正确的输出吗?我已经试过了,但确实不行。我只需要一些关于get\u t

定义传递文本字符串的
get\u triples\u dict()
函数 作为一个参数。函数首先将参数字符串转换为 小写,然后返回一个字典,其中包含所有 文本中唯一的连续三个字母字符,以及 相应的值是连续三次测量的次数 字母字符出现在文本中。使用
isalpha()
方法 检查字符是否按字母顺序排列。这本词典只应 包含多次出现的条目。在你的字典出版之后 如果已创建并填充,则需要删除任何 相应的值为1

有人能帮我修改代码,这样我就能得到正确的输出吗?我已经试过了,但确实不行。我只需要一些关于
get\u triples\u dict
函数的帮助,我不需要触摸
test\u get\u triples\u 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"))
预期输出:


欢迎来到SO。请提供一份报告。这意味着要精确定位您所面临的问题,并构造一个最小的示例(通常是几行)来演示该问题。祝你好运可能重复的