count words of different classes separately in a text file
<p>I have some text files in Persian. each file contains a lot of sentences, each in a new line. And in front of each sentence there is a tab, then a word, then a tab and then an English word. These English words in some files are 2, in some are 3, in some are 5 and in some other, are more or less. Actually, they show the sentences classes. I have to count each class's total words separately (just count the words of sentences not the words after them). For that I have to change the file to a list, so that I can achieve the sentences. Now the problem is that, how should I write the code that it returns each class's total words separately. Below is the sample sentences. </p>

<p><a href="http://i.stack.imgur.com/lMLsP.png" rel="nofollow"><img src="http://i.stack.imgur.com/lMLsP.png" alt="enter image description here"></a></p>

<pre><code>corpus = []
def CountWords (file):
    with open (file, encoding = "utf-8") as f1:
         for line in f1:
             t = line.strip().split("\t")
             corpus.append(t)
             for row in corpus:
                 if row[2] != row[2]:
</code></pre>

<p>Now I don't know how to continue. I appreciate a lot if someone can help. (I have no background in programming).</p>

<p>Try to formulate your algorithm on paper then convert it to Python: I'm sure you'll find your solution by yourself.</p>

<p>If you encounter problems or errors, post your question here, we will be happy to help.</p>

<p>An advice: </p>

<ul>
<li>You can use Ëcsv` module to read your file. Look for some tutorial with this keyword.</li>
<li>You can use Ëcollection.Counter` to count the number of occurrences of words. This can be useful. </li>
</ul>

python nlp