简体   繁体   中英

Finding a word following two input words from txt file in Python

I'm making a dictionary where the keys are tuples of two consecutive words in a txt file and the value for each key is a list of words that were found directly following the key. For example,

>>> with open('alice.txt') as f: 
... d = associated_words(f) 
>>> d[('among', 'the')] 
>>> ['people', 'party.', 'trees,', 'distant', 'leaves,', 'trees', 'branches,', 'bright']

My code so far is below but it's not yet complete. Could someone help on this?

def associated_words(f):
    from collections import defaultdict    
    d = defaultdict(list)
    with open('alice.txt', 'r') as f:
        lines = f.read().replace('\n', '') 

    a, b, c = [], [], []     
    lines.replace(",", "").replace(".", "")
    lines = line.split(" ")
    for (i, word) in enumerate(lines):
        d['something to replace'].append(lines[i+2]) 

something like this? (should be easily adaptable...)

from pathlib import Path
from collections import defaultdict

DATA_PATH = Path(__file__).parent / '../data/alice.txt'

def next_word(fh):
    '''
    a generator that returns the next word from the file; with special
    characters removed; lower case.
    '''
    transtab = str.maketrans(',.`:;()?!—', '          ') # replace unwanted chars
    for line in fh.readlines():
        for word in line.translate(transtab).split():
            yield word.lower()

def handle_triplet(dct, triplet):
    '''
    add a triplet to the dictionary dct
    '''
    dct[(triplet[0], triplet[1])].append(triplet[2])

dct = defaultdict(list) # dictionary that defaults to []

with DATA_PATH.open('r') as fh:
    generator = next_word(fh)
    triplet = (next(generator), next(generator),  next(generator))
    handle_triplet(dct, triplet)
    for word in generator:
        triplet = (triplet[1], triplet[2], word)
        handle_triplet(dct, triplet)

print(dct)

output (excerpt...; not run on the whole text)

defaultdict(<class 'list'>, {
    ('enough', 'under'): ['her'], ('rattle', 'of'): ['the'],
    ('suppose', 'they'): ['are'], ('flung', 'down'): ['his'],
    ('make', 'with'): ['the'], ('ring', 'and'): ['begged'],
    ('taken', 'his'): ['watch'], ('could', 'show'): ['you'],
    ('said', 'tossing'): ['his'], ('a', 'bottle'): ['marked', 'they'],
    ('dead', 'silence'): ['instantly', 'alice', "'it's"], ...

Assuming you file looks like this

each them theirs tree life what not hope

Code:

lines = [line.strip().split(' ') for line in open('test.txt')]

d = {}
for each in lines:
    d[(each[0],each[1])] = each[2:]
print d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM