Matching company names in the news data using Python
<p>I have news dataset which contains almost 10,000 news over the last 3 years.
I also have a list of companies (names of companies) which are registered in NYSE. Now I want to check whether list of company names in the list have appeared in the news dataset or not.
Example:</p>

<pre><code>company Name: 'E.I. du Pont de Nemours and Company'
News: 'Monsanto and DuPont settle major disputes with broad patent-licensing deal, with DuPont agreeing to pay at least $1.75 billion over 10 years for rights to technology for herbicide-resistant soybeans.'
</code></pre>

<p>Now, I can find the news contains company name if the exact company name is in the news but you can see from the above example it is not the case. 
I also tried another way i.e. I took the integral name in the company's full name i.e. in the above example 'Pont' is a word which should be definitely a part of the text when this company name is called. So it worked for majority of the times but then problem occurs in the following example:</p>

<pre><code>Company Name: Ennis, Inc.
News: L D`ennis` Kozlowski, former chief executive convicted of looting nearly $100 million from Tyco International, has emerged into far more modest life after serving six-and-a-half year sentence and probation; Kozlowski, who became ultimate symbol of corporate greed in era that included scandals at Enron and WorldCom, describes his personal transformation and more humble pleasures that have replaced his once high-flying lifestyle.
</code></pre>

<p>Now you can see <code>Ennis</code> is matching with <code>Dennis</code> in the text so it giving irrelevant news results. </p>

<p>Can someone help in telling the right way of doing this ? Thanks.</p>

<p>Use a regex with  <a href="http://www.rexegg.com/regex-boundaries.html" rel="nofollow">boundaries</a> for exact matches whether you choose the full name or some partial part you think is unique is up to you but using word boundaries <code>D'ennis'</code> won't match <code>Ennis</code> :</p>

<pre><code>companies = ["name1", "name2",...]
companies_re = re.compile(r"|".join([r"\b{}\b".format(name) for name in companies]))
</code></pre>

<p>Depending on how many matches per news item, you may want to use <code>companies_re.search(artice)</code> or <code>companies_re.find_all(article)</code>. 
Also for  case insensitive matches pass <code>re.I</code> to compile.</p>

<p>If the only line you want to check is also always the one starting with company <code>company Name:</code> you can narrow down the search:</p>

<pre><code>for line in all_lines:
  if line.startswith("company Name:"):
      name = companies_re.search(line) 
      if name:
         ...
      break
</code></pre>

python python-3.x machine-learning