读一个句子做一个字典或数组或列表 keywords/keyphrases
Read a sentence to make a dictionary or array or list of keywords/keyphrases
这就是问题所在,假设我们有这样一个句子:
one or more time
句子根据space</code>.</p>拆分
<p>我们的想法是制作一个字典或数组或任何合适的列表,其中包含所有唯一且递增顺序的单词,这些单词可以是关键字或关键短语。
所以我应该得到这样的东西:</p>
<pre><code>one
or
more
time
one or
one or more
one or more time
more time
...etc so on
但这些不是:
time more
or time
one time
more or
etc
这是我试过的:
private void AddWordsInDictionary(string phrase, ref Dictionary<string, string> dctKeywords)
{
var keyWords = phrase.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Trim() != string.Empty).ToList();
foreach (var keyword in keyWords)
{
if(!dctKeywords.ContainsKey(keyword))
{
foreach (var key in dctKeywords)
{
dctKeywords.Add(key + " " + keyword, key + " " + keyword);
}
}
else
AddWordsInDictionary(keyword, ref dctKeywords);
}
}
但是它一直返回一个空白字典我也不知道算法是否最优,请帮助。
找到解决方案。
private static void AddWordsInDictionary(string phrase, ref Dictionary<string, string> dctKeywords)
{
var keyWords = phrase.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Trim() != string.Empty).ToList();
Dictionary<string, string> dct = new Dictionary<string ,string>();
int id = 0;
string newPhrase = "";
foreach (var keyword in keyWords)
{
dct[id.ToString()] = keyword;
newPhrase = newPhrase + id.ToString();
id++;
}
int xx = 0;
while (xx < newPhrase.Length)
{
for (int idx = 0; idx <= newPhrase.Length; idx++)
{
if ( idx - xx > 0)
{
var item = newPhrase.Substring(xx, idx - xx).ToString();
StringBuilder sb = new StringBuilder();
foreach (var itm in item)
{
sb.Append(dct[itm.ToString()] + " ");
}
var key = sb.ToString().Trim();
dctKeywords[key] = key;
}
}
xx++;
}
}
这就是问题所在,假设我们有这样一个句子:
one or more time
句子根据space</code>.</p>拆分
<p>我们的想法是制作一个字典或数组或任何合适的列表,其中包含所有唯一且递增顺序的单词,这些单词可以是关键字或关键短语。
所以我应该得到这样的东西:</p>
<pre><code>one
or
more
time
one or
one or more
one or more time
more time
...etc so on
但这些不是:
time more
or time
one time
more or
etc
这是我试过的:
private void AddWordsInDictionary(string phrase, ref Dictionary<string, string> dctKeywords)
{
var keyWords = phrase.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Trim() != string.Empty).ToList();
foreach (var keyword in keyWords)
{
if(!dctKeywords.ContainsKey(keyword))
{
foreach (var key in dctKeywords)
{
dctKeywords.Add(key + " " + keyword, key + " " + keyword);
}
}
else
AddWordsInDictionary(keyword, ref dctKeywords);
}
}
但是它一直返回一个空白字典我也不知道算法是否最优,请帮助。
找到解决方案。
private static void AddWordsInDictionary(string phrase, ref Dictionary<string, string> dctKeywords)
{
var keyWords = phrase.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Trim() != string.Empty).ToList();
Dictionary<string, string> dct = new Dictionary<string ,string>();
int id = 0;
string newPhrase = "";
foreach (var keyword in keyWords)
{
dct[id.ToString()] = keyword;
newPhrase = newPhrase + id.ToString();
id++;
}
int xx = 0;
while (xx < newPhrase.Length)
{
for (int idx = 0; idx <= newPhrase.Length; idx++)
{
if ( idx - xx > 0)
{
var item = newPhrase.Substring(xx, idx - xx).ToString();
StringBuilder sb = new StringBuilder();
foreach (var itm in item)
{
sb.Append(dct[itm.ToString()] + " ");
}
var key = sb.ToString().Trim();
dctKeywords[key] = key;
}
}
xx++;
}
}