gpt4 book ai didi

c# - 检查文本中的正负字符串(C# 到 Objective-C )

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:21 25 4
gpt4 key购买 nike

我有一个文本字符串,我想检查它的几个条件。我在 C# 中有这个函数,我想将它移植到 Objective-C (我是新手)如果检查函数包含 (1) 中的任何一个但不包含 (2) 中的任何一个,则检查函数应返回 true

(1) 包含以下任何内容:

keyword1 

keyword2 AND keyword3

keyword4

(2) 不应包含任何这些关键字

keyword4

keyword5

keyword6

我在 C# 中使用 LINQ 完成了这个功能:

  public static string ProcessText(string text, string postiveKeywords, string negativeKeywords)
{
//prepare the strings to lists
var simpleTextKeywords = text.Split(' ');

//trim any other characters
for (int i = 0; i < simpleTextKeywords.Length; i++ )

{
simpleTextKeywords[i] = simpleTextKeywords[i].Trim(
'~','!','@','#','$','%','^','&','*','(',')','_','+','-','=','[',']',';','\'','\\',',','.','/','{','}',':','"','|','<','>','?');
}

string [] listOfKeywords;
if ( !string.IsNullOrWhiteSpace(postiveKeywords))
listOfKeywords = postiveKeywords.Split(',');
else
listOfKeywords = new string[] { };

string[] listOfnegativeKeywords;
if ( !string.IsNullOrWhiteSpace(negativeKeywords ))
listOfnegativeKeywords = negativeKeywords.Split(',');
else
listOfnegativeKeywords = new string[] { };


//parse the keywordlist
var matches = listOfKeywords
.Any(OR => OR.Split('&').All(kw => simpleTextKeywords.Contains(kw)
&& !listOfnegativeKeywords.Any(x => simpleTextKeywords.Any(y => x == y))
&& !string.IsNullOrWhiteSpace(kw)));
if (!matches)
{
return string.Empty;
}

//return the first match
return listOfKeywords
.First(OR => OR.Split('&')
.All(kw => simpleTextKeywords.Contains(kw)
&& !listOfnegativeKeywords.Any(x => simpleTextKeywords.Any(y => x == y))
&& !string.IsNullOrWhiteSpace(kw)));
}

更新:以下是到目前为止的代码:

+ (bool )ProcessText:(NSString*)text
andPositiveKeyword:(NSString*)positiveKeywords
andNegativeKeyword:(NSString*)negativeKeywords
{
//split the text words
NSMutableArray* simpleTextKeywords = [(NSArray*)[text componentsSeparatedByString:@" "] mutableCopy];

//trim unwanted characters
for (int i=0; i< simpleTextKeywords.count; i++) {
simpleTextKeywords[i] = [simpleTextKeywords[i] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"~!@#$%^&*()_+-=[];\'\\,./{}:\"|<>?"]];
}

NSArray* listOfPositiveKeywords = [[NSArray alloc] init];
if ( ![NSString stringIsNilOrEmpty:positiveKeywords])
{
listOfPositiveKeywords = [positiveKeywords componentsSeparatedByString:@","];
}

NSArray* listOfNegativeKeywords = [[NSArray alloc] init];
if ( ![NSString stringIsNilOrEmpty:negativeKeywords])
{
listOfNegativeKeywords = [ negativeKeywords componentsSeparatedByString:@","];
}

BOOL matches = false;
for (int i=0; i< listOfPositiveKeywords.count; i++) {
//if the keyword has "&"
//all matches should be true
//split the &
NSArray* andKeywords = [listOfPositiveKeywords[i] componentsSeparatedByString:@"&"];
matches = true;
for (int j=0; j<andKeywords.count; j++) {
if ( ![simpleTextKeywords containsObject:andKeywords[j]] )
{
matches = false;
break;
}
}
if ( matches == true )
break;
}

//if there are any matches we have to check if it doesnt have any negative keyword
if ( matches)
{
for ( int i =0; i < listOfNegativeKeywords.count;i++)
{
if ( [simpleTextKeywords containsObject:listOfNegativeKeywords[i]])
matches = false;
}
}
return matches;
}

最佳答案

将要检查的肯定和否定关键字和文本列表转换为 NSSet 的实例。然后使用 NSSet containsObject: 方法检查是否包含。

使用NSString方法componentsSeparatedByString:将要搜索的文本拆分为字符串数组。

使用 NSString 方法 stringByTrimmingCharactersInSet: 和使用 NSCharacterSet 方法创建的集合:characterSetWithCharactersInString: 来修剪不需要的字符。

修剪示例代码:

NSString *trimCharacters = @"~!@#$%^&*()_+-=[];\'\\,./{}:\"|<>?";
NSCharacterSet *trimCharacterSet = [NSCharacterSet characterSetWithCharactersInString:trimCharacters];
NSMutableArray *simpleTextKeywords = [NSMutableArray new];
for (NSString *item in textArray) {
[simpleTextKeywords addObject:[item stringByTrimmingCharactersInSet:trimCharacterSet]];
}

关于c# - 检查文本中的正负字符串(C# 到 Objective-C ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23653379/

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