473,569 Members | 2,852 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,'ho me':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 2801
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
2612
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
2613
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 useful for storing constants especially. Generally I find myself either needing to retrieve the values of constants in an iterative way (as in my...
7
7900
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 the keys from the old dictionaries? The keys in the new dictionary would be the keys from the old dictionary one (dict1) and the values in the...
2
2003
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 names I've made up to illustrate the point more clearly. (the embedded device device can't produce XML and this is what I have
3
12611
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 programming in other languages, I used dictionaries quite often. Should I move to Enums? Thanks,
16
1617
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 has month keys and file size values. The script works except the results are written for all years, rather than just one year. I'm thinking there's...
14
28279
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
1805
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 as I am iterating over the other. Or is there a way to rename keys in dictionaries? Thanks in advance. def filterByKey(dict1, dict2): ''' ...
14
1799
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7681
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
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 we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.