• 基于给定的文档生成倒排索引的编码与实践
    • 引言
    • 第一节、索引的构建方法
    • 第二节、Hash表的构建与实现
    • 第三节、倒排索引文件的生成与实现
    • 第四节、程序需求功能的改进
      • 对相同日期与不同日期的处理
      • 为关键词添上编码
    • 第五节、算法的二次改进
      • 省去二次Hash
      • 除去排序,针对不同日期的记录直接插入

    基于给定的文档生成倒排索引的编码与实践

    作者:July、yansha。

    出处:结构之法算法之道

    引言

    本周实现倒排索引。实现过程中,寻找资料,结果发现找份资料诸多不易:1、网上搜倒排索引实现,结果千篇一律,例子都是那几个同样的单词;2、到谷歌学术上想找点稍微有价值水平的资料,结果下篇论文还收费或者要求注册之类;3、大部分技术书籍只有理论,没有实践。于是,朋友戏言:网上一般有价值的东西不多。希望,本blog的出现能改变此现状。

    在第二十四章、倒排索引关键词不重复Hash编码中,我们针对一个给定的倒排索引文件,提取出其中的关键词,然后针对这些关键词进行Hash不重复编码。本章,咱们再倒退一步,即给定一个正排文档(暂略过文本解析,分词等步骤,日后会慢慢考虑这些且一并予以实现),要求生成对应的倒排索引文件。同时,本章还是基于Hash索引之上(运用暴雪的Hash函数可以比较完美的解决大数据量下的冲突问题),日后自会实现B+树索引。

    与此同时,本编程艺术系列逐步从为面试服务而转到实战性的编程当中了,教初学者如何编程,如何运用高效的算法解决实际应用中的编程问题,将逐步成为本编程艺术系列的主旨之一。

    OK,接下来,咱们针对给定的正排文档一步一步来生成倒排索引文件,有任何问题,欢迎随时不吝赐教或批评指正。谢谢。

    第一节、索引的构建方法

    • 根据信息检索导论(Christtopher D.Manning等著,王斌译)一书给的提示,我们可以选择两种构建索引的算法:BSBI算法,与SPIMI算法。

    BSBI算法,基于磁盘的外部排序算法,此算法首先将词项映射成其ID的数据结构,如Hash映射。而后将文档解析成词项ID-文档ID对,并在内存中一直处理,直到累积至放满一个固定大小的块空间为止,我们选择合适的块大小,使之能方便加载到内存中并允许在内存中快速排序,快速排序后的块转换成倒排索引格式后写入磁盘。

    建立倒排索引的步骤如下:

    • 将文档分割成几个大小相等的部分;
    • 对词项ID-文档ID进行排序;
    • 将具有同一词项ID的所有文档ID放到倒排记录表中,其中每条倒排记录仅仅是一个文档ID;
    • 将基于块的倒排索引写到磁盘上。

    此算法假如说最后可能会产生10个块。其伪码如下:

    1. BSBI NDEXConSTRUCTION()
    2. n <- 0
    3. while(all documents have not been processed)
    4. do n<-n+1
    5. block <- PARSENEXTBLOCK() //文档分析
    6. BSBI-INVERT(block)
    7. WRITEBLOCKTODISK(block,fn)
    8. MERGEBLOCKS(f1,...,fn;fmerged)

    (基于块的排序索引算法,该算法将每个块的倒排索引文件存入文件f1,…,fn中,最后合并成fmerged
    如果该算法应用最后一步产生了10个块,那么接下来便会将10个块索引同时合并成一个索引文件。)

    合并时,同时打开所有块对应的文件,内存中维护了为10个块准备的读缓冲区和一个为最终合并索引准备的写缓冲区。每次迭代中,利用优先级队列(如堆结构或类似的数据结构)选择最小的未处理的词项ID进行处理。如下图所示(图片引自深入搜索引擎—海里信息的压缩、索引和查询,梁斌译),分块索引,分块排序,最终全部合并(说实话,跟MapReduce还是有些类似的):

    基于给定的文档生成倒排索引的编码与实践 - 图1

    读入该词项的倒排记录表并合并,合并结果写回磁盘中。需要时,再次从文件中读入数据到每个读缓冲区。

    BSBI算法主要的时间消耗在排序上,选择什么排序方法呢,简单的快速排序足矣,其时间复杂度为O(N*logN),其中N是所需要排序的项(词项ID-文档ID对)的数目的上界。

    SPIMI算法,内存式单遍扫描索引算法

    与上述BSBI算法不同的是:SPIMI使用词项而不是其ID,它将每个块的词典写入磁盘,对于写一块则重新采用新的词典,只要硬盘空间足够大,它能索引任何大小的文档集。

    倒排索引 = 词典(关键词或词项+词项频率)+倒排记录表。建倒排索引的步骤如下:

    • 从头开始扫描每一个词项-文档ID(信息)对,遇一词,构建索引;
    • 继续扫描,若遇一新词,则再建一新索引块(加入词典,通过Hash表实现,同时,建一新的倒排记录表);若遇一旧词,则找到其倒排记录表的位置,添加其后
    • 在内存内基于分块完成排序,后合并分块;
    • 写入磁盘。

    其伪码如下:

    1. SPIMI-Invert(Token_stream)
    2. output.file=NEWFILE()
    3. dictionary = NEWHASH()
    4. while (free memory available)
    5. do token <-next(token_stream) //逐一处理每个词项-文档ID对
    6. if term(token) !(- dictionary
    7. /*如果词项是第一次出现,那么加入hash词典,同时,建立一个新的倒排索引表*/
    8. then postings_list = AddToDictionary(dictionary,term(token))
    9. /*如果不是第一次出现,那么直接返回其倒排记录表,在下面添加其后*/
    10. else postings_list = GetPostingList(dictionary,term(token))
    11. if full(postings_list)
    12. then postings_list =DoublePostingList(dictionary,term(token))
    13. /*SPIMI与BSBI的区别就在于此,前者直接在倒排记录表中增加此项新纪录*/
    14. AddToPosTingsList (postings_list,docID(token))
    15. sorted_terms <- SortTerms(dictionary)
    16. WriteBlockToDisk(sorted_terms,dictionary,output_file)
    17. return output_file

    SPIMI与BSBI的主要区别:

    SPIMI当发现关键词是第一次出现时,会直接在倒排记录表中增加一项(与BSBI算法不同)。同时,与BSBI算法一开始就整理出所有的词项ID-文档ID,并对它们进行排序的做法不同(而这恰恰是BSBI的做法),这里的每个倒排记录表都是动态增长的(也就是说,倒排记录表的大小会不断调整),同时,扫描一遍就可以实现全体倒排记录表的收集。

    SPIMI这样做有两点好处:

    由于不需要排序操作,因此处理的速度更快,
    由于保留了倒排记录表对词项的归属关系,因此能节省内存,词项的ID也不需要保存。这样,每次单独的SPIMI-Invert调用能够处理的块大小可以非常大,整个倒排索引的构建过程也可以非常高效。

    但不得不提的是,由于事先并不知道每个词项的倒排记录表大小,算法一开始只能分配一个较小的倒排记录表空间,每次当该空间放满的时候,就会申请加倍的空间,

    与此同时,自然而然便会浪费一部分空间(当然,此前因为不保存词项ID,倒也省下一点空间,总体而言,算作是抵销了)。

    不过,至少SPIMI所用的空间会比BSBI所用空间少。当内存耗尽后,包括词典和倒排记录表的块索引将被写到磁盘上,但在此之前,为使倒排记录表按照词典顺序来加快最后的合并操作,所以要对词项进行排序操作。

    小数据量与大数据量的区别

    • 在小数据量时,有足够的内存保证该创建过程可以一次完成;
    • 数据规模增大后,可以采用分组索引,然后再归并索 引的策略。该策略是,

    建立索引的模块根据当时运行系统所在的计算机的内存大小,将索引分为 k 组,使得每组运算所需内存都小于系统能够提供的最大使用内存的大小。
    按照倒排索引的生成算法,生成 k 组倒排索引。
    然后将这 k 组索引归并,即将相同索引词对应的数据合并到一起,就得到了以索引词为主键的最终的倒排文件索引,即反向索引。

    为了测试的方便,本文针对小数据量进行从正排文档到倒排索引文件的实现。而且针对大数量的K路归并算法或基于磁盘的外部排序算法本编程艺术系列第十章中已有详细阐述。

    第二节、Hash表的构建与实现

    如下,给定如下图所示的正排文档,每一行的信息分别为(中间用##########隔开):文档ID、订阅源(子频道)、 频道分类、 网站类ID(大频道)、时间、 md5、文档权值、关键词、作者等等。
    基于给定的文档生成倒排索引的编码与实践 - 图2

    要求基于给定的上述正排文档。生成如第二十四章所示的倒排索引文件(注,关键词所在的文章如果是同一个日期的话,是挨在同一行的,用“#”符号隔开):
    基于给定的文档生成倒排索引的编码与实践 - 图3

    我们知道:为网页建立全文索引是网页预处理的核心部分,包括分析网页和建立倒排文件。二者是顺序进行,先分析网页,后建立倒排文件(也称为反向索引),如图所示:

    基于给定的文档生成倒排索引的编码与实践 - 图4

    正如上图粗略所示,我们知道倒排索引创建的过程如下:

    • 写爬虫抓取相关的网页,而后提取相关网页或文章中所有的关键词;
    • 分词,找出所有单词;
    • 过滤不相干的信息(如广告等信息);
    • 构建倒排索引,关键词=>(文章ID 出现次数 出现的位置)生成词典文件 频率文件 位置文件;
    • 压缩。

    因为已经给定了正排文档,接下来,咱们跳过一系列文本解析,分词等中间步骤,直接根据正排文档生成倒排索引文档(幸亏有yansha相助,不然,寸步难行,其微博地址为:http://weibo.com/yanshazi,欢迎关注他)。

    OK,闲不多说,咱们来一步一步实现吧。

    建相关的数据结构

    根据给定的正排文档,我们可以建立如下的两个结构体表示这些信息:文档ID、订阅源(子频道)、 频道分类、 网站类ID(大频道)、时间、 md5、文档权值、关键词、作者等等。如下所示:

    1. typedef struct key_node
    2. {
    3. char *pkey; // 关键词实体
    4. int count; // 关键词出现次数
    5. int pos; // 关键词在hash表中位置
    6. struct doc_node *next; // 指向文档结点
    7. }KEYNODE, *key_list;
    8. key_list key_array[TABLE_SIZE];
    9. typedef struct doc_node
    10. {
    11. char id[WORD_MAX_LEN]; //文档ID
    12. int classOne; //订阅源(子频道)
    13. char classTwo[WORD_MAX_LEN]; //频道分类
    14. int classThree; //网站类ID(大频道)
    15. char time[WORD_MAX_LEN]; //时间
    16. char md5[WORD_MAX_LEN]; //md5
    17. int weight; //文档权值
    18. struct doc_node *next;
    19. }DOCNODE, *doc_list;

    我们知道,通过第二十四章的暴雪的Hash表算法,可以比较好的避免相关冲突的问题。下面,我们再次引用其代码:
    基于暴雪的Hash之上的改造算法

    1. //函数prepareCryptTable以下的函数生成一个长度为0x100的cryptTable[0x100]
    2. void PrepareCryptTable()
    3. {
    4. unsigned long seed = 0x00100001, index1 = 0, index2 = 0, i;
    5. for( index1 = 0; index1 <0x100; index1++ )
    6. {
    7. for( index2 = index1, i = 0; i < 5; i++, index2 += 0x100)
    8. {
    9. unsigned long temp1, temp2;
    10. seed = (seed * 125 + 3) % 0x2AAAAB;
    11. temp1 = (seed & 0xFFFF)<<0x10;
    12. seed = (seed * 125 + 3) % 0x2AAAAB;
    13. temp2 = (seed & 0xFFFF);
    14. cryptTable[index2] = ( temp1 | temp2 );
    15. }
    16. }
    17. }
    18. //函数HashString以下函数计算lpszFileName 字符串的hash值,其中dwHashType 为hash的类型,
    19. unsigned long HashString(const char *lpszkeyName, unsigned long dwHashType )
    20. {
    21. unsigned char *key = (unsigned char *)lpszkeyName;
    22. unsigned long seed1 = 0x7FED7FED;
    23. unsigned long seed2 = 0xEEEEEEEE;
    24. int ch;
    25. while( *key != 0 )
    26. {
    27. ch = *key++;
    28. seed1 = cryptTable[(dwHashType<<8) + ch] ^ (seed1 + seed2);
    29. seed2 = ch + seed1 + seed2 + (seed2<<5) + 3;
    30. }
    31. return seed1;
    32. }
    33. //按关键字查询,如果成功返回hash表中索引位置
    34. key_list SearchByString(const char *string_in)
    35. {
    36. const int HASH_OFFSET = 0, HASH_C = 1, HASH_D = 2;
    37. unsigned int nHash = HashString(string_in, HASH_OFFSET);
    38. unsigned int nHashC = HashString(string_in, HASH_C);
    39. unsigned int nHashD = HashString(string_in, HASH_D);
    40. unsigned int nHashStart = nHash % TABLE_SIZE;
    41. unsigned int nHashPos = nHashStart;
    42. while (HashTable[nHashPos].bExists)
    43. {
    44. if (HashATable[nHashPos] == (int) nHashC && HashBTable[nHashPos] == (int) nHashD)
    45. {
    46. break;
    47. //查询与插入不同,此处不需修改
    48. }
    49. else
    50. {
    51. nHashPos = (nHashPos + 1) % TABLE_SIZE;
    52. }
    53. if (nHashPos == nHashStart)
    54. {
    55. break;
    56. }
    57. }
    58. if( key_array[nHashPos] && strlen(key_array[nHashPos]->pkey))
    59. {
    60. return key_array[nHashPos];
    61. }
    62. return NULL;
    63. }
    64. //按索引查询,如果成功返回关键字(此函数在本章中没有被用到,可以忽略)
    65. key_list SearchByIndex(unsigned int nIndex)
    66. {
    67. unsigned int nHashPos = nIndex;
    68. if (nIndex < TABLE_SIZE)
    69. {
    70. if(key_array[nHashPos] && strlen(key_array[nHashPos]->pkey))
    71. {
    72. return key_array[nHashPos];
    73. }
    74. }
    75. return NULL;
    76. }
    77. //插入关键字,如果成功返回hash值
    78. int InsertString(const char *str)
    79. {
    80. const int HASH_OFFSET = 0, HASH_A = 1, HASH_B = 2;
    81. unsigned int nHash = HashString(str, HASH_OFFSET);
    82. unsigned int nHashA = HashString(str, HASH_A);
    83. unsigned int nHashB = HashString(str, HASH_B);
    84. unsigned int nHashStart = nHash % TABLE_SIZE;
    85. unsigned int nHashPos = nHashStart;
    86. int len;
    87. while (HashTable[nHashPos].bExists)
    88. {
    89. nHashPos = (nHashPos + 1) % TABLE_SIZE;
    90. if (nHashPos == nHashStart)
    91. break;
    92. }
    93. len = strlen(str);
    94. if (!HashTable[nHashPos].bExists && (len < WORD_MAX_LEN))
    95. {
    96. HashATable[nHashPos] = nHashA;
    97. HashBTable[nHashPos] = nHashB;
    98. key_array[nHashPos] = (KEYNODE *) malloc (sizeof(KEYNODE) * 1);
    99. if(key_array[nHashPos] == NULL)
    100. {
    101. printf("10000 EMS ERROR !!!!\n");
    102. return 0;
    103. }
    104. key_array[nHashPos]->pkey = (char *)malloc(len+1);
    105. if(key_array[nHashPos]->pkey == NULL)
    106. {
    107. printf("10000 EMS ERROR !!!!\n");
    108. return 0;
    109. }
    110. memset(key_array[nHashPos]->pkey, 0, len+1);
    111. strncpy(key_array[nHashPos]->pkey, str, len);
    112. *((key_array[nHashPos]->pkey)+len) = 0;
    113. key_array[nHashPos]->pos = nHashPos;
    114. key_array[nHashPos]->count = 1;
    115. key_array[nHashPos]->next = NULL;
    116. HashTable[nHashPos].bExists = 1;
    117. return nHashPos;
    118. }
    119. if(HashTable[nHashPos].bExists)
    120. printf("30000 in the hash table %s !!!\n", str);
    121. else
    122. printf("90000 strkey error !!!\n");
    123. return -1;
    124. }

    有了这个Hash表,接下来,我们就可以把词插入Hash表进行存储了。

    第三节、倒排索引文件的生成与实现

    Hash表实现了(存于HashSearch.h中),还得编写一系列的函数,如下所示(所有代码还只是初步实现了功能,稍后在第四部分中将予以改进与优化):

    1. //处理空白字符和空白行
    2. int GetRealString(char *pbuf)
    3. {
    4. int len = strlen(pbuf) - 1;
    5. while (len > 0 && (pbuf[len] == (char)0x0d || pbuf[len] == (char)0x0a || pbuf[len] == ' ' || pbuf[len] == '\t'))
    6. {
    7. len--;
    8. }
    9. if (len < 0)
    10. {
    11. *pbuf = '\0';
    12. return len;
    13. }
    14. pbuf[len+1] = '\0';
    15. return len + 1;
    16. }
    17. //重新strcoll字符串比较函数
    18. int strcoll(const void *s1, const void *s2)
    19. {
    20. char *c_s1 = (char *)s1;
    21. char *c_s2 = (char *)s2;
    22. while (*c_s1 == *c_s2++)
    23. {
    24. if (*c_s1++ == '\0')
    25. {
    26. return 0;
    27. }
    28. }
    29. return *c_s1 - *--c_s2;
    30. }
    31. //从行缓冲中得到各项信息,将其写入items数组
    32. void GetItems(char *&move, int &count, int &wordnum)
    33. {
    34. char *front = move;
    35. bool flag = false;
    36. int len;
    37. move = strstr(move, "#####");
    38. if (*(move + 5) == '#')
    39. {
    40. flag = true;
    41. }
    42. if (move)
    43. {
    44. len = move - front;
    45. strncpy(items[count], front, len);
    46. }
    47. items[count][len] = '\0';
    48. count++;
    49. if (flag)
    50. {
    51. move = move + 10;
    52. } else
    53. {
    54. move = move + 5;
    55. }
    56. }
    57. //保存关键字相应的文档内容
    58. doc_list SaveItems()
    59. {
    60. doc_list infolist = (doc_list) malloc(sizeof(DOCNODE));
    61. strcpy_s(infolist->id, items[0]);
    62. infolist->classOne = atoi(items[1]);
    63. strcpy_s(infolist->classTwo, items[2]);
    64. infolist->classThree = atoi(items[3]);
    65. strcpy_s(infolist->time, items[4]);
    66. strcpy_s(infolist->md5, items[5]);
    67. infolist->weight = atoi(items[6]);
    68. return infolist;
    69. }
    70. //得到目录下所有文件名
    71. int GetFileName(char filename[][FILENAME_MAX_LEN])
    72. {
    73. _finddata_t file;
    74. long handle;
    75. int filenum = 0;
    76. //C:\Users\zhangxu\Desktop\CreateInvertedIndex\data
    77. if ((handle = _findfirst("C:\\Users\\zhangxu\\Desktop\\CreateInvertedIndex\\data\\*.txt", &file)) == -1)
    78. {
    79. printf("Not Found\n");
    80. }
    81. else
    82. {
    83. do
    84. {
    85. strcpy_s(filename[filenum++], file.name);
    86. } while (!_findnext(handle, &file));
    87. }
    88. _findclose(handle);
    89. return filenum;
    90. }
    91. //以读方式打开文件,如果成功返回文件指针
    92. FILE* OpenReadFile(int index, char filename[][FILENAME_MAX_LEN])
    93. {
    94. char *abspath;
    95. char dirpath[] = {"data\\"};
    96. abspath = (char *)malloc(ABSPATH_MAX_LEN);
    97. strcpy_s(abspath, ABSPATH_MAX_LEN, dirpath);
    98. strcat_s(abspath, FILENAME_MAX_LEN, filename[index]);
    99. FILE *fp = fopen (abspath, "r");
    100. if (fp == NULL)
    101. {
    102. printf("open read file error!\n");
    103. return NULL;
    104. }
    105. else
    106. {
    107. return fp;
    108. }
    109. }
    110. //以写方式打开文件,如果成功返回文件指针
    111. FILE* OpenWriteFile(const char *in_file_path)
    112. {
    113. if (in_file_path == NULL)
    114. {
    115. printf("output file path error!\n");
    116. return NULL;
    117. }
    118. FILE *fp = fopen(in_file_path, "w+");
    119. if (fp == NULL)
    120. {
    121. printf("open write file error!\n");
    122. }
    123. return fp;
    124. }

    最后,主函数编写如下:

    1. int main()
    2. {
    3. key_list keylist;
    4. char *pbuf, *move;
    5. int filenum = GetFileName(filename);
    6. FILE *fr;
    7. pbuf = (char *)malloc(BUF_MAX_LEN);
    8. memset(pbuf, 0, BUF_MAX_LEN);
    9. FILE *fw = OpenWriteFile("index.txt");
    10. if (fw == NULL)
    11. {
    12. return 0;
    13. }
    14. PrepareCryptTable(); //初始化Hash表
    15. int wordnum = 0;
    16. for (int i = 0; i < filenum; i++)
    17. {
    18. fr = OpenReadFile(i, filename);
    19. if (fr == NULL)
    20. {
    21. break;
    22. }
    23. // 每次读取一行处理
    24. while (fgets(pbuf, BUF_MAX_LEN, fr))
    25. {
    26. int count = 0;
    27. move = pbuf;
    28. if (GetRealString(pbuf) <= 1)
    29. continue;
    30. while (move != NULL)
    31. {
    32. // 找到第一个非'#'的字符
    33. while (*move == '#')
    34. move++;
    35. if (!strcmp(move, ""))
    36. break;
    37. GetItems(move, count, wordnum);
    38. }
    39. for (int i = 7; i < count; i++)
    40. {
    41. // 将关键字对应的文档内容加入文档结点链表中
    42. if (keylist = SearchByString(items[i])) //到hash表内查询
    43. {
    44. doc_list infolist = SaveItems();
    45. infolist->next = keylist->next;
    46. keylist->count++;
    47. keylist->next = infolist;
    48. }
    49. else
    50. {
    51. // 如果关键字第一次出现,则将其加入hash表
    52. int pos = InsertString(items[i]); //插入hash表
    53. keylist = key_array[pos];
    54. doc_list infolist = SaveItems();
    55. infolist->next = NULL;
    56. keylist->next = infolist;
    57. if (pos != -1)
    58. {
    59. strcpy_s(words[wordnum++], items[i]);
    60. }
    61. }
    62. }
    63. }
    64. }
    65. // 通过快排对关键字进行排序
    66. qsort(words, WORD_MAX_NUM, WORD_MAX_LEN, strcoll);
    67. // 遍历关键字数组,将关键字及其对应的文档内容写入文件中
    68. for (int i = 0; i < WORD_MAX_NUM; i++)
    69. {
    70. keylist = SearchByString(words[i]);
    71. if (keylist != NULL)
    72. {
    73. fprintf(fw, "%s %d\n", words[i], keylist->count);
    74. doc_list infolist = keylist->next;
    75. for (int j = 0; j < keylist->count; j++)
    76. {
    77. //文档ID,订阅源(子频道) 频道分类 网站类ID(大频道) 时间 md5,文档权值
    78. fprintf(fw, "%s %d %s %d %s %s %d\n", infolist->id, infolist->classOne,
    79. infolist->classTwo, infolist->classThree, infolist->time, infolist->md5, infolist->weight);
    80. infolist = infolist->next;
    81. }
    82. }
    83. }
    84. free(pbuf);
    85. fclose(fr);
    86. fclose(fw);
    87. system("pause");
    88. return 0;
    89. }

    程序编译运行后,生成的倒排索引文件为index.txt,其与原来给定的正排文档对照如下:
    基于给定的文档生成倒排索引的编码与实践 - 图5

    有没有发现关键词奥恰洛夫出现在的三篇文章是同一个日期1210的,貌似与本文开头指定的倒排索引格式要求不符?因为第二部分开头中,已明确说明:“注,关键词所在的文章如果是同一个日期的话,是挨在同一行的,用“#”符号隔开”。OK,有疑问是好事,代表你思考了,请直接转至下文第4部分。

    第四节、程序需求功能的改进

    对相同日期与不同日期的处理

    细心的读者可能还是会注意到:在第二部分开头中,要求基于给定的上述正排文档。生成如第二十四章所示的倒排索引文件是下面这样子的,即是:
    基于给定的文档生成倒排索引的编码与实践 - 图6

    也就是说,上面建索引的过程本该是如下的:
    基于给定的文档生成倒排索引的编码与实践 - 图7

    与第一部分所述的SMIPI算法有什么区别?对的,就在于对在同一个日期的出现的关键词的处理。如果是遇一旧词,则找到其倒排记录表的位置:相同日期,添加到之前同一日期的记录之后(第一个记录的后面记下同一日期的记录数目);不同日期,另起一行新增记录。

    • 相同(单个)日期,根据文档权值排序
    • 不同日期,根据时间排序

    代码主要修改如下:

    1. //function: 对链表进行冒泡排序
    2. void ListSort(key_list keylist)
    3. {
    4. doc_list p = keylist->next;
    5. doc_list final = NULL;
    6. while (true)
    7. {
    8. bool isfinish = true;
    9. while (p->next != final) {
    10. if (strcmp(p->time, p->next->time) < 0)
    11. {
    12. SwapDocNode(p);
    13. isfinish = false;
    14. }
    15. p = p->next;
    16. }
    17. final = p;
    18. p = keylist->next;
    19. if (isfinish || p->next == final) {
    20. break;
    21. }
    22. }
    23. }
    24. int main()
    25. {
    26. key_list keylist;
    27. char *pbuf, *move;
    28. int filenum = GetFileName(filename);
    29. FILE *frp;
    30. pbuf = (char *)malloc(BUF_MAX_LEN);
    31. memset(pbuf, 0, BUF_MAX_LEN);
    32. FILE *fwp = OpenWriteFile("index.txt");
    33. if (fwp == NULL) {
    34. return 0;
    35. }
    36. PrepareCryptTable();
    37. int wordnum = 0;
    38. for (int i = 0; i < filenum; i++)
    39. {
    40. frp = OpenReadFile(i, filename);
    41. if (frp == NULL) {
    42. break;
    43. }
    44. // 每次读取一行处理
    45. while (fgets(pbuf, BUF_MAX_LEN, frp))
    46. {
    47. int count = 0;
    48. move = pbuf;
    49. if (GetRealString(pbuf) <= 1)
    50. continue;
    51. while (move != NULL)
    52. {
    53. // 找到第一个非'#'的字符
    54. while (*move == '#')
    55. move++;
    56. if (!strcmp(move, ""))
    57. break;
    58. GetItems(move, count, wordnum);
    59. }
    60. for (int i = 7; i < count; i++) {
    61. // 将关键字对应的文档内容加入文档结点链表中
    62. // 如果关键字第一次出现,则将其加入hash表
    63. if (keylist = SearchByString(items[i])) {
    64. doc_list infolist = SaveItems();
    65. infolist->next = keylist->next;
    66. keylist->count++;
    67. keylist->next = infolist;
    68. } else {
    69. int pos = InsertString(items[i]);
    70. keylist = key_array[pos];
    71. doc_list infolist = SaveItems();
    72. infolist->next = NULL;
    73. keylist->next = infolist;
    74. if (pos != -1) {
    75. strcpy_s(words[wordnum++], items[i]);
    76. }
    77. }
    78. }
    79. }
    80. }
    81. // 通过快排对关键字进行排序
    82. qsort(words, WORD_MAX_NUM, WORD_MAX_LEN, strcoll);
    83. // 遍历关键字数组,将关键字及其对应的文档内容写入文件中
    84. int rownum = 1;
    85. for (int i = 0; i < WORD_MAX_NUM; i++) {
    86. keylist = SearchByString(words[i]);
    87. if (keylist != NULL) {
    88. doc_list infolist = keylist->next;
    89. char date[9];
    90. // 截取年月日
    91. for (int j = 0; j < keylist->count; j++)
    92. {
    93. strncpy_s(date, infolist->time, 8);
    94. date[8] = '\0';
    95. strncpy_s(infolist->time, date, 9);
    96. infolist = infolist->next;
    97. }
    98. // 对链表根据时间进行排序
    99. ListSort(keylist);
    100. infolist = keylist->next;
    101. int *count = new int[WORD_MAX_NUM];
    102. memset(count, 0, WORD_MAX_NUM);
    103. strcpy_s(date, infolist->time);
    104. int num = 0;
    105. // 得到单个日期的文档数目
    106. for (int j = 0; j < keylist->count; j++)
    107. {
    108. if (strcmp(date, infolist->time) == 0) {
    109. count[num]++;
    110. } else {
    111. count[++num]++;
    112. }
    113. strcpy_s(date, infolist->time);
    114. infolist = infolist->next;
    115. }
    116. fprintf(fwp, "%s %d %d\n", words[i], num + 1, rownum);
    117. WriteFile(keylist, num, fwp, count);
    118. rownum++;
    119. }
    120. }
    121. free(pbuf);
    122. // fclose(frp);
    123. fclose(fwp);
    124. system("pause");
    125. return 0;
    126. }

    修改后编译运行,生成的index.txt文件如下:
    基于给定的文档生成倒排索引的编码与实践 - 图8

    为关键词添上编码

    如上图所示,已经满足需求了。但可以再在每个关键词的背后添加一个计数表示索引到了第多少个关键词:
    基于给定的文档生成倒排索引的编码与实践 - 图9

    第五节、算法的二次改进

    省去二次Hash

    针对本文评论下读者的留言,做了下思考,自觉可以省去二次hash:

    1. for (int i = 7; i < count; i++)
    2. {
    3. // 将关键字对应的文档内容加入文档结点链表中
    4. //也就是说当查询到hash表中没有某个关键词之,后便会插入
    5. //而查询的时候,search会调用hashstring,得到了nHashC ,nHashD
    6. //插入的时候又调用了一次hashstring,得到了nHashA,nHashB
    7. //而如果查询的时候,是针对同一个关键词查询的,所以也就是说nHashC&nHashD,与nHashA&nHashB是相同的,无需二次hash
    8. //所以,若要改进,改的也就是下面这个if~else语句里头。July,2011.12.30。
    9. if (keylist = SearchByString(items[i])) //到hash表内查询
    10. {
    11. doc_list infolist = SaveItems();
    12. infolist->next = keylist->next;
    13. keylist->count++;
    14. keylist->next = infolist;
    15. }
    16. else
    17. {
    18. // 如果关键字第一次出现,则将其加入hash表
    19. int pos = InsertString(items[i]); //插入hash表
    20. keylist = key_array[pos];
    21. doc_list infolist = SaveItems();
    22. infolist->next = NULL;
    23. keylist->next = infolist;
    24. if (pos != -1)
    25. {
    26. strcpy_s(words[wordnum++], items[i]);
    27. }
    28. }
    29. }
    30. }
    31. }
    32. // 通过快排对关键字进行排序
    33. qsort(words, WORD_MAX_NUM, WORD_MAX_LEN, strcoll);

    除去排序,针对不同日期的记录直接插入

    1. //对链表进行冒泡排序。这里可以改成快速排序:等到统计完所有有关这个关键词的文章之后,才能对他集体快排。
    2. //但其实完全可以用插入排序,不同日期的,根据时间的先后找到插入位置进行插入:
    3. //假如说已有三条不同日期的记录 A B C
    4. //来了D后,发现D在C之前,B之后,那么就必须为它找到B C之间的插入位置,
    5. //A B D C。July、2011.12.31。
    6. void ListSort(key_list keylist)
    7. {
    8. doc_list p = keylist->next;
    9. doc_list final = NULL;
    10. while (true)
    11. {
    12. bool isfinish = true;
    13. while (p->next != final) {
    14. if (strcmp(p->time, p->next->time) < 0) //不同日期的按最早到最晚排序
    15. {
    16. SwapDocNode(p);
    17. isfinish = false;
    18. }
    19. p = p->next;
    20. }
    21. final = p;
    22. p = keylist->next;
    23. if (isfinish || p->next == final) {
    24. break;
    25. }
    26. }
    27. }

    综上两节免去冒泡排序和,省去二次hash和免去冒泡排序,修改后如下:

    1. for (int i = 7; i < count; i++) {
    2. // 将关键字对应的文档内容加入文档结点链表中
    3. // 如果关键字第一次出现,则将其加入hash表
    4. InitHashValue(items[i], hashvalue);
    5. if (keynode = SearchByString(items[i], hashvalue)) {
    6. doc_list infonode = SaveItems();
    7. doc_list p = keynode->next;
    8. // 根据时间由早到晚排序
    9. if (strcmp(infonode->time, p->time) < 0) {
    10. //考虑infonode插入keynode后的情况
    11. infonode->next = p;
    12. keynode->next = infonode;
    13. } else {
    14. //考虑其他情况
    15. doc_list pre = p;
    16. p = p->next;
    17. while (p)
    18. {
    19. if (strcmp(infonode->time, p->time) > 0) {
    20. p = p->next;
    21. pre = pre->next;
    22. } else {
    23. break;
    24. }
    25. }
    26. infonode->next = p;
    27. pre->next = infonode;
    28. }
    29. keynode->count++;
    30. } else {
    31. int pos = InsertString(items[i], hashvalue);
    32. keynode = key_array[pos];
    33. doc_list infolist = SaveItems();
    34. infolist->next = NULL;
    35. keynode->next = infolist;
    36. if (pos != -1) {
    37. strcpy_s(words[wordnum++], items[i]);
    38. }
    39. }
    40. }
    41. }
    42. }
    43. // 通过快排对关键字进行排序
    44. qsort(words, WORD_MAX_NUM, WORD_MAX_LEN, strcoll);

    修改后编译运行的效果图如下(用了另外一份更大的数据文件进行测试):
    基于给定的文档生成倒排索引的编码与实践 - 图10