Ejemplo n.º 1
0
QList<QString> Dialog::ReadWords()
{
    QList<QString> wordList;
    QString filename=":files/words.txt";
    QFile wordFile(filename);
    wordFile.open(QFile::ReadOnly | QFile::Text);
    QTextStream in(&wordFile);
    QString word;
    while(!wordFile.atEnd())
    {
        in>>word;
        wordList.append(word);
    }
    wordList.append(word);
    wordFile.close();
    return wordList;
}
Ejemplo n.º 2
0
int main(int argc, char** argv)
{

	char * HMMFile = argv[1];
	char * vocabFile = argv[2];
	int nearestK = atoi(argv[3]);
	ifstream wordFile(argv[4]);
	map<string, int> stringToIntvocabMap;
	map<int, string> intToStringvocabMap;
	map<int, int> wordCount;
	loadVocabFromFile(vocabFile, &stringToIntvocabMap, &intToStringvocabMap,
			&wordCount);

	HMM h(argv[1]);
	string line;
	while (getline(wordFile, line))
	{
		int word = stringToIntvocabMap[line];
		vector < pair<int, double> > neighbors;
		for (int neighbor = 0; neighbor < h.getNumObs(); neighbor++)
		{
			if (neighbor != word)
			{
				double distance = jsDivergence(h.condstate_dist[word],
						h.condstate_dist[neighbor], h.getNumStates());
				neighbors.push_back(pair<int, double> (neighbor, distance));
			}
		}
		std::sort(neighbors.begin(), neighbors.end(), sort_scorepairLESSTHAN);
		cout << intToStringvocabMap[word] << " " << wordCount[word] << endl;
		for (int i = 0; i < nearestK; i++)
		{
			cout << intToStringvocabMap[neighbors[i].first] << " "
					<< neighbors[i].second << " ";
		}
		cout << endl;

	}
	wordFile.close();

	return 0;
}
Ejemplo n.º 3
0
Server::Server(unsigned short port, std::string wordFilename)
: m_port(port)
, m_running(false)
, m_wordFilename(wordFilename)
, m_mode(Wait)
, m_currentDrawer(nullptr)
{
	std::ifstream wordFile(wordFilename);
	std::string line;
	if (wordFile.is_open()) {
		while (std::getline(wordFile, line)) {
			std::transform(line.begin(), line.end(), line.begin(), ::tolower);
			m_wordList.push_back(line);
		}
		wordFile.close();
	} else {
		cpp3ds::err() << "Failed to open word file: " << wordFilename << std::endl;
	}

	clearDrawData();
}
WordList::WordList(int wordLength)
{
	std::ifstream wordFile(wordFileName);
	std::string word;

	// Read past the copyright notice at the top of words.txt
	while (std::getline(wordFile, word) && word != "---")
	{
		// do nothing
	}

	// Read each line in the file
	while (std::getline(wordFile, word))
	{
		if (word.length() == wordLength)
		{
			// Scan through the line, converting each character to upper case.
			// If a non-alphabet character is encountered, reject the word.
			bool isValid = true;
			for (int i = 0; i < word.length(); i++)
			{
				if (isalpha(word[i]))
				{
					word[i] = toupper(word[i]);
				}
				else
				{
					isValid = false;
					break;
				}
			}

			// If it's a good word, add it to the list.
			if (isValid)
			{
				words.push_back(word);
			}
		}
	}
}
Ejemplo n.º 5
0
int main(int argc, char* argv[]) {
    struct stat filestatus;
    stat(argv[1], &filestatus);

    buffer = new char[filestatus.st_size + 1];

    std::ifstream wordFile(argv[1]);
    if (wordFile.is_open()) {
        char word[MAX_WORD_LENGTH];

        while (wordFile.good()) {
            wordFile.getline(word, MAX_WORD_LENGTH);
            if (wordFile.gcount() != 0) {
                trie.add(word);
            }
        }
        wordFile.close();
    }

    char rootWord[] = "hello";
    trie.add(rootWord);
    checkWord(&trie, rootWord, strlen(rootWord));

    Trie* node;
    char slate[MAX_WORD_LENGTH + 1];
    while (!queue.empty()) {
        char* word = queue.front();
        queue.pop();

        int len = strlen(word);

        // Edits
        char* chars = word;
        node = &trie;
        int i = 0;
        while (word[i] != '\0') {
            char original = chars[i];
            for (char c = 'a'; c < original; ++c) {
                if (node->children[c - 'a'] != NULL) {
                    chars[i] = c;
                    checkWord(node, chars, len, i);
                }

            }
            // Skip original
            for (char c = original + 1; c <= 'z'; ++c) {
                if (node->children[c - 'a'] != NULL) {
                    chars[i] = c;
                    checkWord(node, chars, len, i);
                }
            }
            chars[i] = original;

            node = node->children[original - 'a'];
            ++i;
        }

        // Inserts
        chars = slate;
        chars[len + 1] = '\0';
        node = &trie;
        int idx = -2;
        for (i = 0; i <= len; ++i) {
            if (++idx >= 0)
                chars[idx] = word[idx];
            for (int j = i + 1; j <= len; ++j)
                chars[j] = word[j - 1];
            for (char c = 'a'; c <= 'z'; ++c) {
                if (node->children[c - 'a'] != NULL) {
                    chars[i] = c;
                    checkWord(node, chars, len + 1, i);
                }
            }

            node = node->children[word[i] - 'a'];
        }

        // Deletes
        chars = slate;
        chars[len - 1] = '\0';
        idx = -2;
        for (i = 0; i < len; ++i) {
            if (++idx >= 0)
                chars[idx] = word[idx];
            for (int j = i + 1; j < len; ++j)
                chars[j - 1] = word[j];

            checkWord(&trie, chars, len - 1);
        }
    }

    // Don't include initial word in count
    std::cout << numVisited - 1 << std::endl;
    return 0;
}