Primo commit

main
Isabella Inuso 2 years ago
commit 3012e01168

13
.gitignore vendored

@ -0,0 +1,13 @@
.vscode/
# Dati
*.zip
wiki*/
# Test
*Test*
*.exe
# Eseguibili
Progetto

@ -0,0 +1,128 @@
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <stdint.h>
#include <numeric>
#include <bits/stdc++.h>
#include "aux.cpp"
using namespace std;
struct Edge {
string from, to;
int weight;
// void print() {
// cout << from << " --> " << to << " w: " << weight << endl;
// }
};
bool compareEdge(Edge e1, Edge e2) {
return (e1.weight > e2.weight);
}
class UndirectedWeightedGraph {
private:
void addWeightedEdge(string from, string to, int weight) {
for (Edge& edge : AdjList[from]) {
if (to == edge.to) {
edge.weight += weight;
return;
}
}
Edge newEdge;
newEdge.from = from;
newEdge.to = to;
newEdge.weight = weight;
AdjList[from].push_back(newEdge);
}
public:
unordered_map<string, vector<Edge>> AdjList;
void addEdge(string node1, string node2) {
addWeightedEdge(node1, node2, 1);
addWeightedEdge(node2, node1, 1);
}
// void print() {
// for (auto& pair : AdjList) {
// for (Edge& edge : pair.second) {
// edge.print();
// }
// }
// }
bool checkComb(vector<Edge> star) {
auto it = star.begin();
auto v = AdjList[(*it).to];
unordered_set<string> acc;
for (auto& e : v) {
acc.insert(e.to);
}
it++;
for (; it != star.end(); it++) {
auto v = AdjList[(*it).to];
unordered_set<string> s;
for (auto& e : v) {
s.insert(e.to);
}
acc = intersectSets(acc,s);
}
return acc.size() == 1;
}
vector<vector<Edge>> findSol(int c, int k) {
vector<vector<Edge>> Sol;
vector<string> keys = getShuffleKeys(AdjList);
for (auto& key : keys) {
auto node = AdjList[key];
if (c == 0)
break;
if (node.size() >= k) {
sort(node.begin(), node.end(), compareEdge);
vector<int> indices(k);
iota(indices.begin(), indices.end(), 0);
do {
vector<Edge> subV;
for (int i : indices)
subV.push_back(node[i]);
if (checkComb(subV)) {
c--;
Sol.push_back(subV);
break;
}
} while(!nextComb(indices, node.size()));
}
}
return Sol;
}
void printSol(vector<vector<Edge>> Sol) {
for (auto& edges : Sol) {
cout << edges[0].from << ":" << " ";
for (auto& edge : edges) {
cout << edge.to << " ";
}
cout << endl;
}
}
};

@ -0,0 +1,54 @@
#include <sys/stat.h>
#include "populateGraph.cpp"
using namespace std;
#define ERR(msg) {fprintf(stderr, "%s\n", msg); exit(2);}
int main(int argc,char const *argv[]) {
int k = 5;
int count = 1;
vector<string> Files;
// configurazione opzioni
for (int i = 1; i < argc; i++) {
string arg(argv[i]);
if (arg == "-c") {
i++;
count = atoi(argv[i]);
}
else if (arg == "-k") {
i++;
k = atoi(argv[i]);
}
else {
Files.push_back(arg);
}
}
UndirectedWeightedGraph graph;
for (string File : Files) {
ifstream s(File);
if (!s) ERR("errore di apertura del file")
stringstream buffer;
buffer << s.rdbuf();
//parsing del file
vector<phrase> phrases = parseFile(buffer.str());
//popolazione del grafo
populateGraph(&graph, phrases);
}
vector<vector<Edge>> Sol = graph.findSol(count, k);
graph.printSol(Sol);
return 0;
}

@ -0,0 +1,6 @@
# Progetto di ASD di Isabella
## Usage
$ g++ Progetto.cpp -o Progetto
$ ./Progetto -k 5 -c 10

@ -0,0 +1,59 @@
#pragma once
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
bool nextComb(vector<int> &indices, int n) {
int k = indices.size();
int i = n - 1;
int j = k - 1;
while (indices[j] == i) {
if (j == 0)
return 1;
i--;
j--;
}
indices[j]++;
for (int l = 0; l <= k - j - 1; l++) {
indices[j + l] = indices[j] + l;
}
return 0;
}
template <typename S>
unordered_set<S> intersectSets(unordered_set<S> s1, unordered_set<S> s2) {
unordered_set<S> s;
if (s1.size() <= s2.size()) {
for (auto &el : s1) {
if (s2.find(el) != s2.end()) {
s.insert(el);
}
}
}
else {
for (auto &el : s2) {
if (s1.find(el) != s1.end()) {
s.insert(el);
}
}
}
return s;
}
template <typename K, typename V>
vector<K> getShuffleKeys(unordered_map<K, V> map) {
srand(time(NULL));
vector<K> output;
for (auto& pair : map) {
output.push_back(pair.first);
}
random_shuffle(output.begin(), output.end());
return output;
}

@ -0,0 +1,21 @@
#pragma once
#include "reader.cpp"
#include "Graph.cpp"
using namespace std;
void populateGraph(UndirectedWeightedGraph* graph, vector<phrase> phrases) {
// prendo una parola nel periodo e aggiungo al grafo un arco con la parola dopo e quella dopo ancora se esistono, poi passo al periodo successivo
for (phrase phrase : phrases) {
for (int i = 0; i < phrase.size(); i++) {
if (i+1 < phrase.size()) {
graph->addEdge(phrase[i], phrase[i+1]);
}
// if (i+2 < phrase.size()) {
// graph->addEdge(phrase[i], phrase[i+2]);
// }
}
}
}

@ -0,0 +1,88 @@
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <stdint.h>
#include <fstream>
using namespace std;
typedef vector<string> phrase;
bool isWordChar(char a) {
return (('a' <= a && a <= 'z') || ('A' <= a && a <= 'Z') || ('0' <= a && a <= '9') || a == '@');
}
bool isPhraseBreak(char a) {
return ((a == '!') || (a == ',') || (a == '.') || (a == ':') || (a == ';') || (a == '?') || (a == '(') || (a == ')') || (a == '/') || (a == '\\') || (a == '\n') || (a == '[') || (a == ']') || (a == '_') || (a == '{') || (a == '}') || (a == 126 /* tilde */));
}
unordered_set<string> getStopWords() {
unordered_set<string> swords;
ifstream stop_words("stop_words.txt");
string line;
while (getline(stop_words, line)) {
swords.insert(line);
}
return swords;
}
char asciitolower(char in) {
if (in <= 'Z' && in >= 'A')
return in - ('Z' - 'z');
return in;
}
vector<phrase> parseFile (string text) {
for (size_t i = 0; i < text.size(); i++)
{
text[i] = asciitolower(text[i]);
}
unordered_set<string> stop_words = getStopWords();
// prendo un vettore, raggruppo caratteri tra spazi in stringhe fino a punteggiatura, raggruppo queste stringhe in periodi che inserisco in un nuovo vettore
vector<phrase> phrases;
phrase phrase;
string word;
for (int i = 0; i < text.size(); i++) {
if (isWordChar(text[i])) {
word.append(1, text[i]);
}
else if (isPhraseBreak(text[i])) {
if (word.size() > 0 && stop_words.find(word) == stop_words.end()) {
phrase.push_back(word);
}
word = "";
if (phrase.size() > 0) {
phrases.push_back(phrase);
phrase.clear();
}
}
else {
if (word.size() > 0 && stop_words.find(word) == stop_words.end()) {
phrase.push_back(word);
}
word = "";
}
}
if (word.size() > 0 && stop_words.find(word) == stop_words.end()) {
phrase.push_back(word);
}
if (phrase.size() > 0) {
phrases.push_back(phrase);
phrase.clear();
}
word = "";
return phrases;
}

@ -0,0 +1,665 @@
a
able
about
above
abst
accordance
according
accordingly
across
act
actually
added
adj
affected
affecting
affects
after
afterwards
again
against
ah
all
almost
alone
along
already
also
although
always
am
among
amongst
an
and
announce
another
any
anybody
anyhow
anymore
anyone
anything
anyway
anyways
anywhere
apparently
approximately
are
aren
arent
arise
around
as
aside
ask
asking
at
auth
available
away
awfully
b
back
be
became
because
become
becomes
becoming
been
before
beforehand
begin
beginning
beginnings
begins
behind
being
believe
below
beside
besides
between
beyond
biol
both
brief
briefly
but
by
c
ca
came
can
cannot
can't
cause
causes
certain
certainly
co
com
come
comes
contain
containing
contains
could
couldnt
d
date
did
didn't
different
do
does
doesn't
doing
done
don't
down
downwards
due
during
e
each
ed
edu
effect
eg
eight
eighty
either
else
elsewhere
end
ending
enough
especially
et
et-al
etc
even
ever
every
everybody
everyone
everything
everywhere
ex
except
f
far
few
ff
fifth
first
five
fix
followed
following
follows
for
former
formerly
forth
found
four
from
further
furthermore
g
gave
get
gets
getting
give
given
gives
giving
go
goes
gone
got
gotten
h
had
happens
hardly
has
hasn't
have
haven't
having
he
hed
hence
her
here
hereafter
hereby
herein
heres
hereupon
hers
herself
hes
hi
hid
him
himself
his
hither
home
how
howbeit
however
hundred
i
id
ie
if
i'll
im
immediate
immediately
importance
important
in
inc
indeed
index
information
instead
into
invention
inward
is
isn't
it
itd
it'll
its
itself
i've
j
just
k
keep keeps
kept
kg
km
know
known
knows
l
largely
last
lately
later
latter
latterly
least
less
lest
let
lets
like
liked
likely
line
little
'll
look
looking
looks
ltd
m
made
mainly
make
makes
many
may
maybe
me
mean
means
meantime
meanwhile
merely
mg
might
million
miss
ml
more
moreover
most
mostly
mr
mrs
much
mug
must
my
myself
n
na
name
namely
nay
nd
near
nearly
necessarily
necessary
need
needs
neither
never
nevertheless
new
next
nine
ninety
no
nobody
non
none
nonetheless
noone
nor
normally
nos
not
noted
nothing
now
nowhere
o
obtain
obtained
obviously
of
off
often
oh
ok
okay
old
omitted
on
once
one
ones
only
onto
or
ord
other
others
otherwise
ought
our
ours
ourselves
out
outside
over
overall
owing
own
p
page
pages
part
particular
particularly
past
per
perhaps
placed
please
plus
poorly
possible
possibly
potentially
pp
predominantly
present
previously
primarily
probably
promptly
proud
provides
put
q
que
quickly
quite
qv
r
ran
rather
rd
re
readily
really
recent
recently
ref
refs
regarding
regardless
regards
related
relatively
research
respectively
resulted
resulting
results
right
run
s
said
same
saw
say
saying
says
sec
section
see
seeing
seem
seemed
seeming
seems
seen
self
selves
sent
seven
several
shall
she
shed
she'll
shes
should
shouldn't
show
showed
shown
showns
shows
significant
significantly
similar
similarly
since
six
slightly
so
some
somebody
somehow
someone
somethan
something
sometime
sometimes
somewhat
somewhere
soon
sorry
specifically
specified
specify
specifying
still
stop
strongly
sub
substantially
successfully
such
sufficiently
suggest
sup
sure t
take
taken
taking
tell
tends
th
than
thank
thanks
thanx
that
that'll
thats
that've
the
their
theirs
them
themselves
then
thence
there
thereafter
thereby
thered
therefore
therein
there'll
thereof
therere
theres
thereto
thereupon
there've
these
they
theyd
they'll
theyre
they've
think
this
those
thou
though
thoughh
thousand
throug
through
throughout
thru
thus
til
tip
to
together
too
took
toward
towards
tried
tries
truly
try
trying
ts
twice
two
u
un
under
unfortunately
unless
unlike
unlikely
until
unto
up
upon
ups
us
use
used
useful
usefully
usefulness
uses
using
usually
v
value
various
've
very
via
viz
vol
vols
vs
w
want
wants
was
wasnt
way
we
wed
welcome
we'll
went
were
werent
we've
what
whatever
what'll
whats
when
whence
whenever
where
whereafter
whereas
whereby
wherein
wheres
whereupon
wherever
whether
which
while
whim
whither
who
whod
whoever
whole
who'll
whom
whomever
whos
whose
why
widely
willing
wish
with
within
without
wont
words
world
would
wouldnt
www
x
y
yes
yet
you
youd
you'll
your
youre
yours
yourself
yourselves
you've
z
zero
Loading…
Cancel
Save