複数の通信社から記事をスクレイピングしていますが、記事を自動的に分類するフィルターを作成したいと考えています。
データベースには、世界のすべての国を含むテーブルと、それらの都市を含む関連テーブルがあります。
ですから、私は今、これに取り組む方法について非常に混乱しています。私が考えることができる唯一の解決策は、ストーリーの単語を分割し、すべての単語をすべての国と都市と比較することです。これにより、膨大な量のリソースが消費されると思います.
ご意見をお聞かせください。
複数の通信社から記事をスクレイピングしていますが、記事を自動的に分類するフィルターを作成したいと考えています。
データベースには、世界のすべての国を含むテーブルと、それらの都市を含む関連テーブルがあります。
ですから、私は今、これに取り組む方法について非常に混乱しています。私が考えることができる唯一の解決策は、ストーリーの単語を分割し、すべての単語をすべての国と都市と比較することです。これにより、膨大な量のリソースが消費されると思います.
ご意見をお聞かせください。
都市が見つかるまで記事を検索するのは、世界のニュース記事のように非常に非効率的であるとは思えません。BBC World News のホームページを見てみたところ、ほとんどの都市が最初の段落に記載されていました。このことから何らかの洞察を主張したら、おそらく統計学者を泣かせるでしょうが、これはほとんどの世界のニュースサイトに当てはまるかもしれないという予感があります.
残念ながら、私は Python に精通していませんが、これを実現する C# プログラムを次に示します。
class Program
{
//You could have 2 Hashsets, one for cities and one for countries and find both but you can always derive a country from a city so you won't need as much memory to reduce the load factor of the hash table.
//However this does mean if an article mentions only a country and not a city you can't categorize it.
static HashSet<String> cities = new HashSet<String>();
static Dictionary<String, String> cityToCountry = new Dictionary<String, String>();
const int MAX_WORDS_TO_READ = 200;
static void Main(string[] args)
{
addCities();
String sentance = "Former South African President Nelson Mandela is discharged from hospital in Johannesburg after 10 days of treatment for pneumonia.";
String city = findCity(sentance);
String country = cityToCountry[city];
Console.WriteLine(city + ", " + country);
Console.ReadLine();
}
static String findCity(String sentance)
{
String word = "";
int wordsSeen = 0;
foreach (char c in sentance)
{
if (c != ' ')
{
word += c;
}
else
{
if (isCity(word))
{
return word;
}
else
{
word = "";
}
wordsSeen++;
}
//If you assume that if the city is not in the first n words then the article isn't about a specific city or one that you don't have in your database
//then you can avoid having to check through massive articles that have no city
if(wordsSeen > MAX_WORDS_TO_READ)
{
return null;
}
}
return null;
}
static bool isCity(String city)
{
return cities.Contains(city);
}
static void addCities()
{
//Naturally you would parse in a list of cities from somewhere
cities.Add("Berlin");
cities.Add("London");
cities.Add("Johannesburg");
cityToCountry.Add("Berlin", "Germany");
cityToCountry.Add("London", "England");
cityToCountry.Add("Johannesburg", "South Africa");
}
}
一方、BBC ニュース イングランドのセクションを見てみると、記事自体で国について言及されていないこのような記事で終わります。都市のリストにオークハンプトンが含まれている場合、私は非常に驚かれることでしょう。この問題を解決するには、記事が掲載されているニュース サイトのセクションで指定されたコンテキストを使用できますが、結論として、この方法の効率性は、スクレイピングするニュース記事の種類によって異なります。