Great native python based answers given by other users. But here’s the nltk
approach (just in case, the OP gets penalized for reinventing what’s already existing in the nltk
library).
There is an ngram module that people seldom use in nltk
. It’s not because it’s hard to read ngrams, but training a model base on ngrams where n > 3 will result in much data sparsity.
from nltk import ngrams sentence = 'this is a foo bar sentences and i want to ngramize it' n = 6 sixgrams = ngrams(sentence.split(), n) for grams in sixgrams: print grams
Related Posts:
- working of \n in python [duplicate]
- How to remove punctuation in python?
- Python: TypeError: cannot concatenate ‘str’ and ‘int’ objects
- How to get the filename without the extension from a path in Python?
- sprintf like functionality in Python
- Alphabet range in Python
- How do I lowercase a string in Python?
- How do I get a substring of a string in Python?
- Does Python have a string ‘contains’ substring method?
- Convert bytes to a string
- Replacing instances of a character in a string
- Changing one character in a string
- How to read a file line-by-line into a list?
- What is lexicographical order?
- strip(char) on a string
- How to convert list to string [duplicate]
- TypeError: a bytes-like object is required, not ‘str’ when writing to a file in Python3
- Converting integer to string in Python
- Reverse a string in Python
- How do I compare two strings in python?
- Regex not operator
- ValueError: could not convert string to float: id
- Regex not operator
- Print string to text file
- Best way to convert string to bytes in Python 3?
- Pythonic way to create a long multi-line string
- Best way to convert string to bytes in Python 3?
- Does Python have a string ‘contains’ substring method?
- Extract a substring using PowerShell
- TypeError: not all arguments converted during string formatting python
- Extract substring in Bash
- Regex not operator
- ‘str’ object does not support item assignment
- How do I append one string to another in Python?
- How do I append one string to another in Python?
- Random string generation with upper case letters and digits
- How can I split and parse a string in Python?
- How to read a text file into a string variable and strip newlines?
- What is the difference between \r\n, \r, and \n? [duplicate]
- Convert bytes to a string
- How to print like printf in Python3?
- string to string array conversion in java
- re.sub erroring with “Expected string or bytes-like object”
- How to read a text file into a string variable and strip newlines?
- What is the difference between \r\n, \r, and \n? [duplicate]
- Python convert tuple to string
- Regex how to match an optional character
- write() versus writelines() and concatenated strings
- Why is it string.join(list) instead of list.join(string)?
- Why is it string.join(list) instead of list.join(string)?
- Split a string into 2 in Python
- Best way to strip punctuation from a string
- Split string with multiple delimiters in Python [duplicate]
- How to check if the string is empty?
- How to find all occurrences of a substring?
- How do I properly compare strings in C?
- How to remove .html from URL?
- Split string on whitespace in Python
- What is the difference between a “line feed” and a “carriage return”?
- How to convert an int to string in C?
- How do I convert a list of ascii values to a string in python?
- How to check whether a str(variable) is empty or not?
- Python TypeError: not enough arguments for format string
- What does the ‘b’ character do in front of a string literal?
- Ignoring upper case and lower case in Java
- How to add a string in a certain position?
- Check string “None” or “not” in Python 2.7
- Remove all special characters, punctuation and spaces from string
- This can be done without regex:
- How can I concatenate str and int objects?
- What does the ‘b’ character do in front of a string literal?
- How to replace all double quotes to single quotes using jquery?
- Pythonic way to create a long multi-line string
- Best way to replace multiple characters in a string?
- Convert character to ASCII numeric value in java
- Convert columns to string in Pandas
- What does strcmp() exactly return in C?
- Generate random string/characters in JavaScript
- “[ ]” vs. “[[ ]]” in Bash shell
- How to delete a character from a string using Python
- Python: Get the first character of the first string in a list?
- How to trim whitespace from a Bash variable?
- How to convert string to binary?
- Remove the first character of a string
- Convert hex to binary
- Quoting backslashes in Python string literals
- Python string class like StringBuilder in C#?
- C – The %x format specifier
- Lua string.format options
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- What are all possible pos tags of NLTK?
- Remove all special characters, punctuation and spaces from string
- How to check if a string is a substring of items in a list of strings?
- Best way to replace multiple characters in a string?
- How do I remove a substring from the end of a string?
- How can I splice a string?
- Detect whether a Python string is a number or a letter
- Finding all possible permutations of a given string in python
- How do I append one string to another in Python?
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character