Searching Index from list of tuples












0















I have a sentence = "hi there, my car number is H 11231, and my card number is 11122" . I tokenized the sentence then POS tagged the tokenized sentence. I want to grab the car number, I created a loop to check if the index is at a number lets say (11231). Then check 1 tuple before or after if it has the tag NNP (which stands for 1 Letter)



import nltk

sentence = 'hi there, my car number is H 11231, and my card number is 11122'

tokenizedSent = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokenizedSent)
print(tagged)
output =

print(tagged)
for i in tagged:
if i[1] == 'CD':
output.append(i[0])
elif i[1] == 'NNP':
output.append(i[0])


The sentence has two numbers which are 11231 and 11122. However, only one of them is the car number which is the one before is tagged by NNP










share|improve this question




















  • 1





    Please type-in the code and the output in the question instead of posting screenshot

    – Andreas
    Nov 28 '18 at 8:26
















0















I have a sentence = "hi there, my car number is H 11231, and my card number is 11122" . I tokenized the sentence then POS tagged the tokenized sentence. I want to grab the car number, I created a loop to check if the index is at a number lets say (11231). Then check 1 tuple before or after if it has the tag NNP (which stands for 1 Letter)



import nltk

sentence = 'hi there, my car number is H 11231, and my card number is 11122'

tokenizedSent = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokenizedSent)
print(tagged)
output =

print(tagged)
for i in tagged:
if i[1] == 'CD':
output.append(i[0])
elif i[1] == 'NNP':
output.append(i[0])


The sentence has two numbers which are 11231 and 11122. However, only one of them is the car number which is the one before is tagged by NNP










share|improve this question




















  • 1





    Please type-in the code and the output in the question instead of posting screenshot

    – Andreas
    Nov 28 '18 at 8:26














0












0








0








I have a sentence = "hi there, my car number is H 11231, and my card number is 11122" . I tokenized the sentence then POS tagged the tokenized sentence. I want to grab the car number, I created a loop to check if the index is at a number lets say (11231). Then check 1 tuple before or after if it has the tag NNP (which stands for 1 Letter)



import nltk

sentence = 'hi there, my car number is H 11231, and my card number is 11122'

tokenizedSent = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokenizedSent)
print(tagged)
output =

print(tagged)
for i in tagged:
if i[1] == 'CD':
output.append(i[0])
elif i[1] == 'NNP':
output.append(i[0])


The sentence has two numbers which are 11231 and 11122. However, only one of them is the car number which is the one before is tagged by NNP










share|improve this question
















I have a sentence = "hi there, my car number is H 11231, and my card number is 11122" . I tokenized the sentence then POS tagged the tokenized sentence. I want to grab the car number, I created a loop to check if the index is at a number lets say (11231). Then check 1 tuple before or after if it has the tag NNP (which stands for 1 Letter)



import nltk

sentence = 'hi there, my car number is H 11231, and my card number is 11122'

tokenizedSent = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokenizedSent)
print(tagged)
output =

print(tagged)
for i in tagged:
if i[1] == 'CD':
output.append(i[0])
elif i[1] == 'NNP':
output.append(i[0])


The sentence has two numbers which are 11231 and 11122. However, only one of them is the car number which is the one before is tagged by NNP







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 8:52









Andreas

2,05731323




2,05731323










asked Nov 28 '18 at 8:21









Abdulla AhliAbdulla Ahli

52




52








  • 1





    Please type-in the code and the output in the question instead of posting screenshot

    – Andreas
    Nov 28 '18 at 8:26














  • 1





    Please type-in the code and the output in the question instead of posting screenshot

    – Andreas
    Nov 28 '18 at 8:26








1




1





Please type-in the code and the output in the question instead of posting screenshot

– Andreas
Nov 28 '18 at 8:26





Please type-in the code and the output in the question instead of posting screenshot

– Andreas
Nov 28 '18 at 8:26












1 Answer
1






active

oldest

votes


















0














Your solution traverse the collection of tag and only takes CD and NNP tags.



You want only numbers.



so first thing to do : get the numbers :



IndexTag = namedtuple('IndexTag', ['tag', 'index'])
numbers =
for i, tag in enumerate(tagged):
if tag[1] == 'number':
numbers.append(IndexTag(tag, i))


now that you have your numbers you can check that the "previous tag is NNP" :



car_ids = 
for number in numbers:
if number.index > 0 and tagged[number.index - 1][1] == 'NNP':
car_ids.append(number)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53514989%2fsearching-index-from-list-of-tuples%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Your solution traverse the collection of tag and only takes CD and NNP tags.



    You want only numbers.



    so first thing to do : get the numbers :



    IndexTag = namedtuple('IndexTag', ['tag', 'index'])
    numbers =
    for i, tag in enumerate(tagged):
    if tag[1] == 'number':
    numbers.append(IndexTag(tag, i))


    now that you have your numbers you can check that the "previous tag is NNP" :



    car_ids = 
    for number in numbers:
    if number.index > 0 and tagged[number.index - 1][1] == 'NNP':
    car_ids.append(number)





    share|improve this answer




























      0














      Your solution traverse the collection of tag and only takes CD and NNP tags.



      You want only numbers.



      so first thing to do : get the numbers :



      IndexTag = namedtuple('IndexTag', ['tag', 'index'])
      numbers =
      for i, tag in enumerate(tagged):
      if tag[1] == 'number':
      numbers.append(IndexTag(tag, i))


      now that you have your numbers you can check that the "previous tag is NNP" :



      car_ids = 
      for number in numbers:
      if number.index > 0 and tagged[number.index - 1][1] == 'NNP':
      car_ids.append(number)





      share|improve this answer


























        0












        0








        0







        Your solution traverse the collection of tag and only takes CD and NNP tags.



        You want only numbers.



        so first thing to do : get the numbers :



        IndexTag = namedtuple('IndexTag', ['tag', 'index'])
        numbers =
        for i, tag in enumerate(tagged):
        if tag[1] == 'number':
        numbers.append(IndexTag(tag, i))


        now that you have your numbers you can check that the "previous tag is NNP" :



        car_ids = 
        for number in numbers:
        if number.index > 0 and tagged[number.index - 1][1] == 'NNP':
        car_ids.append(number)





        share|improve this answer













        Your solution traverse the collection of tag and only takes CD and NNP tags.



        You want only numbers.



        so first thing to do : get the numbers :



        IndexTag = namedtuple('IndexTag', ['tag', 'index'])
        numbers =
        for i, tag in enumerate(tagged):
        if tag[1] == 'number':
        numbers.append(IndexTag(tag, i))


        now that you have your numbers you can check that the "previous tag is NNP" :



        car_ids = 
        for number in numbers:
        if number.index > 0 and tagged[number.index - 1][1] == 'NNP':
        car_ids.append(number)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 8:30









        artragisartragis

        3,47911225




        3,47911225
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53514989%2fsearching-index-from-list-of-tuples%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)