473,657 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting frequency of words in a text file using STL containers

How can I count the frequency of words in a ASCII File using STL?
a) I dont know what words will be found in the file
b) The max number of occurrences is 10,000 per word (in case that
matters)

Can anyone give me an example program and compare/contrast the use of
various STL containers for this problem?

Feb 8 '07 #1
8 4289
On Feb 8, 11:14 am, "writean...@gma il.com" <ekr...@gmail.c omwrote:
How can I count the frequency of words in a ASCII File using STL?
a) I dont know what words will be found in the file
b) The max number of occurrences is 10,000 per word (in case that
matters)

Use a std::map<std::s tring, int>. Look up std::map in your
coursebook, and it should be fairly obvious how to do this. If you
have trouble, post some code and explain the problem you're having.

Best regards,

Tom
Feb 8 '07 #2
Tom,

THank you. When the word already exists in the map, then I can use freq
++ in the "int" part of the map.
My problem is, when I find a new word, there is no existing entry, so
I cant use (freq++) in the "int" part of the map.
Can u give an example for the above?

Thanks
On Feb 8, 9:18 am, "Thomas Tutone" <Thomas8675...@ yahoo.comwrote:
On Feb 8, 11:14 am, "writean...@gma il.com" <ekr...@gmail.c omwrote:
How can I count the frequency of words in a ASCII File using STL?
a) I dont know what words will be found in the file
b) The max number of occurrences is 10,000 per word (in case that
matters)

Use a std::map<std::s tring, int>. Look up std::map in your
coursebook, and it should be fairly obvious how to do this. If you
have trouble, post some code and explain the problem you're having.

Best regards,

Tom

Feb 8 '07 #3
On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "wr********@gma il.com"
<ek****@gmail.c omwrote,
>THank you. When the word already exists in the map, then I can use freq
++ in the "int" part of the map.
My problem is, when I find a new word, there is no existing entry, so
I cant use (freq++) in the "int" part of the map.
Why not? Just use operator[] and let it create a new entry.
Feb 8 '07 #4
On Feb 8, 10:05 am, David Harmon <sou...@netcom. comwrote:
On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "writean...@gma il.com"
<ekr...@gmail.c omwrote,
THank you. When the word already exists in the map, then I can use freq
++ in the "int" part of the map.
My problem is, when I find a new word, there is no existing entry, so
I cant use (freq++) in the "int" part of the map.

Why not? Just use operator[] and let it create a new entry.
How Though? Sorry for my ignorance, I need a simple example since I am
new to this :(

Feb 8 '07 #5
On Feb 8, 12:10 pm, "writean...@gma il.com" <ekr...@gmail.c omwrote:
On Feb 8, 10:05 am, David Harmon <sou...@netcom. comwrote:
On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "writean...@gma il.com"
<ekr...@gmail.c omwrote,
>THank you. When the word already exists in the map, then I can use freq
>++ in the "int" part of the map.
>My problem is, when I find a new word, there is no existing entry, so
>I cant use (freq++) in the "int" part of the map.
Why not? Just use operator[] and let it create a new entry.

How Though? Sorry for my ignorance, I need a simple example since I am
new to this :(
Suppose you have a std::map<std::s tring, intcalled wordCount.
Suppose the next word you have is stored in a std::string called
word. Suppose that word is currently equal to "the". Now, you want
to increment its count accordingly. Then just do this:

++wordCount[word];

This will work whether word's value previously existed in wordCount or
not.

If you have more problems, post them.

Best regards,

Tom

Feb 8 '07 #6
In article <11************ **********@a75g 2000cwd.googleg roups.com>,
"wr********@gma il.com" <ek****@gmail.c omwrote:
On Feb 8, 10:05 am, David Harmon <sou...@netcom. comwrote:
On 8 Feb 2007 08:25:40 -0800 in comp.lang.c++, "writean...@gma il.com"
<ekr...@gmail.c omwrote,
>THank you. When the word already exists in the map, then I can use freq
>++ in the "int" part of the map.
>My problem is, when I find a new word, there is no existing entry, so
>I cant use (freq++) in the "int" part of the map.
Why not? Just use operator[] and let it create a new entry.

How Though? Sorry for my ignorance, I need a simple example since I am
new to this :(
int main() {
map<string, intfreq;
++freq["hello"];
assert( freq["hello"] == 1 );
}
Feb 8 '07 #7
wr********@gmai l.com wrote:
Tom,
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.c om/c++-faq-lite/how-to-post.html>
Feb 8 '07 #8
On Feb 8, 3:01 pm, "Default User" <defaultuse...@ yahoo.comwrote:
writean...@gmai l.com wrote:
Tom,

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.c om/c++-faq-lite/how-to-post.html>
*plonk*

Feb 8 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3101
by: Simon Verona | last post by:
Not sure if this is the best group... it may be better off in one of the ADO groups, but I'm sure somebody here knows the answer: I'm trying to load up a text file using ADO.net, as follows: Dim TextConnectionString As String TextConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & FilePath & ";" & _
2
4481
by: Praveen_db2 | last post by:
Hi all Db2 8.1.3 windows Is there any way to write data into a text file using a stored procedure? The way we return a cursor output to the calling application, can we return data in a text file? Regards Praveen
2
17443
by: asenthil | last post by:
Hai, i'm a beginner to java... just now i had tried to read and write files using java... and then i had tried to connect a database using jdbc... now i want to export the data's from a database into a text file
4
7874
by: varsha desai | last post by:
Hello there, I want to change some data(which is in one line only) of text file using VB 6.0. Which is the best method for it? Another question is I want to delete last two, three lines of text file using VB 6.0. Which method can be best method for it?
1
8786
by: neveen | last post by:
i want to open and read text file using j2me that can run on mobile 6630 then i want to make button called read that when pressed the data inside text display
2
2606
by: charlesbritto | last post by:
A C++ program for counting individual string in a text file using file handling functions., for ex: if the text file contains, am a boy,am studying +2,am from chennai Now the result shoud be as 3 bcaz ("am" presenting 3 times)
2
3199
by: sajidali | last post by:
i am trying to append a text file using c#. when i executes my application i writes to the text file using more then one functions. i want to append file only during execution of programe. next time when i execute programe it should overwrite the file. plz help me in this regard.
4
4240
by: kshw | last post by:
Hi, I'm trying to remove non-stop words from a text file using regular expresions but it is not working. I used something like ('^?or') in order to avoid removing (or) from the mibble of words e.g. morning. Temp = Original_File = open('out.txt', 'r') Original_File_Content = Original_File.read() Original_File.close() Temp.append("".join(Original_File_Content)) FileString = "".join(Temp)
0
1331
by: Thiem Teddy | last post by:
Dear all I use VB6 to export data into xls or text file, I followed this example ( http://bytes.com/topic/visual-basic/answers/530866-how-export-table-data-into-xls-text-file-using-vb6 ). but I don't know what kind of libraries that i need to add into this program because i'm a new in VB6 may you tell me how many libraries i need to add into this program. Thanks ThiemTeddy
0
8302
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8601
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7314
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5630
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1601
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.