Reputation: 13
So I have a dictionary that was made from reading a file and making a key for each word found in that file with the value being a set of line numbers that the word occurs on. Here is an example of the dictionary from a file.
{'we': {4}, 'created': {4}, 'into': {2}, 'cant': {6}, 'imagination': {3}, 'with': {4}, 'nature': {2}, 'genius': {7}, 'gravity': {6}, 'of': {1, 3, 5}, 'rather': {1}, 'has': {7}, 'difference': {7}, 'try': {1}, 'better': {2}, 'used': {4}, 'value': {1}, 'between': {7}, 'blame': {6}, 'problems': {4}, 'is': {3, 7}, 'everything': {2}, 'not': {1, 3}, 'to': {1}, 'intelligence': {3}, 'thinking': {4}, 'them': {4}, 'deep': {2}, 'become': {1}, 'falling': {6}, 'for': {6}, 'character': {5}, 'when': {4}, 'will': {2}, 'solve': {4}, 'limits': {7}, 'same': {4}, 'weakness': {5}, 'and': {2, 7}, 'but': {1, 3}, 'love': {6}, 'knowledge': {3}, 'understand': {2}, 'then': {2}, 'man': {1}, 'our': {4}, 'attitude': {5}, 'in': {6}, 'the': {3, 4, 7}, 'that': {7}, 'sign': {3}, 'look': {2}, 'stupidity': {7}, 'cannot': {4}, 'its': {7}, 'true': {3}, 'success': {1}, 'becomes': {5}, 'you': {2, 6}}
What I need to do is take user entered space separated words (that I made into a list) and search the dictionary for the intersection of lines that they are all on. For example if the user enters "the" then it would return 3, 4, 7 and if they entered "the is" would return 3, 7.
Here is what I have come up with so far just trying to get it to work for 1 word:
inp_lst = inp_str.strip().split()
print("The co-occurance for: " + ", ".join(inp_lst))
for word in inp_lst:
word = word.strip().strip(string.punctuation).lower()\
.replace("'","").replace("-","")
if word in D:
word_set = D[word]
else:
return None
cooccurance_lst = list(word_set)
return cooccurance_lst.sort()
And everything I try keeps returning None.
Upvotes: 0
Views: 231
Reputation: 57033
Let's assume uinput
is the list of user-entered words and D
is your dictionary, e.g.:
uinput = "the is".split()
Then you can go over the uinput
, use each word as a dictionary key, fetch its value, and finally take the intersection, exactly as your question's title suggests:
set.intersection(*[D[x] for x in uinput if x in D])
#{3, 7}
Upvotes: 4
Reputation: 101
I think the problem is the line if word not in D:
. In that line, you make sure any input that is in D
is deferred to the else
, thereby returning None
(I'm assuming this all takes place within a function, as that's the only place where return
statements make sense). Changing it to if word in D:
should allow you to continue debugging.
Upvotes: 0
Reputation: 3619
this is the problem :
if word not in D:
word_set = D[word]
should be
if word in D:
word_set = D[word]
Upvotes: 0