Python Forum

Full Version: how can rectify error from the below code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear ALL,

import numpy

def gettingc1©:
    c1= []
    mystr = c.strip(' ').split()
    for i in mystr[:1]:
        c1.append(i)    
    return c1
    


def frequency1(c1, d):
    from collections import Counter

    termslist = Counter(i for i in d if i==c1).items()
    
    for k, v in termslist:
       
        return v
            
def maximum_frequence1(d):
    from collections import Counter 
    list0 = []
    terms= Counter(word for word in d).most_common()
    if terms[0][1] > terms[1][1]:
         list0.append([0][0])
    for k, v in terms:
        print(k)
        print(v)
        return v



def single_term_p1(c, d): 
    c1= gettingc1©
    return (frequency1 (c1, d)/ maximum_frequence1(d))

d=['biomedical informatics community', 'utility', 'domain ontologies', 'many barriers', 'effective use', 'important requirement', 'domain ontologies', 'high degree', 'coverage', 'domain concepts', 'concept relationships', 'development', u'ontology', 'prone process', 'limited resources result', u'concept', u'relationship', 'difficulty', 'ontology', 'knowledge changes', u'methodology', u'field', 'natural language processing', 'information extraction', 'information retrieval', 'machine', u'technique', 'enrichment', 'ontology', 'text documents', 'article', u'methodology', 'developed systems', 'existing methods', 'development', 'biomedical ontologies']

c= "domain ontologies"

print(single_term_p1(c, d))

OUTPUT

 File "C:/Users/user/example.py", line 49, in <module>
    print(single_term_p1(c, d))

  File "C:/Users/user/example.py", line 43, in single_term_p1
    return (frequency1 (c1, d)/ maximum_frequence1(d))

TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
This is your fifth thread. start using python code tags when posting code, error code tags when posting traceback, etc.. Not doing is breach of the forum rules.
"NoteType" is the type of None, so having TypeError: unsupported operand type(s) for /: 'NoneType' and 'int' means that you are trying to divide None by and int which likely because frequency1 (c1, d) returned None.
(Feb-28-2017, 08:45 AM)desul Wrote: [ -> ]Dear ALL,

import numpy

def gettingc1(c):
    c1= []
    mystr = c.strip(' ').split()
    for i in mystr[:1]:
        c1.append(i)    
    return c1
    


def frequency1(c1, d):
    from collections import Counter

    termslist = Counter(i for i in d if i==c1).items()
    
    for k, v in termslist:
       
        return v
            
def maximum_frequence1(d):
    from collections import Counter 
    list0 = []
    terms= Counter(word for word in d).most_common()
    if terms[0][1] > terms[1][1]:
         list0.append([0][0])
    for k, v in terms:
        print(k)
        print(v)
        return v



def single_term_p1(c, d): 
    c1= gettingc1(c)
    return (frequency1 (c1, d)/ maximum_frequence1(d))

d=['biomedical informatics community', 'utility', 'domain ontologies', 'many barriers', 'effective use', 'important requirement', 'domain ontologies', 'high degree', 'coverage', 'domain concepts', 'concept relationships', 'development', u'ontology', 'prone process', 'limited resources result', u'concept', u'relationship', 'difficulty', 'ontology', 'knowledge changes', u'methodology', u'field', 'natural language processing', 'information extraction', 'information retrieval', 'machine', u'technique', 'enrichment', 'ontology', 'text documents', 'article', u'methodology', 'developed systems', 'existing methods', 'development', 'biomedical ontologies']

c= "domain ontologies"

print(single_term_p1(c, d))
Output:
 File "C:/Users/user/example.py", line 49, in <module>
    print(single_term_p1(c, d))

  File "C:/Users/user/example.py", line 43, in single_term_p1
    return (frequency1 (c1, d)/ maximum_frequence1(d))

TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
Hello! You pass to getingc1() "domain ontologies" as an argument. Print c1  before the return statement to see what is going out. You get a single list with a single value ' domain'.


In [9]: mystr = ['domain', 'ontologies']

In [10]: c1 = []

In [11]: for i in mystr[:1]:
    ...:     c1.append(i)
    ...:     

In [12]: c1
Out[12]: ['domain']
So in frequency1() you count for a string which is not in 'd'.
In [20]: print(termslist)

dict_items([])
 
So you get an empty container from which you return 'v'. In the end the returned value from frequency1 function is None. You can't divide None with something else.
in your list d you don't have 'domain' and that is why in your frequency1 function terms is empty list. At the same time your return v is whitin the for k, v in terms loop. So at the end frequency1 just returns None.