gpt4 book ai didi

c# - 阅读句子以制作字典或数组或关键字/关键短语列表

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:53:29 31 4
gpt4 key购买 nike

这就是问题所在,假设我们有这样一个句子:

one or more time

句子根据空格拆分。

我们的想法是制作一个字典或数组或任何合适的列表,其中包含所有唯一且递增顺序的单词,这些单词可以是关键字或关键短语。所以我应该得到这样的东西:

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++;
}

}

关于c# - 阅读句子以制作字典或数组或关键字/关键短语列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43962790/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com