-
March 26th, 2013, 09:02 AM
#1
Need help with reading and outputting files
Hi everyone, I have got an assignment. It is about comparing two files one file has sentences and the other abbreviations. so far i have managed to compare the abbreviations which appear in the sentence and erase punctuation linked to abbreviations.
However I am currently stuck on putting the punctuation back in when outputting the file. I am also stuck with replacing matching words in the sentences with the expanded abbreviations. Any help would be greatly appreciated. Not sure which code to show. but sentences and abbreviations are below.
ABBREVIATIONS
Aberd Aberdeen
admin administration
approx approximate
Austral Australia
div division
Capt Captain
Comdr Commander
e east
HQ Head Quarters
m metres
mil million
mt Mount
n north
NATO North Atlantic Treaty Organisation
NSW New South Wales
pop population
s south
sq square
w west
Text file
NATO troops were on exercise in Aberd Capt Jones of the first div told Comdr Frank that his troops were near the E flank of the NATO forces. Complaining about the amount of Admin, Capt Jones radioed the NATO HQ asked for navigation relative to the HQ. The Captain then left.
For example the abbreviation "Aberd" in the sentence above needs to be replaced with Aberdeen from the abbreviations file. I need the last part of the abbreviation not "Aberd Aberdeen" need just "Aberdeen". Hope the info is understandable.
Any help will be greatly appreciated.
-
March 26th, 2013, 09:27 AM
#2
Re: Need help with reading and outputting files
the way I took out punctuation:
CODE::
while(!text1.eof())
----{
-----------text1 >> str;
-----------text1Vector.push_back(str);
-----------for(int j = 0;j < text1Vector.size(); j++)
-----------{
--------------string& str = text1Vector[j];
--------------for(int k = 0; k < str.length(); k++)
--------------{
------------------if (ispunct(str[k]))
------------------{
---------------------str.erase(k, 1);
------------------}
--------------}
-----------}
-------cout << str << " ";
-----}
Last edited by abub93; March 26th, 2013 at 09:30 AM.
-
March 26th, 2013, 09:42 AM
#3
Re: Need help with reading and outputting files
I'm not following how punctuation applies to this problem, given the examples you provided.
If it were me, I'd create a map of the abbreviation to the unabbreviated words.. I'd parse the sentence, look up each word in the map, and replace it if it were found in the map. If you don't take out the punctuation, you don't need to put it back.
Also, your remove punctuation routine won't work if there are consecutive punctuation characters.
-
March 26th, 2013, 09:45 AM
#4
Re: Need help with reading and outputting files
^^^Thanks for your reply. Punctuation is a separate problem not the same sorry if that wasn't clear. And for map/parse, I dont know how to do them things I'm just a beginner, know the basics
-
March 26th, 2013, 10:01 AM
#5
Re: Need help with reading and outputting files
Victor Nijegorodov
-
March 26th, 2013, 11:32 AM
#6
Re: Need help with reading and outputting files
This is a simple example showing use of the STL map class.
Code:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
typedef map<string, string> MAP;
typedef MAP::iterator MPOS;
MAP words;
MPOS mpos;
string ab;
words["n"] = "North";
words["s"] = "South";
words["e"] = "East";
words["w"] = "West";
cout << "Enter abrev: ";
cin >> ab;
cout << "'" << ab << "' ";
if ((mpos = words.find(ab)) == words.end()) {
cout << "not found" << endl;
} else {
cout << "maps to '" << mpos->second << "'" << endl;
}
return (0);
}
The words map would be initialised from your abbreviations file. Then from your sentence file for each word read find it in the word map. If the word exists in the word map, use the replacement from the map otherwise if it doesn't exist in the map use the original word.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 26th, 2013, 12:59 PM
#7
Re: Need help with reading and outputting files
Originally Posted by abub93
the way I took out punctuation:
I know you're a beginner, but I would just like to show you all that code here:
Code:
for(int k = 0; k < str.length(); k++)
{
if (ispunct(str[k]))
{
str.erase(k, 1);
}
}
can be done in one line:
Code:
#include <algorithm>
//...
str.erase(std::remove_if(str.begin(), str.end(), ::ispunct), str.end());
This eliminates mistakes such as
1) Not looping enough times
2) Looping too many times.
3) Not erasing all of the punctuations.
4) Using non-optimal methods of looping (e.g. calling length() each time in the loop).
5) Just plain old writing the loop wrong.
For 4) and 5) look at your code. You are at the same time erasing characters from str, thus making the length() shorter, but you're using str.length() as the loop terminator. Two things at once going on at the same time will lead to bugs.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 26th, 2013 at 01:04 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|