Files
KJD/Work/Api/FromTitleToCatTaobao.cs
T
2025-06-29 19:38:31 +08:00

351 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using AllBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Work.Api
{
public class FromTitleToCatTaobao
{
/// <summary>
/// 用标题计算分类
/// </summary>
/// <param name="title"></param>
/// <returns></returns>
public static int Get(string title, bool IsGetMoreCatid = false)
{
title = BaseRegex.Clear(title, @"([\d]+)");
char[] k = "[]\\/ -+=.【】{};,。,.::;*()()@!!&{}".ToCharArray();
foreach (char c in k)
title = title.Replace(c.ToString(), "");
string[] keys = "批发,新款,特价,热卖,定制,定做,热卖,正品,包邮,正品,出售,新品,厂家,直销,自营,跨境,爆款,批发,品牌,一件,代发,清仓,直供,外贸".Split(',');
foreach (string c in keys)
title = title.Replace(c, "");
if (string.IsNullOrEmpty(title) || title.Length < 2)
return 0;
Dictionary<int, int> cats = new Dictionary<int, int>();
string apiUrl = "http://tcat.qixiaoke.com/taobao/keyword.php?keyword=";
string html = GetHtml.Utf8(apiUrl + GetHtml.UrlEncodeUtf8(title));
if (!string.IsNullOrEmpty(html))
{
string[] l = html.Split(',');
foreach (string ss in l)
{
if (string.IsNullOrEmpty(ss))
continue;
int s = int.Parse(ss);
if (cats.ContainsKey(s))
cats[s]++;
else
cats.Add(s, 1);
}
int top = 0;
int all = 0;
int catId = 0;
foreach (KeyValuePair<int, int> cat in cats)
{
if (top < cat.Value)
{
top = cat.Value;
catId = cat.Key;
}
all += cat.Value;
}
return catId;
}
int Step = 2;
int half = title.Length / 2;
string key1 = title.Substring(0, half);
string key2 = title.Substring(half);
SetCats(key1, ref cats);
SetCats(key2, ref cats);
int cid = GetMaxCount(ref cats, title.Length > 8 ? 4 : 16);
if (IsGetMoreCatid && cats.Count > 5)
{
return cid;
}
else if (cats.Count > 0 && !IsGetMoreCatid)
{
return cid;
}
Step = 4;
AddCount(ref cats);
int four = title.Length / 4;
if (four < 2)//没法继续细分了
return GetMaxCount(ref cats, 4096);
for (int i = 0; i < 4; i++)
{
string key = title.Substring(i * four, four);
SetCats(key, ref cats);
}
cid = GetMaxCount(ref cats, title.Length > 16 ? 16 : 64);
if (cid > 0)
return cid;
AddCount(ref cats);
Step = 8;
int eigth = title.Length / 8;
if (eigth < 2)
return GetMaxCount(ref cats, 4096);
for (int i = 0; i < 8; i++)
{
string key = title.Substring(i * eigth, eigth);
SetCats(key, ref cats);
}
return GetMaxCount(ref cats, 4096);
}
public static string GetCategory(string title)
{
if (string.IsNullOrEmpty(title)) return string.Empty;
string name = title;
title = BaseRegex.Clear(title, @"([\d]+)");
char[] k = "[]\\/ -+=.【】{};,。,.::;*()()@!!&{}".ToCharArray();
foreach (char c in k)
title = title.Replace(c.ToString(), "");
string[] keys = "批发,新款,特价,热卖,定制,定做,热卖,正品,包邮,正品,出售,新品,厂家,直销,自营,跨境,爆款,批发,品牌,一件,代发,清仓,直供,外贸".Split(',');
foreach (string c in keys)
title = title.Replace(c, "");
if (string.IsNullOrEmpty(title) || title.Length < 2)
return string.Empty;
Dictionary<int, int> cats = new Dictionary<int, int>();
string apiUrl = "http://tcat.qixiaoke.com/taobao/keyword.php?keyword=";
string html = GetHtml.Utf8(apiUrl + GetHtml.UrlEncodeUtf8(title));
if (!string.IsNullOrEmpty(html))
{
string[] l = html.Split(',');
foreach (string ss in l)
{
if (string.IsNullOrEmpty(ss))
continue;
int s = int.Parse(ss);
if (cats.ContainsKey(s))
cats[s]++;
else
cats.Add(s, 1);
}
int top = 0;
int all = 0;
int catId = 0;
foreach (KeyValuePair<int, int> cat in cats)
{
if (top < cat.Value)
{
top = cat.Value;
catId = cat.Key;
}
all += cat.Value;
}
return name + ":" + catId;
}
int Step = 2;
int half = title.Length / 2;
string key1 = title.Substring(0, half);
string key2 = title.Substring(half);
SetCats(key1, ref cats);
SetCats(key2, ref cats);
int cid = GetMaxCount(ref cats, title.Length > 8 ? 4 : 16);
if (cid > 0)
return name + ":" + cid;
Step = 4;
AddCount(ref cats);
int four = title.Length / 4;
if (four < 2)//没法继续细分了
return name + ":" + GetMaxCount(ref cats, 4096);
for (int i = 0; i < 4; i++)
{
string key = title.Substring(i * four, four);
SetCats(key, ref cats);
}
cid = GetMaxCount(ref cats, title.Length > 16 ? 16 : 64);
if (cid > 0)
return name + ":" + cid;
AddCount(ref cats);
Step = 8;
int eigth = title.Length / 8;
if (eigth < 2)
return name + ":" + GetMaxCount(ref cats, 4096);
for (int i = 0; i < 8; i++)
{
string key = title.Substring(i * eigth, eigth);
SetCats(key, ref cats);
}
return name + ":" + GetMaxCount(ref cats, 4096);
}
/// <summary>
/// 用标题计算分类(为了保证防止跟之前冲突 特意写个方法)
/// </summary>
/// <param name="title"></param>
/// <returns></returns>
public int GetCataryId(string title, out Dictionary<int, int> DicTbCatId)
{
Dictionary<int, int> DicTbCatIdTemp = new Dictionary<int, int>();
title = BaseRegex.Clear(title, @"([\d]+)");
char[] k = "[]\\/ -+=.【】{};,。,.::;*()()@!!&{}".ToCharArray();
foreach (char c in k)
title = title.Replace(c.ToString(), "");
string[] keys = "批发,新款,特价,热卖,定制,定做,热卖,正品,包邮,正品,出售,新品,厂家,直销,自营,跨境,爆款,批发,品牌,一件,代发,清仓,直供,外贸".Split(',');
foreach (string c in keys)
title = title.Replace(c, "");
DicTbCatId = DicTbCatIdTemp;
if (string.IsNullOrEmpty(title) || title.Length < 2)
return 0;
Dictionary<int, int> cats = new Dictionary<int, int>();
string apiUrl = "http://tcat.qixiaoke.com/taobao/keyword.php?keyword=";
string html = GetHtml.Utf8(apiUrl + GetHtml.UrlEncodeUtf8(title));
if (!string.IsNullOrEmpty(html))
{
string[] l = html.Split(',');
foreach (string ss in l)
{
if (string.IsNullOrEmpty(ss))
continue;
int s = int.Parse(ss);
if (cats.ContainsKey(s))
cats[s]++;
else
cats.Add(s, 1);
}
int top = 0;
int all = 0;
int catId = 0;
foreach (KeyValuePair<int, int> cat in cats)
{
if (top < cat.Value)
{
top = cat.Value;
catId = cat.Key;
}
all += cat.Value;
}
return catId;
}
int Step = 2;
int half = title.Length / 2;
string key1 = title.Substring(0, half);
string key2 = title.Substring(half);
SetCats(key1, ref cats);
SetCats(key2, ref cats);
int cid = GetMaxCount(ref cats, title.Length > 8 ? 4 : 16);
DicTbCatId = cats;
if (cats.Count > 5)
{
return cid;
}
Step = 4;
AddCount(ref cats);
DicTbCatId = cats;
int four = title.Length / 4;
if (four < 2)//没法继续细分了
return GetMaxCount(ref cats, 4096);
for (int i = 0; i < 4; i++)
{
string key = title.Substring(i * four, four);
SetCats(key, ref cats);
}
cid = GetMaxCount(ref cats, title.Length > 16 ? 16 : 64);
DicTbCatId = cats;
if (cid > 0)
return cid;
AddCount(ref cats);
Step = 8;
int eigth = title.Length / 8;
DicTbCatId = cats;
if (eigth < 2)
return GetMaxCount(ref cats, 4096);
for (int i = 0; i < 8; i++)
{
string key = title.Substring(i * eigth, eigth);
SetCats(key, ref cats);
}
DicTbCatId = cats;
return GetMaxCount(ref cats, 4096);
}
/// <summary>
/// 根据关键词获取相关分类
/// </summary>
/// <param name="key"></param>
/// <param name="cats"></param>
public static void SetCats(string key, ref Dictionary<int, int> cats)
{
string apiUrl = "http://tcat.qixiaoke.com/taobao/keyword.php?keyword=";
string html = GetHtml.Utf8(apiUrl + GetHtml.UrlEncodeUtf8(key));
if (!string.IsNullOrEmpty(html))
{
string[] l = html.Split(',');
foreach (string ss in l)
{
if (string.IsNullOrEmpty(ss))
continue;
int s = int.Parse(ss);
if (cats.ContainsKey(s))
cats[s]++;
else
cats.Add(s, 1);
}
}
}
/// <summary>
/// 根据倍数判断是否要取得准确结果
/// </summary>
/// <param name="cats"></param>
/// <param name="times"></param>
/// <returns></returns>
public static int GetMaxCount(ref Dictionary<int, int> cats, int times)
{
int top = 0;
int all = 0;
int catId = 0;
foreach (KeyValuePair<int, int> cat in cats)
{
if (top < cat.Value)
{
top = cat.Value;
catId = cat.Key;
}
all += cat.Value;
}
if (all / times < top)
return catId;
return 0;
}
/// <summary>
/// 权值翻倍
/// </summary>
/// <param name="cats"></param>
public static void AddCount(ref Dictionary<int, int> cats)
{
try
{
foreach (KeyValuePair<int, int> cat in cats)
{
cats[cat.Key] *= 2;
}
}
catch
{
}
}
}
}