今天,我试着写了一个程序,它可以接收一段文字,并创建一个图表来显示不同单词之间的关系。一切都很顺利,除了我不知道如何以更好的方式找到联系。更好的方式意味着类似于mind map.This是一个简单的输入,但我想创建一个程序,它可以从维基百科中提取一段内容,并给出一个非常好的思维导图。对于下面的输入,我从我的程序的点格式输出中得到的图形是
roses are red line_end
sky is blue line_end
life is beautiful line_end
everything is going fine line_end file_end

但是对于像这样的输入,它只是创建了一个比文本本身更模糊的非常大的图形。
Probability is a measure of the likeliness that an event will occur line_end
Probability is used to quantify an attitude of mind towards some proposition of whose truth we are not certain line_end
file_end

所以我的问题是,在这种情况下,什么算法可以很好地工作。我应该学习什么才能制作这样的程序。下面是我的C++程序(我也使用ruby进行了文本处理,通过"line_end“和"file_end”获得当前形式的段落,但这不是我遇到问题的地方)
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#define MP(X,Y) (make_pair<string,string>(X,Y))
using namespace std;
map<string, vector<string> > mind_map;
set<string> ignore_these_words;
set<pair<string,string> > already_discovered;
string black_list[] = {"and","is","are","was","for","the","a","an","or","under","up","over","beside","below",
"across","to","from","by","have","had","has","been","be","it","me","you"};
vector<string> current_sentence;
int main()
{
for(int i =0; i<(sizeof(black_list)/sizeof(black_list[0])) ; i++)
ignore_these_words.insert(black_list[i] );
while(1)
{
string input_word;
cin >> input_word;
if( ignore_these_words.find(input_word) != ignore_these_words.end() )
continue;
/* if the sentence end has been reached, then insert all pairs of combinations of words in the graph
for example if the sentence is "roses are red and beautiful", then it will try to insert the following pairs of edges
after ignoring "are" and "and" from the ignore list
(roses,red)
(roses,beautiful)
(red,beautiful)
*/
if(input_word == "line_end")
{
for(int i =0; i< current_sentence.size() ; i++)
for(int j = i+1; j < current_sentence.size(); j++)
/* if we have not discovered this connection earlier */
if( already_discovered.find( MP(current_sentence[i],current_sentence[j]) ) == already_discovered.end() )
{
mind_map[current_sentence[i]].push_back( current_sentence[j]);
already_discovered.insert(MP(current_sentence[i],current_sentence[j]) );
already_discovered.insert(MP(current_sentence[j],current_sentence[i] ) );
}
current_sentence.clear();
continue;
}
/* if the file end has been reached, then output the graph in dot format */
if( input_word == "file_end")
{
cout << "graph {"<<endl;
for( map<string,vector<string> >::iterator it = mind_map.begin(); it != mind_map.end(); ++it)
for( int i =0; i< (*it).second.size(); i++)
cout<<"\""<<(*it).first<<"\""<<" -- "<<"\""<<(*it).second[i]<<"\""<<endl;
cout<< "}"<<endl;
break;
}
current_sentence.push_back(input_word);
}
return 0;
}预先感谢:).And如果有人有这样的代码,请给我,我想通过这个让我的学习更有成效。
发布于 2014-06-07 02:35:18
虽然将语言比作互联网可能有些粗糙,但我相信PageRank (谷歌搜索引擎使用的)与你正在尝试做的事情有一些重要的相似之处(创建一个地图来展示其相对重要性)。
谷歌的PageRank基于给每个网站一个相对的“重要性”。因此,当网站A有一个到网站B的链接时,B收到相对于A的重要性的“重要性”。例如,当一个没有名字的网站链接到维基百科时,维基百科的重要性得到了很小的提升,但如果维基百科提供了到另一个网站的链接,该网站就会变得更加重要,因为维基百科的重要性很大。PageRank还有更多的细微差别,但这给了我们一个体验。
类似地,为链接单词分配“方向”就像一个网站链接到另一个网站:"A is B“是A到B的”链接“。可以说”玫瑰是红色的“就像”玫瑰“重视”红色“。因为很多东西都是“红色”的,所以“红色”这个词就会获得大量的“重要性”--就像“红色”这样的普通描述性词汇在语义上对语言很重要一样。希望这能给你一个可能的方向。
https://stackoverflow.com/questions/23394672
复制相似问题