阅读 dict 文件找到 "words" 并添加到 trie
reading through a dict file finding "words" and add into trie
这道题我得通读一遍,分清什么是单词。一个词不需要有意义,即。一个词可以是 asdas、sdgsgd、dog、sweet 等...要访问我必须通过映射文件来实现。
File *map, *dictfile, *datafile;
char *dictname, *dataname;
map = fopen(argv[1],"r");
while (fgets(buffer,sizeof(buffer),map) != NULL)
{
dictname = strtok(buffer," ");
dataname = strtok(NULL, " ");
strtok(dictname,"\n");
strtok(dataname,"\n");
该代码进入映射文件,然后区分文件名和文件名。
从他们那里我打开文件
if((datafile = fopen(dictname,"r")) == NULL) //error checking
{
in here I have to call a readDict(dictfile)
}
我的问题出在 readDict 中,我必须在这个字典文件中逐字逐句地区分什么是单词,什么不是。一个词可以由任何字母字符组成。
假设包含:dictionary$@#$LoL!@#FFDAfg(())
这里面的单词是:dictionary, LoL, FFDAfg。
我需要通读这些字符,如果它是一个字母,我需要直接将它添加到 trie 中(我还没有想出如何通过一次只添加一个字符来管理 trie)或者我必须跟踪每个字符并将其放入一个字符串中,一旦我到达一个非字母字符,我需要将 "word" 添加到 trie 中。
我的 trie 结构是:
struct trieNode
{
bool isWord;
struct trieNode *children[26]; //26 given there are 26 letters in the alphabet
};
我有方法
struct trieNode *createNode()
{
int i;
struct trieNode *tmp = (struct trieNode*)malloc(sizeof(struct trieNode));
for (i = 0; i<26;i++)
tmp -> children[i] = NULL;
tmp -> isWord = false;
return tmp;
我目前的插入方法是:
void insert(char *key)
{
int level = 0;
int index = getIndex(key[level]); //previously defined just gets the index of where the key should go
int len = strlen(key);
if(root == NULL)
root = createNode(); //root is defined under my struct def as: struct trieNode *root = NULL;
struct trieNode *tmp = root;
for (level = 0; level < len; level++)
{
if (tmp -> children [index] == NULL)
tmp ->children[index] = createNode();
tmp = tmp->children[index];
}
}
我相信如果我最终将字符串插入到 trie 中,此方法会起作用,但我的问题是我不确定如何从我之前的 readDict 文件中获取字符串。此外,我不确定如何修改它(如果可能)以一次插入一个字符,这样我就可以按字符读取我的字符,然后检查它是否是一个字母并转换为小写字母如果不是,则添加到 trie 中那里。
所以一种粗略的方法就是这样。您可能需要添加更多条件来处理一些边缘情况。
void *readDict(char *fileName)
{
FILE *file = fopen(fileName, "r");
char *word = malloc(100);
int index = 0;
int c;
while ((c = fgetc(file)) != EOF)
{
char ch = (char)c;
if (isalpha(ch)) // check if ch is a letter
word[index++] = ch;
else
{
word[index] = '[=10=]';
index = 0;
insert(word);
}
}
fclose(file);
}
这道题我得通读一遍,分清什么是单词。一个词不需要有意义,即。一个词可以是 asdas、sdgsgd、dog、sweet 等...要访问我必须通过映射文件来实现。
File *map, *dictfile, *datafile;
char *dictname, *dataname;
map = fopen(argv[1],"r");
while (fgets(buffer,sizeof(buffer),map) != NULL)
{
dictname = strtok(buffer," ");
dataname = strtok(NULL, " ");
strtok(dictname,"\n");
strtok(dataname,"\n");
该代码进入映射文件,然后区分文件名和文件名。 从他们那里我打开文件
if((datafile = fopen(dictname,"r")) == NULL) //error checking
{
in here I have to call a readDict(dictfile)
}
我的问题出在 readDict 中,我必须在这个字典文件中逐字逐句地区分什么是单词,什么不是。一个词可以由任何字母字符组成。 假设包含:dictionary$@#$LoL!@#FFDAfg(()) 这里面的单词是:dictionary, LoL, FFDAfg。 我需要通读这些字符,如果它是一个字母,我需要直接将它添加到 trie 中(我还没有想出如何通过一次只添加一个字符来管理 trie)或者我必须跟踪每个字符并将其放入一个字符串中,一旦我到达一个非字母字符,我需要将 "word" 添加到 trie 中。
我的 trie 结构是:
struct trieNode
{
bool isWord;
struct trieNode *children[26]; //26 given there are 26 letters in the alphabet
};
我有方法
struct trieNode *createNode()
{
int i;
struct trieNode *tmp = (struct trieNode*)malloc(sizeof(struct trieNode));
for (i = 0; i<26;i++)
tmp -> children[i] = NULL;
tmp -> isWord = false;
return tmp;
我目前的插入方法是:
void insert(char *key)
{
int level = 0;
int index = getIndex(key[level]); //previously defined just gets the index of where the key should go
int len = strlen(key);
if(root == NULL)
root = createNode(); //root is defined under my struct def as: struct trieNode *root = NULL;
struct trieNode *tmp = root;
for (level = 0; level < len; level++)
{
if (tmp -> children [index] == NULL)
tmp ->children[index] = createNode();
tmp = tmp->children[index];
}
}
我相信如果我最终将字符串插入到 trie 中,此方法会起作用,但我的问题是我不确定如何从我之前的 readDict 文件中获取字符串。此外,我不确定如何修改它(如果可能)以一次插入一个字符,这样我就可以按字符读取我的字符,然后检查它是否是一个字母并转换为小写字母如果不是,则添加到 trie 中那里。
所以一种粗略的方法就是这样。您可能需要添加更多条件来处理一些边缘情况。
void *readDict(char *fileName)
{
FILE *file = fopen(fileName, "r");
char *word = malloc(100);
int index = 0;
int c;
while ((c = fgetc(file)) != EOF)
{
char ch = (char)c;
if (isalpha(ch)) // check if ch is a letter
word[index++] = ch;
else
{
word[index] = '[=10=]';
index = 0;
insert(word);
}
}
fclose(file);
}