创建配置文件并使用计数

2024-05-23 16:50:55 发布

您现在位置:Python中文网/ 问答频道 /正文

这将是一个很难解释,但我会尽我所能。你知道吗

所以我有一个文本文件,是一个段落。我最近把这个段落改成只包含唯一的词(没有停止词)。下面是一个示例:

'mississippi worth reading about', ' commonplace river contrary ways remarkable', ' considering missouri main branch longest river world--four miles', ' seems safe crookedest river world part journey uses cover ground crow fly six seventy-five', ' discharges water st', ' lawrence twenty-five rhine thirty-eight thames', ' river vast drainage-basin draws water supply twenty-eight states territories delaware atlantic seaboard country idaho pacific slope spread forty-five degrees longitude', ' mississippi receives carries gulf water fifty-four subordinate rivers navigable steamboats hundreds navigable flats keels', ' area drainage-basin combined areas england wales scotland ireland france spain portugal germany austria italy turkey almost wide region fertile mississippi valley proper exceptionally so']

我在这里所做的是把段落分成几个句子,去掉任何标点符号。然后我把它放进一个列表。你知道吗

例如,该列表名为temp,如果我打印出print(temp[0]),它将输出:

'mississippi worth reading about'

太棒了。然而,我的下一步,我被困在我试图创建一个小词库使用的余弦相似方程,也许你们中的一些人熟悉。你知道吗

不过,首先我想创建一些配置文件。我将给出一个配置文件示例,如'River'。在临时列表中,每个元素都是一个句子。我想实现的是,对于每个包含单词river的句子,创建一个该句子中其他所有单词的计数。你知道吗

因此,如果我有'commonplace river contrary ways remarkable',也就是temp[1],那么使用count方法的字典的开头就是。你知道吗

{'commonplace': 1, 'river': 1, 'contrary': 1, 'ways': 1, 'remarkable': 1,}

首先看一下输出:

river 1 (profile word)
   commonplace: 1
   contrary: 1
   remarkable: 1
   ways: 1

所以对于每一个在这句话里有河应该是最后的输出。你知道吗

river 4 (profile)
    atlantic: 1
    branch: 1
    commonplace: 1
    considering: 1
    contrary: 1
    country: 1
    cover: 1
    crookedest: 1
    crow: 1
    degrees: 1
    delaware: 1
    drainage-basin: 1
    draws: 1
    fly: 1
    forty-five: 1
    ground: 1
    idaho: 1
    journey: 1
    longest: 1
    longitude: 1
    main: 1
    missouri: 1
    pacific: 1
    part: 1
    remarkable: 1
    safe: 1
    seaboard: 1
    seems: 1
    seventy-five: 1
    six: 1
    slope: 1
    spread: 1
    states: 1
    supply: 1
    territories: 1
    twenty-eight: 1
    uses: 1
    vast: 1
    water: 1
    ways: 1

我不知道是不是最好只是一个完整的单字列表,而不是把单字作为一个元素拆分成一个句子。例如,这是上面第一个列表中的一组单词。你知道吗

{'austria', 'fortyfive', 'fiftyfour', 'longest', 'vast', 'almost', 'states', 'region', 'commonplace', 'wide', 'flats', 'main', 'longitude', 'part', 'gulf', 'st', 'contrary', 'missouri', 'pacific', 'hundreds', 'area', 'areas', 'turkey', 'discharges', 'twentyeight', 'fly', 'worth', 'thirtyeight', 'valley', 'seaboard', 'wales', 'ireland', 'ways', 'uses', 'scotland', 'ground', 'river', 'steamboats', 'seventyfive', 'territories', 'safe', 'degrees', 'twentyfive', 'england', 'thames', 'subordinate', 'drainagebasin', 'water', 'considering', 'fertile', 'rivers', 'spread', 'reading', 'combined', 'seems', 'france', 'crookedest', 'drainagebasin:', 'supply', 'rhine', 'portugal', 'six', 'slopea', 'draws', 'exceptionally', 'mississippi', 'idaho', 'worldfour', 'atlantic', 'italy', 'spain', 'receives', 'cover', 'remarkable', 'germany', 'crow', 'delaware', 'country', 'branch', 'carries', 'proper', 'lawrence', 'journey', 'keels', 'navigable'}

我很抱歉,如果这是一个错误的解释,但它很难为我解释。这是阻碍我使用余弦相似方程的障碍。你知道吗

谢谢你

编辑:

仅设置唯一单词:

{'remarkable', 'six', 'part', 'navigable', 'england', 'areas', 'worth', 'ways', 'longest', 'lawrence', 'journey', 'longitude', 'austria', 'rivers', 'st', 'crow', 'pacific', 'thirty-eight', 'gulf', 'ireland', 'drainage-basin', 'delaware', 'spread', 'proper', 'subordinate', 'territories', 'germany', 'cover', 'fifty-four', 'slope--a', 'fertile', 'degrees', 'wales', 'seems', 'exceptionally', 'water', 'italy', 'fly', 'missouri', 'turkey', 'atlantic', 'flats', 'hundreds', 'world--four', 'branch', 'twenty-eight', 'main', 'spain', 'receives', 'keels', 'states', 'portugal', 'draws', 'almost', 'contrary', 'seaboard', 'safe', 'mississippi', 'idaho', 'scotland', 'steamboats', 'france', 'valley', 'twenty-five', 'carries', 'wide', 'crookedest', 'area', 'reading', 'rhine', 'discharges', 'uses', 'commonplace', 'combined', 'considering', 'seventy-five', 'river', 'region', 'forty-five', 'ground', 'country', 'vast', 'thames', 'supply'}

我的尝试:

for i in unique:
            kw = i
            count_word = [i for i in temp for j in i.split() if j == kw]
            count_dict = {j: i.count(j) for i in count_word for j in i.split() if j != kw}
            print(kw)
            for a, c in sorted(count_dict.items(), key=lambda x: x[0]):
                print('{}: {}'.format(a, c))
            print()

Tags: in列表forcountremarkable句子waysfive
1条回答
网友
1楼 · 发布于 2024-05-23 16:50:55

为此,我们可以将kw(keyword)指定为river,然后我们可以使用列表理解来获取包含该kw的所有项,注意有些句子包含rivers,因此kw in将不起作用。从这里开始,我们可以使用字典理解来构造一个字典,我们将使用j来表示i.split()中的每个单词,i.count(j)来表示每个项目中每个单词的计数,我们还将加入if j != kw,因此我们的列表中不包括river。最后,我们可以使用for k, v in dicta.items()打印,如果需要,我们可以添加排序方法,以按字母顺序获得结果。你知道吗

kw = 'river'
lista = [i for i in temp for j in i.split() if j == kw]
dicta = {j: i.count(j) for i in lista for j in i.split() if j != kw}

for k, v in sorted(dicta.items(), key=lambda x: x[0]):
    print('{}: {}'.format(k, v))
atlantic: 1
branch: 1
commonplace: 1
considering: 1
contrary: 1
country: 1
...
twenty-eight: 1
uses: 1
vast: 1
water: 1
ways: 1
world: 1
world four: 1

扩展循环:

lista = []
for i in temp:
    for j in i.split():
        if j == kw:
            lista.append(i)

dicta = {}
for i in lista:
    for j in i.split():
        dicta[j] = i.count(j)

附加请求:

Read all entire file into one variable as string

all_words = 'some string'
all_words = all_words.split()
unique = set(all_words)

for i in unique:
    kw = i
    temp = list of sentences to check against
    rest of existing code
    maybe instead of printing the final statement append the dictionaries created to a list

相关问题 更多 >