diff options
author | Ta180m | 2020-05-26 13:57:23 -0500 |
---|---|---|
committer | Ta180m | 2020-05-26 13:57:23 -0500 |
commit | b3f54959a840ed70be7670b5e4d31e8e64fb24fb (patch) | |
tree | 649a409e516d7e278405888bfa78e8d8292b8b5d | |
parent | a22a1ab59bf7ed4ab7f46e722458c1e18ba29fd3 (diff) | |
parent | fd36c741cf4f19a83206635ddab2712557057a25 (diff) |
Merge branch 'master' of https://github.com/Ta180m/Compression-Project
-rw-r--r-- | src/main.cpp | 186 |
1 files changed, 96 insertions, 90 deletions
diff --git a/src/main.cpp b/src/main.cpp index 2d31702..d07fbe1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,23 +2,23 @@ #include "huffman.h"
#define pb push_back
-struct weightstruct
-{
- int word;
- int clause;
- int sentence;
- weightstruct(int a=100,int b=0,int c=0):word(a),clause(b),sentence(c){}
- weightstruct operator + (const weightstruct& w) const
- {
- return weightstruct(word+w.word,clause+w.clause,sentence+w.sentence);
- }
+struct weightstruct +{ + int word; + int clause; + int sentence; + weightstruct(int a=0,int b=0,int c=0):word(a),clause(b),sentence(c){} + weightstruct operator + (const weightstruct& w) const + { + return weightstruct(word+w.word,clause+w.clause,sentence+w.sentence); + } };
weightstruct getweight(string s)
{
if(s[0]=='$')
return weightstruct(1000,0,0);
- else if(s=="farmer"||s.length()==1||s=="example"||s=="is"||s=="are")return weightstruct(-100,0,0);
+ else if(s=="farmer"||s.length()==1||s=="example"||s=="is"||s=="are")return weightstruct(-100,0,0); else if(s=="one"||s=="two"||s=="three"||s=="four"||s=="five"||s=="six"||s=="seven"||s=="eight"||s=="nine")return weightstruct(-100,0,0);
else return weightstruct(0,0,0);
}
@@ -49,31 +49,33 @@ struct clause if(word!="") words.pb(word);
}
void calcweight()
- {
+ { + weights.clear();
for(auto& w: words)weights.pb(getweight(w));
for(auto& w: weights) weight=weight+w;
- }
- string gettext(int minw)
- {
- bool iss=true;
- string ans="";
- for(int i=0;i<words.size();i++)
- if(weights[i].word>=minw)
- {
- if(iss)
- {
- iss=false;
- ans+=words[i];
- }
- else ans+=" "+words[i];
- }
+ } + string gettext(int minw) + { + bool iss=true; + string ans=""; + for(int i=0;i<words.size();i++) + if(weights[i].word>=minw) + { + if(iss) + { + iss=false; + ans+=words[i]; + } + else ans+=" "+words[i]; + } + return ans; }
};
struct sentence
{
vector<clause> clauses;
string s;
- int weight;
+ weightstruct weight;
void parse()
{
string cl="";
@@ -109,27 +111,27 @@ struct sentence c.calcweight();
weight=weight+c.weight;
}
- }
- string gettext(int minw)
- {
- bool iss=true;
- string ans="";
- for(auto& c:clauses)
- if(c.weight.clause>=minw)
- {
- string tmp=c.gettext();
- if(tmp.length()>0)
- {
- if(iss)
- {
- iss=false;
- ans+=c.gettext();
- }
- else ans+=" "+words[i];
- }
- }
- }
- return ans;
+ } + string gettext(int minw) + { + bool iss=true; + string ans=""; + for(auto& c:clauses) + if(c.weight.clause>=minw) + { + string tmp=c.gettext(minw); + if(tmp.length()>0) + { + if(iss) + { + iss=false; + ans=ans+tmp; + } + else ans+=","+tmp; + } + + } + return ans; }
};
vector<sentence> text;
@@ -159,47 +161,51 @@ void init() sentencefile+=' ';
}
for(auto& i:text)i.parse();
-}
-int main() {
- /*init();
- for(sentence& i:text)
- {
- for(clause& j:i.clauses)
- {
- for(auto k:j.words)
- {
- cout<<k<<endl;
- }
- }
- }*/
-
- string orig = "Snow has arrived on the farm, and as she does at the beginning of every winter, Bessie is building a snow-cow! Most of the time, Bessie strives to make her sculpture look as much like a real cow as possible. However, feeling artistically inspired, this year she decides to pursue a more abstract route and build a sculpture in the shape of a tree, consisting of N snowballs (1<=N<=105) connected by N-1 branches, each connecting a pair of snowballs such that there is a unique path between every pair of snowballs. Bessie has added a nose to one of the snowballs, so it represents the head of the abstract snow cow. She designates it as snowball number 1. To add more visual interest, she plans to dye some of the snowballs different colors in an artistic fashion by filling old milk pails with colored dye and splashing them onto the sculpture. Colors are identified by integers in the range 1...105, and Bessie has an unlimited supply of buckets filled with dyes of every possible color. When Bessie splashes a snowball with a bucket of dye, all the snowballs in its subtree are also splashed with the same dye (snowball y is in the subtree of snowball x if x lies on the path from y to the head snowball). By splashing each color with great care, Bessie makes sure that all colors a snowball has been splashed with will remain visible. For example, if a snowball had colors [1,2,3] and Bessie splashes it with color 4, the snowball will then have colors [1,2,3,4]. After splashing the snowballs some number of times, Bessie may also want to know how colorful a part of her snow-cow is. The \"colorfulness\" of a snowball x is equal to the number of distinct colors c such that snowball x is colored c. If Bessie asks you about snowball x, you should reply with the sum of the colorfulness values of all snowballs in the subtree of x. Please help Bessie find the colorfulness of her snow-cow at certain points in time.";
- vector<bool> enc = huffman::encode(orig);
- string dec = huffman::decode(enc);
- if (orig != dec) {
- cout << "Not a match!\n";
- }
- else {
- cout << dec << '\n';
- for (auto b : enc) cout << b;
- cout << '\n';
- cout << "Original length: " << orig.size() << '\n';
- cout << "Compressed length: " << (enc.size() + 7) / 8 << '\n';
- cout << "Percent compression: " << 1.0 - (double)(enc.size() + 7) / 8 / orig.size() << '\n';
- }
+} + +string gettextstring(int minw) +{ + bool iss=true; + string ans=""; + for(auto& c:text) + if(c.weight.sentence>=minw) + { + string tmp=c.gettext(minw); + if(tmp.length()>0) + { + if(iss) + { + iss=false; + ans=ans+tmp; + } + else ans+="."+tmp; + } + + } + return ans; +} - /*string orig = "Snow has arrived on the farm, and as she does at the beginning of every winter, Bessie is building a snow-cow! Most of the time, Bessie strives to make her sculpture look as much like a real cow as possible. However, feeling artistically inspired, this year she decides to pursue a more abstract route and build a sculpture in the shape of a tree, consisting of N snowballs (1<=N<=105) connected by N-1 branches, each connecting a pair of snowballs such that there is a unique path between every pair of snowballs. Bessie has added a nose to one of the snowballs, so it represents the head of the abstract snow cow. She designates it as snowball number 1. To add more visual interest, she plans to dye some of the snowballs different colors in an artistic fashion by filling old milk pails with colored dye and splashing them onto the sculpture. Colors are identified by integers in the range 1...105, and Bessie has an unlimited supply of buckets filled with dyes of every possible color. When Bessie splashes a snowball with a bucket of dye, all the snowballs in its subtree are also splashed with the same dye (snowball y is in the subtree of snowball x if x lies on the path from y to the head snowball). By splashing each color with great care, Bessie makes sure that all colors a snowball has been splashed with will remain visible. For example, if a snowball had colors [1,2,3] and Bessie splashes it with color 4, the snowball will then have colors [1,2,3,4]. After splashing the snowballs some number of times, Bessie may also want to know how colorful a part of her snow-cow is. The \"colorfulness\" of a snowball x is equal to the number of distinct colors c such that snowball x is colored c. If Bessie asks you about snowball x, you should reply with the sum of the colorfulness values of all snowballs in the subtree of x. Please help Bessie find the colorfulness of her snow-cow at certain points in time.";
- vector<bool> enc = huffman::encode(orig);
- string dec = huffman::decode(enc);
- if (orig != dec) {
- cout << "Not a match!\n";
- }
- else {
- cout << dec << '\n';
- for (auto b : enc) cout << b;
- cout << '\n';
- cout << "Original length: " << orig.size() << '\n';
- cout << "Compressed length: " << (enc.size() + 7) / 8 << '\n';
- cout << "Percent compression: " << 1.0 - (double)(enc.size() + 7) / 8 / orig.size() << '\n';
+int main() {
+ init();
+ for(auto& x:text) + x.calcweight(); + cout<<gettextstring(-10000);
+ + + + // WARNING: Huffman will CRASH if you pass a string with only one unique /*aracter
+ /*string orig = "test1234"; + vector<bool> enc = huffman::encode(orig); + string dec = huffman::decode(enc); + if (orig != dec) { + cout << "Not a match!\n"; + } + else { + cout << dec << '\n'; + for (auto b : enc) cout << b; + cout << '\n'; + cout << "Original length: " << orig.size() << '\n'; + cout << "Compressed length: " << (enc.size() + 7) / 8 << '\n'; + cout << "Percent compression: " << 100.0 - (double)100.0 * (enc.size() + 7) / 8 / orig.size() << "%\n"; }*/
}
\ No newline at end of file |