索引句子的最佳算法

Luk*_*kas 5 algorithm indexing seo search

想象一下,我有一种情况需要索引句子.让我更深入地解释一下.

例如,我有这些句子:

  1. 美丽的天空.
  2. 美丽的天空梦想.
  3. 美好的梦.

据我所知,索引看起来像这样:

alt text http://img7.imageshack.us/img7/4029/indexarb.png

但我也想通过任何这些词进行搜索.

例如,如果我通过"the"搜索它应该显示给我连接到"美丽".如果我按"美丽"搜索,它应该给我(上一个)"The",(下一个)"sky"和"dream"的连接.如果我通过"天空"搜索它应该给(之前)连接到"美丽"等...

有任何想法吗 ?也许你知道这种问题的现有算法?

Unk*_*own 5

简答

使用两个前/后链接向量创建一个结构.然后将单词structs存储在哈希表中,并将键作为单词本身.

答案很长

这是一个语言解析问题,除非你不介意胡言乱语,否则不容易解决.

  1. 我去了公园篮球场.
  2. 你会把车停好吗?

您的链接算法将创建如下句子:

  1. 我去了公园的车.
  2. 你会把篮球场停放吗?

我不太确定这个搜索引擎优化应用程序,但我不欢迎另一个乱码垃圾网站占用搜索结果.


Dar*_*ark 0

这应该会让你很接近,在 C# 中:

class Program
{
    public class Node
    {
        private string _term;
        private Dictionary<string, KeyValuePair<Node, Node>> _related = new Dictionary<string, KeyValuePair<Node, Node>>();

        public Node(string term)
        {
            _term = term;
        }

        public void Add(string phrase, Node previous, string [] phraseRemainder, Dictionary<string,Node> existing)
        {
            Node next= null;
            if (phraseRemainder.Length > 0)
            {
                if (!existing.TryGetValue(phraseRemainder[0], out next))
                {
                    existing[phraseRemainder[0]] = next = new Node(phraseRemainder[0]);
                }
                next.Add(phrase, this, phraseRemainder.Skip(1).ToArray(), existing);
            }
            _related.Add(phrase, new KeyValuePair<Node, Node>(previous, next));

        }
    }


    static void Main(string[] args)
    {
        string [] sentences = 
            new string [] { 
                "The beautiful sky",
                "Beautiful sky dream",
                "beautiful dream"
            };

        Dictionary<string, Node> parsedSentences = new Dictionary<string,Node>();

        foreach(string sentence in sentences)
        {
            string [] words = sentence.ToLowerInvariant().Split(' ');
            Node startNode;
            if (!parsedSentences.TryGetValue(words[0],out startNode))
            {
                parsedSentences[words[0]] = startNode = new Node(words[0]);
            }
            if (words.Length > 1)
                startNode.Add(sentence,null,words.Skip(1).ToArray(),parsedSentences);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我冒昧地假设您想保留实际的初始短语。最后,您将获得短语中的单词列表,以及每个短语中使用该单词的短语列表,以及每个短语中下一个和上一个单词的引用。