adding cs235
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
#include "Factory.h"
|
||||
//You may add #include statments here
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
Unlike all other documents provided, you may modify this document slightly (but do not change its name)
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
createMimic()
|
||||
|
||||
Creates and returns an object whose class extends MimicInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "Mimic", you might say, "return new Mimic();".
|
||||
*/
|
||||
MimicInterface* Factory::createMimic()
|
||||
{
|
||||
return NULL;//Modify this line
|
||||
}
|
||||
//=======================================================================================
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "MimicInterface.h"
|
||||
#pragma once
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
createMimic()
|
||||
|
||||
Creates and returns an object whose class extends MimicInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "Mimic", you might say, "return new Mimic();".
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
static MimicInterface* createMimic();
|
||||
};
|
||||
|
||||
//=======================================================================================
|
||||
@@ -0,0 +1,31 @@
|
||||
CXXFLAGS= -Wall -g -std=c++0x
|
||||
OBJECTS=Factory.o mimic.o dmmap.o pwnd.o #ignoreme.a
|
||||
EXE=main
|
||||
all: pwnd.o $(EXE)
|
||||
|
||||
$(EXE): $(OBJECTS)
|
||||
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
|
||||
test: test.cpp mimic.o dmmap.o
|
||||
rtest: test
|
||||
./test
|
||||
Factory.o: Factory.cpp Factory.h
|
||||
mimic.o: mimic.cpp mimic.h
|
||||
dmmap.o: dmmap.cpp dmmap.h
|
||||
pwnd.o: pwnd.c
|
||||
|
||||
run: main
|
||||
./main
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -vf $(EXE)
|
||||
@rm -vf *.1
|
||||
@rm -vf *.0
|
||||
@rm -vf test
|
||||
@rm -rvf *.dSYM
|
||||
|
||||
drun: main
|
||||
gdb ./main
|
||||
|
||||
valgrind: $(EXE)
|
||||
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
||||
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
class MimicInterface
|
||||
{
|
||||
public:
|
||||
MimicInterface(){}
|
||||
virtual ~MimicInterface(){}
|
||||
|
||||
//Part 1--------------------------------------------------------------
|
||||
/**
|
||||
* createMap
|
||||
*
|
||||
* Creates a prefix-suffix map based on the input text.
|
||||
*
|
||||
* Go through the input text and examine each group of 3 words. Refer
|
||||
* to the first two words as the "prefix" and the third word as the
|
||||
* "suffix". Create a map that associates each prefix with each suffix.
|
||||
* If you encounter a prefix that has been read already, add the new
|
||||
* suffix to the list of suffixes already associated with that prefix;
|
||||
* in this manner, each prefix can be associated with multiple
|
||||
* suffixes and even multiple copies of the same suffix. Note that
|
||||
* the input texts will only contain words separated by spaces. Also
|
||||
* note that the last two word prefix in the text should be associated
|
||||
* with the suffix "THE_END".
|
||||
*
|
||||
* @param input
|
||||
* the sample text to be mimicked
|
||||
*/
|
||||
virtual void createMap(string input) = 0;
|
||||
|
||||
/**
|
||||
* getSuffixList
|
||||
*
|
||||
* Returns the list of suffixes associated with the given prefix.
|
||||
* Returns an empty vector if the given prefix is not in the map or no
|
||||
* map has been created yet.
|
||||
*
|
||||
* @param prefix
|
||||
* the prefix to be found
|
||||
* @return a list of suffixes associated with the given prefix if the
|
||||
* prefix is found; an empty vector otherwise
|
||||
*/
|
||||
virtual vector<string> getSuffixList(string prefix) = 0;
|
||||
|
||||
//Part 2--------------------------------------------------------------
|
||||
/**
|
||||
* generateText
|
||||
*
|
||||
* Generates random text using the map created by the createMap method.
|
||||
*
|
||||
* To generate the new text, start with the first prefix that was read
|
||||
* and randomly select one of the suffixes associated with that prefix.
|
||||
* The next prefix is the second word from the previous prefix and the
|
||||
* selected suffix. Continue selecting random suffixes and building the
|
||||
* next prefix until the suffix "THE_END" is selected. The token
|
||||
* "THE_END" should not be returned as part of your generated text.
|
||||
*
|
||||
* @return random text generated using the map created with the sample
|
||||
* text; an empty string if no map has been created yet
|
||||
*/
|
||||
virtual string generateText() = 0;
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "dmmap.h"
|
||||
|
||||
dmmap::dmmap(string prefix, string suffix) {
|
||||
key = prefix;
|
||||
values.push_back(suffix);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef __DMMAP_H__
|
||||
#define __DMMAP_H__
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class dmmap {
|
||||
public:
|
||||
dmmap(string prefix, string suffix);
|
||||
string key;
|
||||
vector<string> values;
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type LengthError struct {
|
||||
Count int
|
||||
}
|
||||
|
||||
func (e LengthError) Error() string {
|
||||
return fmt.Sprintf("%d is not long enough (must be longer than 2)", e.Count)
|
||||
}
|
||||
|
||||
func parse(words []string) (map[string][]string, string, error) {
|
||||
if len(words) < 3 {
|
||||
return nil, "", LengthError{len(words)}
|
||||
}
|
||||
mapping := make(map[string][]string)
|
||||
var a, b, k, v, first_prefix string
|
||||
for i := 0; i < len(words)-2; i++ {
|
||||
a = words[i]
|
||||
b = words[i+1]
|
||||
k = fmt.Sprintf("%v %v", a, b)
|
||||
if i == 0 {
|
||||
first_prefix = k
|
||||
}
|
||||
v = words[i+2]
|
||||
mapping[k] = append(mapping[k], v)
|
||||
}
|
||||
k = fmt.Sprintf("%v %v", words[len(words)-2], words[len(words)-1])
|
||||
v = "THE_END"
|
||||
mapping[k] = append(mapping[k], v)
|
||||
return mapping, first_prefix, nil
|
||||
}
|
||||
|
||||
func generateText(mapping map[string][]string, first_prefix string) string {
|
||||
cur_prefix := first_prefix
|
||||
output := first_prefix
|
||||
for {
|
||||
suffix := mapping[cur_prefix][rand.Intn(len(mapping[cur_prefix]))]
|
||||
if suffix == "THE_END" {
|
||||
break
|
||||
}
|
||||
output += fmt.Sprintf(" %v", suffix)
|
||||
_prefix := strings.SplitN(cur_prefix, " ", 2)
|
||||
cur_prefix = fmt.Sprintf("%v %v", _prefix[1], suffix)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
instream := bufio.NewReader(os.Stdin)
|
||||
for s, err := instream.ReadBytes('\n'); err == nil; s, err = instream.ReadBytes('\n') {
|
||||
words := strings.Fields(string(s))
|
||||
mapping, first_prefix, err := parse(words)
|
||||
var generated_text string
|
||||
if err != nil {
|
||||
generated_text = err.Error()
|
||||
} else {
|
||||
generated_text = generateText(mapping, first_prefix)
|
||||
}
|
||||
fmt.Printf("%v\n\t%v\n\t%v\n", words, mapping, generated_text)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
#include "mimic.h"
|
||||
|
||||
mimic::mimic() {}
|
||||
|
||||
vector<string> parser(string input) {
|
||||
vector<string> results;
|
||||
string s;
|
||||
for(unsigned int i = 0; i < input.length(); i++) {
|
||||
char c = input[i];
|
||||
if(c != ' ') {
|
||||
s += c;
|
||||
}
|
||||
else {
|
||||
if(s != "") {
|
||||
results.push_back(s);
|
||||
s.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
if(s != "") {
|
||||
results.push_back(s);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
void mimic::add_to_dmmap(string prefix, string suffix) {
|
||||
bool add_test = true;
|
||||
for(unsigned int i = 0; i < dmmaps.size(); i++) {
|
||||
if(dmmaps[i].key == prefix) {
|
||||
add_test = false;
|
||||
dmmaps[i].values.push_back(suffix);
|
||||
}
|
||||
}
|
||||
if(add_test) {
|
||||
dmmaps.push_back(dmmap(prefix, suffix));
|
||||
}
|
||||
}
|
||||
|
||||
void mimic::createMap(string input) {
|
||||
cout << input << endl;
|
||||
vector<string> parsed_input = parser(input);
|
||||
for(unsigned int i = 0; i < parsed_input.size() - 2; i++) {
|
||||
string prefix;
|
||||
string suffix;
|
||||
prefix += parsed_input[i];
|
||||
prefix += " ";
|
||||
prefix += parsed_input[i+1];
|
||||
suffix += parsed_input[i+2];
|
||||
add_to_dmmap(prefix, suffix);
|
||||
}
|
||||
string prefix = parsed_input[parsed_input.size() - 2];
|
||||
prefix += " ";
|
||||
prefix += parsed_input[parsed_input.size() - 1];
|
||||
add_to_dmmap(prefix, "THE_END");
|
||||
}
|
||||
|
||||
vector<string> mimic::getSuffixList(string prefix) {
|
||||
for(unsigned int i = 0; i < dmmaps.size(); i++) {
|
||||
if(dmmaps[i].key == prefix) {
|
||||
return dmmaps[i].values;
|
||||
}
|
||||
}
|
||||
vector<string> v;
|
||||
return v;
|
||||
}
|
||||
|
||||
string mimic::generateText() {
|
||||
string text;
|
||||
srand (time(NULL));
|
||||
if(dmmaps.size() == 0) {
|
||||
return "";
|
||||
}
|
||||
text += dmmaps[0].key;
|
||||
text += " ";
|
||||
string prefix1 = dmmaps[0].key;
|
||||
while(true) {
|
||||
vector<string> v = getSuffixList(prefix1);
|
||||
string temp = v[rand() % v.size()];
|
||||
if(temp == "THE_END") {
|
||||
text.erase(text.find_last_not_of(" ")+1);
|
||||
return text;
|
||||
}
|
||||
else {
|
||||
text += temp;
|
||||
text += " ";
|
||||
}
|
||||
vector<string> prefix_vec = parser(prefix1);
|
||||
prefix1 = prefix_vec[1];
|
||||
prefix1 += " ";
|
||||
prefix1 += temp;
|
||||
}
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & os, mimic m) {
|
||||
os << "[";
|
||||
for(unsigned int i = 0; i < m.dmmaps.size(); i++) {
|
||||
os << "'" << m.dmmaps[i].key << "' ,";
|
||||
for(unsigned int j = 0; j < m.dmmaps[i].values.size(); j++) {
|
||||
os << m.dmmaps[i].values[j] << ", ";
|
||||
}
|
||||
}
|
||||
os << "]";
|
||||
return os;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef __MIMIC__H_
|
||||
#define __MIMIC__H_
|
||||
|
||||
#include "MimicInterface.h"
|
||||
#include "dmmap.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class mimic : public MimicInterface {
|
||||
public:
|
||||
mimic();
|
||||
vector<dmmap> dmmaps;
|
||||
void createMap(string input);
|
||||
vector<string> getSuffixList(string prefix);
|
||||
void add_to_dmmap(string prefix, string suffix);
|
||||
string generateText();
|
||||
friend ostream & operator<<(ostream & os, mimic m);
|
||||
};
|
||||
ostream & operator<<(ostream & os, mimic m);
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int usleep(useconds_t usec) {
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
I want to swing I want to boat Boat wants me
|
||||
a b c d e f g
|
||||
a b c
|
||||
1 2
|
||||
1
|
||||
|
||||
No no no, no!
|
||||
1 2 3 4 5
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "mimic.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
mimic m;
|
||||
m.createMap("I want to swing I want to boat Boat wants me");
|
||||
cout << m << endl;
|
||||
cout << "'" << m.generateText() << "'" << endl;
|
||||
// m.createMap("hello beautiful world");
|
||||
// cout << m << endl;
|
||||
// cout << m.generateText() << endl;
|
||||
}
|
||||
Reference in New Issue
Block a user