#include "stdio.h"
#include "string.h"
#include "math.h"
#include "malloc.h" const long long Max_size = ;//输入字符串的最大长度,可以由单个词条和多个词条组成
const long long N = ;//输出与某个单词最接近的N个词
const long long Max_w = ;//单个词条的最大长度 int main(int argc,char **argv)
{
FILE *f;//读取的文件指针
char stemp[Max_size];//中间变量
char *bestw[N];//存储与某个词最接近N个词条
char ch;
float *M;//存储所有词条的距离相关信息
char *vocab;//存储所有词条的字符信息
/*
*file_name[Max_size];存放要读取内容的文件名;
*st[100][Max_size];//二维数组,中间变量
*dist:距离;len:长度;bestd[N]:存储与某个词最接近的N个词的距离
*vec[Max_size]:存储Max_size个词与某个指定词的距离
*words:词条的总数目;size:词条表示的维数;
*key[100],存储某个词条在总词条中的位置下标
*/
char file_name[Max_size],st[][Max_size];
float dist,len,bestd[N],vec[Max_size];
long long words,size,i,j,k,l,num,key[]; if(argc<)//打印程序所在路径
{
printf("Usage:./distance<FILE>\nwhere FILE contains word projections in the BINARY FORMAIN\n");
return ;
} strcpy(file_name,argv[]);//argv[1]中存放的文件名赋给file_name
f = fopen(file_name,"rb"); if(f == NULL)//文件打开失败
{
printf("Input file not found\n");
return -;
} fscanf(f,"lld",&words);//输入总词条数组
fscanf(f,"lld",&size);//输入用来表示每个词条的维数大小 vocab = (char *)malloc((long long)words * Max_w *sizeof(char)); //根据实际的维数分配存储块大小
for(i = ; i < N; i++)bestw[i] = (char *)malloc(Max_size * sizeof(char)); M = (float *)malloc((long long)words * (long long)size *sizeof(float));
if(M==NULL)//不能分配所需要大小的内存
{
printf("Cannot allocate memory:%lld MB %lld %lld\n",(long long )words *size*sizeof(float)//,words,size);
return -;
} for(j = ; j < words; j++)//words个词条每个词条坐标进行归一化
{
i = ;
while(true)
{
vocab[j*Max_w + i] = fgetc(f);
if(feof(f) || vocab[j * Max_w + i] == ' ')break;
if((i <Max_w)&&(vocab[j * Max_w + i] !='\n'))i++;
}
vocab[j * Max_w + i] = ;
for(i = ;i < size ; i++ ) fread(&M[j * size + i],sizeof(float),,f);
len = ;
for(i = ; i < size ; i++ ) len += M[j * size + i]*M[j * size + i];
len = sqrt(len);
for(i = ; i < size ; i++ ) M[j * size + i] = M[j * size + i] / len; //坐标归一化
}
fclose(f); while(true)
{
for(i = ; i < N ; i++ )
{
bestd[i] = ;//N个词条距离初始为0
bestw[i][] = ;//初始化
} printf("Entor word or sentence(EXIT to break:)"); i = ;
while(true)//键盘读入单个词条或多个词条 ;当字符数组超过Max_size -1 或者遇到回车符结束
{
stemp[i] = fgetc(stdin);
if((stemp[i] == '\n')||(i >=Max_size -))
{
stemp[i] = ;
break;
}
}
if(!strcmp(stemp,"EXIT"))break;//如果输入的是"EXIT",则退出 num = ;//用来统计键盘输入词条的数目
j = ;
k = ;
while(true)
{
st[num][j++] = stemp[k++];
st[num][j] = ;
if(stemp[k] ==)break;//读完结束
if(stemp[k++] == ' ')//遇到空格 词条数目+1
{
num++;//词条数目+1
j = ;
}
} num++;//词条的总数目 //找出每个词条 最接近的N个词条
for(i = ; i < num ; i++)
{
for(j = ; j < words ;j++)if(!strcmp(&vocab[j * Max_w],st[j]))break;//在总词条中找到这个词条,获得这个词条在总词条中的位置
if(j == words) j = -;//这个词条不存在
key[i] = j;
printf("\nWord:%s Position in vocabulary: %lld\n",st[i],key[i]); if(j == -)//这个词条不在这个词汇表中
{
printf("Out of dictionary word!\n");
break;//终止循环
}
} if(j==-)continue;//继续执行 printf("\n Word Cosine distance\n");
printf("------------------------------------------------------------------------\n"); for(i = ; i < size ; i++ ) vec[i] = ;//距离初始化为0
for(j = ; j < num ; j++ ) //遍历每个词,如果输入多个词向量vec[i]是各个词向量的累加和
{
if(key[j] == -)continue;
for(i = ; i < size ; i++ ) vec[i] += M[i + key[j]*size];
} len = ;
for(i = ; i< size ; i++) len +=vec[i] * vec[i];
len = sqrt(len); for(i = ; i < size ; i++) vec[i] = vec[i] / len;//将vec归一化,当只输入一个词时,不起作用 for(i = ; i < N ; i++)
{
bestd[i] = -;
bestw[i][] = ;
} //由于查询词和词汇表都做了归一化,所以余弦相似度等价于向量的内积,内机越大越相似
for(k = ; k < words ; k++)//遍历词汇表
{
i = ;
for(j = ; j < num ; j++)//i的作用;如果遍历词和查询词相同,则跳过此词
{
if(key[j] == k) i =;
}
if(i == ) continue;
dist = ; for(i = ; i < N ; i++ )
{
dist += vec[i] * M[k * size + i] ;
} for(i = ; i < N ; i++ )
{
if(dist > bestd[i])
{
for(j = N- ; j > i ; j--)
{
bestd[j] = bestd[j - ];
strcpy(bestw[j],bestw[j-]);
}
bestd[j] = dist;
strcpy(bestw[i] , &vocab[k * Max_size]);
break;
}
}
}
for(i = ; i < N ; i++ )printf("%50s\t\t%f\n",bestw[i],bestd[i]);
}
return ;
}

distance.c的更多相关文章

  1. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  4. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  5. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  6. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  7. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  8. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  9. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  10. C#实现Levenshtein distance最小编辑距离算法

    Levenshtein distance,中文名为最小编辑距离,其目的是找出两个字符串之间需要改动多少个字符后变成一致.该算法使用了动态规划的算法策略,该问题具备最优子结构,最小编辑距离包含子最小编辑 ...

随机推荐

  1. <转>golang 并发性能数据

    1.管道chan吞吐极限10,000,000,单次Put,Get耗时大约100ns/op,无论是采用单Go程,还是多Go程并发(并发数:100, 10000, 100000),耗时均没有变化,Go内核 ...

  2. eclipse +cvs 的基本使用方法(一)

    很多时候我们在做项目开发时,会用到cvs,现在我给大家介绍一下关于eclipse下怎么使用cvs管理功能,eclipse本身是自带cvs的,我们只要简单设置一下让它连接到cvs服务器上.    看下图 ...

  3. myEclipse + phonegap-2.9.0 总跳出3个脚本提示

    环境:myEclipse + phonegap-2.9.0按照教程全部完毕后,浏览页面时,总会跳出3个脚本提示:1:gap:["Device","getDeviceInf ...

  4. 如何使用composer?

    /** *@测试环境:我笔记本 本地xampp集成环境 *@操作系统:Windows 7 **/   安装方法:官方有很详细的介绍,这里就不重复造轮子了. 官方链接:http://docs.phpco ...

  5. API例子:用Java/JavaScript下载内容提取器

    1,引言 本文讲解怎样用Java和JavaScript使用 GooSeeker API 接口下载内容提取器,这是一个示例程序.什么是内容提取器?为什么用这种方式?源自Python即时网络爬虫开源项目: ...

  6. 中国大学MOOC-翁恺-C语言程序设计习题集

    今年网易出了“中国大学MOOC”,于是选了浙大翁恺老师的“C语言程序设计”学习,近期打算把自己在该课程中的PAT习题解答做一个记录,等自己编程能力提高后再来看现在写的代码哪里还有写的不好,可以改进的地 ...

  7. IEnumerable

    C#基础之IEnumerable 1.IEnumerable的作用 在使用Linq查询数据时经常以IEnumerable<T>来作为数据查询返回对象,在使用foreach进行遍历时需要该对 ...

  8. android 遇到的细节 FAQ

    1.ListView 设置addHead 在3.0与之前版本若在:setAdapter之后添加,运行报错.4.0以后不报错 2.ListView  Adapter    getView函数忘记返回vi ...

  9. 梦游前端,JavaScript兼容性

    前端兼容问题出现的原因 何为操作系统?操作系统(Operating System)是管理和控制计算机硬件与软件资源的计算机程序.是的,任何的应用软件必须在操作系统的支持下运行. 大家会疑问?为什么我要 ...

  10. poj 1907 Work Reduction_贪心

    题意:公司要你要完成N份任务,但是你是不可能全部完成的,所以需要雇佣别人来做,做到剩下M份时,自己再亲自出马.现在有个机构,有两种付费方式,第一种是每付A元帮你完成1份,第二种是每付B元帮你完成剩下任 ...