473,508 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create a dictionary from a sentence

3 New Member
given a file containing sentences separated by .,?, or !
for example the file may contain the following: hello bob. bob goes home. how can you make the above into:
{'hello':{'bob':1}}
{'bob':{'hello':1,'goes':1,'home':1}}
{'goes':{'bob':1,'home':1}}
for every WORD in the file you create a dictionary containing sub dictionary in which each key represents the other words found in the same sentence as the WORD and the values is the number of times the word appears in those sentences.
because in the example above bob appears in two sentences your sub dictionary contains all the other words that appear in the same sentence and their count. this is to be done for every word in the file!
Oct 31 '10 #1
1 2799
bvdet
2,851 Recognized Expert Moderator Specialist
Have you tried to write the code yourself? We are here to help, but we cannot write it for you. Here's an example of creating a dictionary given a sentence:

Expand|Select|Wrap|Line Numbers
  1. >>> sentence = "the only way to learn how to program in Python is to write the program yourself"
  2. >>> wordList = sentence.split()
  3. >>> dd = {}
  4. >>> for word in wordList:
  5. ...     dd.setdefault(word, 0)
  6. ...     dd[word] += 1
  7. ...     
  8. >>> dd
  9. {'how': 1, 'Python': 1, 'is': 1, 'in': 1, 'yourself': 1, 'write': 1, 'to': 3, 'only': 1, 'program': 2, 'way': 1, 'learn': 1, 'the': 2}
  10. >>> 
Oct 31 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2607
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
2
2610
by: David Pratt | last post by:
Hi. I like working with lists of dictionaries since order is preserved in a list when I want order and the dictionaries make it explicit what I have got inside them. I find this combination very...
7
7891
by: ProvoWallis | last post by:
I'm still learning python so this might be a crazy question but I thought I would ask anyway. Can anyone tell me if it is possible to join two dictionaries together to create a new dictionary using...
2
1993
by: techiepundit | last post by:
I'm parsing some data of the form: OuterName1 InnerName1=5,InnerName2=7,InnerName3=34; OuterName2 InnerNameX=43,InnerNameY=67,InnerName3=21; OuterName3 .... and so on.... These are fake...
3
12590
by: hardieca | last post by:
Hi, I'm starting out with C#, and I'm wondering if someone could tell me what is the difference between Enums and dictionaries... My book doesn't even make reference to dictionaries, but while...
16
1609
by: IamIan | last post by:
Hello, I'm writing a simple FTP log parser that sums file sizes as it runs. I have a yearTotals dictionary with year keys and the monthTotals dictionary as its values. The monthTotals dictionary...
14
28274
by: erikcw | last post by:
Hi, I'm trying to turn o list of objects into a dictionary using a list comprehension. Something like entries = {} = d.id] for d in links]
4
1799
by: kdt | last post by:
Hi Trying to create a function that takes two dictionaries, and deletes key:values that are common in both dictionaries. So far I have the following; but I can only delete values in one dictionary...
14
1792
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.