Blog Archive

Friday, February 28, 2020

C++ : How to Initialize a map in one line using initialzer_list ?

https://thispointer.com/how-to-initialize-a-map-with-stdinitialzer_list/

Syntax:
{
{key1, val1}, 
{key2, val2},
...
{keyN, valN}
}

Hint:
map store the elements internally as a pair, i.e., key-value pair.


Example1: 

Save as: your_file.cpp
#include <vector>
#include <map>
#include <initializer_list>
#include <iostream>
 
class Course {
std::map<std::string, int> mMapOfMarks;
 
public:
Course(std::initializer_list<std::pair<const std::string, int> > marksMap) :
mMapOfMarks(marksMap) {
}
void display() {
for (auto entry : mMapOfMarks)
std::cout << entry.first << " :: " << entry.second << std::endl;
std::cout << std::endl;
}
 
};
 
int main() {
// Creating a Course Object and calling the constructor
// that accepts a initializer_list
Course mathsCourse { { "Riti", 2 }, { "Jack", 4 } };
 
mathsCourse.display();
return 0;
}

compile:
g++ -std=c++11 your_file.cpp -o your_program


Friday, February 14, 2020

[cheatsheet] DOS/C++/ Python



https://www.hoomanb.com/cs/quickref/CppQuickRef.pdf


https://web.pa.msu.edu/people/duxbury/courses/phy480/python_refcard.pdf

http://www.cs.columbia.edu/~sedwards/classes/2015/1102-fall/Command%20Prompt%20Cheatsheet.pdf

https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/cc754340(v=ws.11)?redirectedfrom=MSDN

https://peoplesofttutorial.com/ms-dos-command-reference-cheat-sheet/


dos:

doskey /history > historyLog.txt
dir someDir\subDir
cd filepath chdir filepath
cd .. (goes one directory up)
md mkdir Creates a folder (directory)
md folder-name
mkdir folder-name

rm folder-name
rmdir folder-name
rm /s folder-name
rmdir /s folder-name
Note: if the folder isn’t empty, you must add the /s.

 Copies a file from one location to another
copy filepath-from filepath-to

move Moves file from one folder to another
move folder1\file.txt folder2\ ren

 rename Changes the name of a file
ren file1 file2

del
Deletes one or more files
del filename

exit Exits batch script or current command control
exit

echo Used to display a message or to turn off/on messages in batch scripts
echo message

type Displays contents of a text file
type myfile.txt

fc Compares two files and displays the difference between them
fc file1 file2

cls Clears the screen
cls
Ctrl + Home will clear all characters in the command input to the left of the cursor.
Ctrl + End does the same for all characters to the right of the cursor.

cmp:compare the size of two files
cmp file1 file2

fc: This batch command lists the actual differences between two files.
fc file1 file2


find: This batch command searches for a string in files or input, outputting matching lines.
find "pattern" fileToBeSearched

set: Displays the list of environment variables on the current system. It can also set or remove

help Provides more details about DOS/Command Prompt commands

help (lists all commands)

help command
command /?

whoami displays domain\userName
whoami

pushd
Stores the current directory for use by the popd command, and then changes to the specified directory. For examples of how to use this command, see Examples. Syntax Copy pushd [<Path>]

How to get help:
1) Unix: man yourCmd

2) Python: help(yourCmd)

https://en.wikibooks.org/wiki/Python_Programming/Self_Help
help()      # Starts an interactive help
help("topics")  # Outputs the list of help topics
help("OPERATORS") # Shows help on the topic of operators
help("len")    # Shows help on len function
help("re")    # Shows help on re module
help("re.sub")  # Shows help on sub function from re module
help(len)     # Shows help on the object passed, the len function
help([].pop)   # Shows help on the pop function of a list
dir([])      # Outputs a list of attributes of a list, which includes functions
import re
help(re)     # Shows help on the help module
help(re.sub)   # Shows help on the sub function of re module
help(1)      # Shows help on int type
help([])     # Shows help on list type
help(def)     # Fails: def is a keyword that does not refer to an object
help("def")    # Shows help on function definitions


3) DOS: help yourCmd   (Option2: yourCmd /?)

Monday, February 10, 2020

[kaldi] How to fix the error of "Expected token FM, got FV"

Answer:
Try use copy-vector instead of copy-feats when you are using kaldi.
The error message actually saying Expected token Feature Matrix, got Feature Vector instead

Kaldi defines its own binary format, ark (archive format), this article explains how to read and write or convert this format.

Read/write Kaldi features

We introduce the reading and writing of two type of features here, matrix like MFCC feature, and vector based iVector and VAD vector.

Read Kaldi MFCC features

Raw MFCC Feature location

Most Kaldi features are stored in mfcc folder with an extension of ark or scp.

The ark file

The ark stores raw features, its size is normally in few hundred MBs.
Eg: 20 dimensional MFCC features matrix is stored in the ark file like following:
UtteranceID1 [ d1 d2 d3 d4 d5 .. d20\n d1 d2 d3 d4 d5 .. d20\n d1 d2 d3 d4 d5 .. d20\n ...]\n
UtteranceID2 [ d1 d2 d3 d4 d5 .. d20\n d1 d2 d3 d4 d5 .. d20\n ]\n
Where \n means new line.
To view raw feature, type the following command in the terminal
copy-feats ark:./abc.ark ark,t:
This command means copy the feature from input source (ark:source) to output target (ark,t:target),here we leave “target” empty so the feature will print to the terminal. Following command will dump the features to text file
copy-feats ark:./abc.ark ark,t:a.txt
And dump to binary file:
copy-feats ark:./abc.ark ark:a.bin

The scp file

It is often saw a scp file which describes the content of an ark file.
The scp is only a text file, with following format:
UtteranceID1 arkLocation1:offset1 
UtteranceID2 arkLocation2:offset2
Following two commands will give same results
copy-feats scp:./abc.scp ark,t:yourfilename
copy-feats ark:./abc.ark ark,t:yourfilename

Features in the data folder

feats.scp and vad.scp are two feature descriptors in the Kaldi data folder.

Write kaldi MFCC features

One can write kaldi feature to the ark follow the given text format. However most script in Kaldi require its scp file, one way to create scp file is:
copy-feats ark:./abc.ark ark,scp:b.ark,b.scp

Read/write kaldi iVector or VAD vector

Both iVector and VAD vector are in vector form. Read/write kaldi iVector is similar to read/write MFCC feature, only replace the copy-feats with copy-vector command.

Raw iVetor Feature location

Most Kaldi iVector are stored in “exp” folder with an extension of ark or scp.
The VADs are stored in ‘mfcc’ folder.

Read Kaldi iVector

iVector is sorted in the ark in a vector format, please notice that there is a ‘SPACE’ after ‘[’ and no ‘SPACE’ in UtteranceID:
UtteranceID1 [ d1 d2 d3 d4 d5 .. d400 ]\n
UtteranceID2 [ d1 d2 d3 d4 d5 .. d400 ]\n
VAD have same format except the length of VAD is depends on the length of utterance. 1 for voiced frame , 0 for unvoice frame. 1 frame= 10ms
Similar to read MFCC feature,following command will read the iVector:
copy-vector ark:./ivector.1.ark ark,t:YOUR_TEXT_FILE

Write Kaldi iVector

Once you know the iVector format, you can write it back easily. Following command will convert your ark file to Kaldi format, and generate a scp file.
copy-vector ark,t:./your.ark ark,scp:kaldi.ivectors.ark,kaldi.ivectors.scp


FAQ:

Where is the copy-feats and copy-vector ? It is in your kaldi src folder. You need include their directory in your path. Please refer to the path.sh in the this SRE folder.
To load the setting in the path.sh, type this in terminal in SRE2010 folder
. path.sh
Or you can put the setting in ~/.profile or ~/.bashrc
Reference:
https://wsstriving.github.io/2016/04/17/how-to-check-kaldi-ark/
https://github.com/StevenLOL/Research_speech_speaker_verification_nist_sre2010/blob/master/doc/help_kaldi.md